79165201

Date: 2024-11-07 06:16:05
Score: 2.5
Natty:
Report link

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();
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): also face same issue
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Ahamed