Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing context path(root folder) does not show login page in Spring Boot Application

In my Spring Boot(2.0) application, I have set the context path in my application.properties file as below

server.servlet.context-path=/myApp

Also, I have the following security configurations class extending WebSecurityConfigurerAdapter

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // TODO Auto-generated method stub


        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.GET,"/uifiles/dist/css/**").permitAll()
        .antMatchers(HttpMethod.GET,"/uifiles/plugins/icheck-bootstrap/**").permitAll()
        .antMatchers(HttpMethod.GET,"/uifiles/plugins/fontawesome-free/css/**").permitAll()
        .antMatchers(HttpMethod.GET,"/css/**").permitAll()
        .antMatchers(HttpMethod.GET, "/uifiles/**").permitAll()
        .antMatchers(HttpMethod.GET, "/error/**").permitAll()
        .antMatchers(HttpMethod.GET,"/files/**").permitAll()
        .antMatchers(HttpMethod.GET,"/files/fonts/**").permitAll()
        .antMatchers(HttpMethod.GET,"/files/images/**").permitAll()
        .and()
        .authorizeRequests().antMatchers("/login").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .successForwardUrl("/home")
        .defaultSuccessUrl("/home")
        .permitAll()
        .and()
        .logout()
        .invalidateHttpSession(true)
        .clearAuthentication(true)
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .logoutSuccessUrl("/login")
        .and()
        .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());  

    }

When I run the application (from Spring Tool Suit) and access the application via url

http://localhost:8080/myApp/login

it works fine and opens the login page

But, when I enter

http://localhost:8080/myApp/

it redirects me to http://localhost:8080/login (This gives a 404 page)

I want both http://localhost:8080/myApp/ & http://localhost:8080/myApp/login to open the login page

The login page is in located in the root of the project folder. Also, I have spring.mvc.view.suffix=.jsp in my application.properties. So the controller automatically adds the file extension for the requests.

Logout functionality works fine and it redirects to http://localhost:8080/myApp/login

  • Spring Boot 2.1.7
  • Tomcat 9.0.21
  • JDK 1.8
like image 738
Sangeet Menon Avatar asked Nov 06 '22 09:11

Sangeet Menon


1 Answers

Total stab at this - I vaguely remember this being an absolute redirect from years ago.

Try "${server.servlet.context-path}/login" as the value for loginPage.

If it works - great, if not I'll try and dig deeper into this and see what I can find.

like image 56
Dave G Avatar answered Nov 11 '22 06:11

Dave G