Created using thymeleaf and prviously it working fine but currently not Spring security ignoring my GoogleAuthSuccessHandler I debug it method is not callled by SecurityConfig
This is GoogleOAuth2SuccessHandler class `
@Autowired
RoleRepository roleRepository;
@Autowired
UserRepository userRepository;
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
OAuth2AuthenticationToken token =(OAuth2AuthenticationToken) authentication;
String email = token.getPrincipal().getAttributes().get("email").toString();
if(userRepository.findUserByEmail(email).isPresent()) {
}
else {
User user = new User();
user.setFirstName(token.getPrincipal().getAttributes().get("given_name").toString());
user.setLastName("user");
user.setEmail(email);
List<Role> roles = new ArrayList<>();
roles.add(roleRepository.findById(2).get());
user.setRoles(roles);
userRepository.save(user);
}
redirectStrategy.sendRedirect(request, response, "/");
`
This is Security Config Class Via Extending WebSecurityConfigurerAdapter Spring 2.7.0
`
@Autowired
GoogleOAuth2SuccessHandler googleOAuth2SuccessHandler;
@Autowired
CustomUserDetailService customUserDetailService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/","/shop/**","/register/**","/forgotPassword/**").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.antMatchers("/working/**").hasAuthority("WORKER")
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.failureUrl("/login?error=true")
.defaultSuccessUrl("/")
.usernameParameter("email")
.passwordParameter("password")
.and()
.oauth2Login()
.loginPage("/login")
.successHandler(this.googleOAuth2SuccessHandler)
.and()
.logout()
.logoutSuccessUrl("/login")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.and()
.exceptionHandling()
.and()
.csrf()
.disable();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailService);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**","/static/**","/images/**","/productImages/**","/css/**","/js/**");
}
`
I want to resolve my problem couple of month ago it working finely.
Hatib Shaikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.