0

I have a similar issue with this post that I get status 404 instead of index page by adding context path. I doubt that I did something wrong like put the @EnableWebSecurity in the WebSecurityConfig file or in the Controller.

Here is the spring-boot config

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers(
            "/registration**",
            "/js/**",
            "/css/**",
            "/img/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/bad-404")
            .defaultSuccessUrl("/")
            .usernameParameter("email") //needed, if custom login page
            .passwordParameter("password") //needed, if custom login page                
            .permitAll()
            .and()
            .logout()
            .invalidateHttpSession(true)
            .clearAuthentication(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/login?logout")
            .permitAll();

}

Here is the main class to run the app

public static void main(String[] args) {
    System.setProperty("server.servlet.context-path", "/index");
    SpringApplication.run(SmartcardApplication.class, args);
}

Here is the Controller class

@Controller
public class MainController {

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/")
    public String home(){
        return "index";
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Stu_Dent
  • 370
  • 1
  • 4
  • 19
  • When you add context path to your spring boot application then all your url becomes `http://localhost:8080/index/*` – code_mechanic Sep 20 '20 at 04:36
  • @code_mechanic it started local8080/login. When I change to local8080/index or local/index it gaves me 404. – Stu_Dent Sep 20 '20 at 05:52
  • 1
    You mean calling like `http://localhost:8080/index/login` fails with 404? – code_mechanic Sep 20 '20 at 06:10
  • @code_mechanic No, I want to show localhost/index or localhost:8080/index when I run this program instead of the login page. What you mentioned it works, but it not what I want. Thanks – Stu_Dent Sep 20 '20 at 08:02
  • 1
    I asked about login because context-path is applied to every url now, if that works, then the simple `/` should work as well, anyways will check it again. – code_mechanic Sep 20 '20 at 08:05
  • @code_mechanic I found that it is restricted to the login page every time I run the program unless I register a user in dB (MongoDB). – Stu_Dent Sep 20 '20 at 09:31

0 Answers0