|
1 | 1 | package com.park.utmstack.security.saml; |
2 | 2 |
|
| 3 | +import com.park.utmstack.domain.User; |
3 | 4 | import com.park.utmstack.repository.UserRepository; |
4 | 5 | import com.park.utmstack.security.jwt.TokenProvider; |
5 | 6 | import lombok.RequiredArgsConstructor; |
|
21 | 22 | import java.net.URI; |
22 | 23 | import java.util.Collection; |
23 | 24 | import java.util.Objects; |
| 25 | +import java.util.Optional; |
24 | 26 |
|
25 | 27 | import static com.park.utmstack.config.Constants.FRONT_BASE_URL; |
26 | 28 |
|
@@ -50,33 +52,25 @@ public void onAuthenticationSuccess(HttpServletRequest request, |
50 | 52 | String frontBaseUrl = scheme + "://" + host; |
51 | 53 |
|
52 | 54 | Saml2AuthenticatedPrincipal samlUser = (Saml2AuthenticatedPrincipal) authentication.getPrincipal(); |
53 | | - var roles = samlUser.getAttribute("roles"); |
54 | | - |
55 | 55 | String username = samlUser.getName(); |
56 | 56 |
|
57 | | - if (roles == null || ((Collection<?>) roles).isEmpty() || userRepository.findOneByLogin(username).isEmpty()) { |
58 | | - log.error("{}: Attempted SAML2 login with invalid roles or non-existing user account.", username); |
59 | | - failureHandler.onAuthenticationFailure(request, response, |
60 | | - new BadCredentialsException("The provided credentials do not match any active user account or the account lacks required roles.")); |
61 | | - return; |
62 | | - } |
| 57 | + User user = userRepository.findOneByLogin(username) |
| 58 | + .orElseThrow(() -> new BadCredentialsException("The provided credentials do not match any active user account.")); |
63 | 59 |
|
64 | | - Collection<? extends GrantedAuthority> authorities = Objects.requireNonNull(samlUser.getAttribute("roles")) |
| 60 | + Collection<? extends GrantedAuthority> authorities = Objects.requireNonNull(user.getAuthorities()) |
65 | 61 | .stream() |
66 | 62 | .map(Objects::toString) |
67 | 63 | .filter(r -> r.startsWith("ROLE_")) |
68 | 64 | .map(SimpleGrantedAuthority::new) |
69 | 65 | .toList(); |
70 | 66 |
|
71 | 67 | UsernamePasswordAuthenticationToken auth = |
72 | | - new UsernamePasswordAuthenticationToken(username, null, authorities); |
| 68 | + new UsernamePasswordAuthenticationToken((Object) username, null, authorities); |
73 | 69 |
|
74 | 70 | SecurityContextHolder.getContext().setAuthentication(auth); |
75 | 71 |
|
76 | | - // Generate JWT |
77 | 72 | String token = tokenProvider.createToken(auth, false, true); |
78 | 73 |
|
79 | | - // Redirect to frontend with token |
80 | 74 | URI redirectUri = UriComponentsBuilder.fromUriString(frontBaseUrl) |
81 | 75 | .path("/") |
82 | 76 | .queryParam("token", token) |
|
0 commit comments