im also face same issue in Login with google. anyone have solution? i follow same step
@GetMapping("/loginSuccess")
public String loginSuccess(@AuthenticationPrincipal OAuth2User oauthUser) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println("Authentication principal: " + authentication.getPrincipal());
if (oauthUser == null) {
return "Error: OAuth2User is null. Authentication failed.";
}
String username = oauthUser.getAttribute("name");
JwtToken jwtToken = jwtUtils.generateTokenFromUsername(username);
return "Login successful! JWT Token: " + jwtToken;
}
here my SecurityConfig
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> {
auth.requestMatchers(
antMatcher("/api/auth/**"),
antMatcher("/oauth2/**"),
antMatcher("/login/**")
).permitAll()
.anyRequest().authenticated();
})
.oauth2Login(oauth2 -> oauth2
.defaultSuccessUrl("/api/auth/loginSuccess")
.failureUrl("/api/auth/loginFailure")
)
.httpBasic()
.and()
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class)
.authenticationProvider(authenticationProvider());
return http.build();
}