提交 f05bb874 编写于 作者: zlt2000's avatar zlt2000

优化网关认证信息传递逻辑,适配oauth2的客户端模式

上级 0ed68b9f
...@@ -8,6 +8,8 @@ import org.springframework.security.core.Authentication; ...@@ -8,6 +8,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.web.server.WebFilterExchange; import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler; import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -23,19 +25,23 @@ import reactor.core.publisher.Mono; ...@@ -23,19 +25,23 @@ import reactor.core.publisher.Mono;
public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHandler { public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHandler {
@Override @Override
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) { public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
SysUser user = (SysUser)authentication.getPrincipal(); MultiValueMap<String, String> headerValues = new LinkedMultiValueMap(4);
Long userId = user.getId(); Object principal = authentication.getPrincipal();
String username = user.getUsername(); //客户端模式只返回一个clientId
if (principal instanceof SysUser) {
SysUser user = (SysUser)authentication.getPrincipal();
headerValues.add(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId()));
headerValues.add(SecurityConstants.USER_HEADER, user.getUsername());
}
OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
String clientId = oauth2Authentication.getOAuth2Request().getClientId(); String clientId = oauth2Authentication.getOAuth2Request().getClientId();
headerValues.add(SecurityConstants.TENANT_HEADER, clientId);
headerValues.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
ServerWebExchange exchange = webFilterExchange.getExchange(); ServerWebExchange exchange = webFilterExchange.getExchange();
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate() ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
.headers(h -> { .headers(h -> {
h.add(SecurityConstants.USER_ID_HEADER, String.valueOf(userId)); h.addAll(headerValues);
h.add(SecurityConstants.USER_HEADER, username);
h.add(SecurityConstants.TENANT_HEADER, clientId);
h.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
}) })
.build(); .build();
......
...@@ -41,16 +41,16 @@ public class UserInfoHeaderFilter extends ZuulFilter { ...@@ -41,16 +41,16 @@ public class UserInfoHeaderFilter extends ZuulFilter {
public Object run() { public Object run() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) { if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
SysUser user = (SysUser)authentication.getPrincipal(); Object principal = authentication.getPrincipal();
Long userId = user.getId(); RequestContext ctx = RequestContext.getCurrentContext();
String username = user.getUsername(); //客户端模式只返回一个clientId
if (principal instanceof SysUser) {
SysUser user = (SysUser)authentication.getPrincipal();
ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId()));
ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, user.getUsername());
}
OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
String clientId = oauth2Authentication.getOAuth2Request().getClientId(); String clientId = oauth2Authentication.getOAuth2Request().getClientId();
RequestContext ctx = RequestContext.getCurrentContext();
ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(userId));
ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, username);
ctx.addZuulRequestHeader(SecurityConstants.TENANT_HEADER, clientId); ctx.addZuulRequestHeader(SecurityConstants.TENANT_HEADER, clientId);
ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册