44import jakarta .servlet .http .HttpServletRequest ;
55import jakarta .servlet .http .HttpServletResponse ;
66import java .io .IOException ;
7+ import lombok .RequiredArgsConstructor ;
78import lombok .extern .slf4j .Slf4j ;
9+ import org .acme .authorization_server .domain .service .SecurityService ;
810import org .acme .authorization_server .domain .service .UserService ;
9- import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1011import org .springframework .security .core .Authentication ;
1112import org .springframework .security .core .context .SecurityContextHolder ;
12- import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
1313import org .springframework .security .oauth2 .core .user .OAuth2User ;
1414import org .springframework .security .web .authentication .SavedRequestAwareAuthenticationSuccessHandler ;
1515import org .springframework .stereotype .Component ;
1818
1919@ Slf4j
2020@ Component
21+ @ RequiredArgsConstructor
2122public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
2223
2324 private final UserService userService ;
24-
25- public LoginSuccessHandler (final UserService userService ) {
26- super ();
27- this .userService = userService ;
28- }
25+ private final SecurityService securityService ;
2926
3027 @ Override
3128 public void onAuthenticationSuccess (final HttpServletRequest request ,
3229 final HttpServletResponse response ,
3330 final Authentication authentication )
3431 throws ServletException , IOException {
32+ final String userName = ((OAuth2User ) authentication .getPrincipal ()).getAttribute ("email" );
3533
36- if (userService .existsByEmail (authentication . getName () )) {
37- final var applicationAuthentication = setApplicationAuthentication ( authentication );
38- super .onAuthenticationSuccess (request , response , applicationAuthentication );
34+ if (userService .existsByEmail (userName )) {
35+ securityService . setAuthenticationContext ( userName );
36+ super .onAuthenticationSuccess (request , response , authentication );
3937 } else {
4038 userNotFoundInApplication (request , response );
4139 }
@@ -52,14 +50,4 @@ private void userNotFoundInApplication(final HttpServletRequest request,
5250 + "Before making login with identity providers, its necessary "
5351 + "to create an User in this application, with the same email as username" );
5452 }
55-
56- private Authentication setApplicationAuthentication (final Authentication authentication ) {
57- final var principal = (OAuth2AuthenticationToken ) authentication .getPrincipal ();
58- final OAuth2User oAuth2User = principal .getPrincipal ();
59- final String username = oAuth2User .getAttribute ("email" );
60- final var applicationAuthentication =
61- new UsernamePasswordAuthenticationToken (username , null );
62- SecurityContextHolder .getContext ().setAuthentication (applicationAuthentication );
63- return applicationAuthentication ;
64- }
6553}
0 commit comments