简体   繁体   中英

Spring defaults to my home.jsp rather than going to my login.jsp for authentication

When I launch my webapp I want spring to redirect to my login.jsp in order to authenticate before it goes to my home.jsp but when the app starts it immediately goes to my home.jsp. I created this SecurityFilterChain which I had thought would default to my login.jsp for authentication.

@Configuration
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/login", "/register").permitAll()
                .and()
                .formLogin()
                    .loginPage("/login").permitAll();
        return http.build();
    }
}

If more information is needed please let me know.

I found what was wrong. I was using filterChain rather than the securityFilterChain. I also modified the method a little bit as shown here

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((requests) -> requests
                        .antMatchers("/login", "/register").permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin((form) -> form
                        .loginPage("/login")
                        .loginProcessingUrl("/submit-login")
                        .permitAll()
                )
                .logout(LogoutConfigurer::permitAll);

        return http.build();
    }

Once making these changes the app defaulted to opening up my login.jsp for authentication before continuing on. Thank you all for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM