0

enter image description here

I am trying to implement social login along with form login in spring security 5. Google oauth is working fine but form login is throwing error below is my security config file:

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CustomOAuth2UserService oauth2UserService;

    @Bean
    public DaoAuthenticationProvider authProvider() throws Exception {
        CustomAuthProvider authProvider = new CustomAuthProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
        return authProvider;
    }

    @Bean
    @Order(1)
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                // .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                 //.and()
                .authorizeHttpRequests()
                .mvcMatchers("/favicon.ico", "/signup", "/signup/**", "/assets", "/assets/**", "/cdn.jsdelivr.net",
                        "/cdn.jsdelivr.net/**", "/login", "/login/**", "/login/oauth2/code/google", "/oauth2",
                        "/oauth2/**")
                .permitAll().anyRequest().authenticated().and().formLogin(form -> form.loginPage("/login").permitAll())
                .authenticationManager(new ProviderManager(List.of(authProvider())))

                .logout().logoutUrl("/logout").deleteCookies("JSESSIONID").invalidateHttpSession(true);
        ;
        return http.build();

    }
    
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    SecurityFilterChain securityFilterChain2(HttpSecurity http) throws Exception {

        http.csrf()
        .disable()
                // .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                // .and()
                .authorizeHttpRequests()
                .mvcMatchers("/favicon.ico", "/signup", "/signup/**", "/assets", "/assets/**", "/cdn.jsdelivr.net",
                        "/cdn.jsdelivr.net/**", "/login", "/login/**", "/login/oauth2/code/google", "/oauth2",
                        "/oauth2/**")
                .permitAll().anyRequest().authenticated().and()
                .oauth2Login().loginPage("/login")
                .userInfoEndpoint().userService(oauth2UserService).and()
                .and()
                .logout().logoutSuccessHandler(new LogoutSuccessHandler() {
                    
                    @Override
                    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
                            throws IOException, ServletException {
                        SecurityContext context = SecurityContextHolder.getContext();
                        SecurityContextHolder.clearContext();
                        context.setAuthentication(null);
                        logger.info("onLogoutSuccess::::: {}");
                        
                    }
                }).logoutUrl("/logout").deleteCookies("JSESSIONID").invalidateHttpSession(true);
        ;
        return http.build();

    }
    
    
    



    @Bean
    public UserDetailsService userDetailsService() {
        return new CustomUserDetailService();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

}

Please let me know what is wrong with the configuration.

Feroz Siddiqui
  • 3,840
  • 6
  • 34
  • 69
  • `form.loginPage("/login")` here you are manually telling spring that YOU will supply a login page at this path. So are you doing that? – Toerktumlare Sep 19 '22 at 00:33
  • Note that your second filter chain `securityFilterChain` is never used because every request is handled by the first filter chain `securityFilterChain2`. You can see how to set up multiple filter chains in the [Spring Security reference documentation](https://docs.spring.io/spring-security/reference/servlet/configuration/java.html#_multiple_httpsecurity). – Eleftheria Stein-Kousathana Sep 19 '22 at 09:07

0 Answers0