|
8 | 8 | import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
9 | 9 | import org.springframework.security.web.server.WebFilterExchange;
|
10 | 10 | import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
|
| 11 | +import org.springframework.util.LinkedMultiValueMap; |
| 12 | +import org.springframework.util.MultiValueMap; |
11 | 13 | import org.springframework.web.server.ServerWebExchange;
|
12 | 14 | import reactor.core.publisher.Mono;
|
13 | 15 |
|
|
23 | 25 | public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHandler {
|
24 | 26 | @Override
|
25 | 27 | public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
26 |
| - SysUser user = (SysUser)authentication.getPrincipal(); |
27 |
| - Long userId = user.getId(); |
28 |
| - String username = user.getUsername(); |
| 28 | + MultiValueMap<String, String> headerValues = new LinkedMultiValueMap(4); |
| 29 | + Object principal = authentication.getPrincipal(); |
| 30 | + //客户端模式只返回一个clientId |
| 31 | + if (principal instanceof SysUser) { |
| 32 | + SysUser user = (SysUser)authentication.getPrincipal(); |
| 33 | + headerValues.add(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId())); |
| 34 | + headerValues.add(SecurityConstants.USER_HEADER, user.getUsername()); |
| 35 | + } |
29 | 36 | OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
|
30 | 37 | String clientId = oauth2Authentication.getOAuth2Request().getClientId();
|
| 38 | + headerValues.add(SecurityConstants.TENANT_HEADER, clientId); |
| 39 | + headerValues.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); |
31 | 40 |
|
32 | 41 | ServerWebExchange exchange = webFilterExchange.getExchange();
|
33 | 42 | ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
34 | 43 | .headers(h -> {
|
35 |
| - h.add(SecurityConstants.USER_ID_HEADER, String.valueOf(userId)); |
36 |
| - h.add(SecurityConstants.USER_HEADER, username); |
37 |
| - h.add(SecurityConstants.TENANT_HEADER, clientId); |
38 |
| - h.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); |
| 44 | + h.addAll(headerValues); |
39 | 45 | })
|
40 | 46 | .build();
|
41 | 47 |
|
|
0 commit comments