From 578dba07c91822d6195bb7e1085c614536bcf007 Mon Sep 17 00:00:00 2001 From: zlt Date: Mon, 5 Aug 2019 22:50:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BAjwtToken=E9=A2=9D=E5=A4=96?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7id=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8C@LoginUser=E6=B3=A8=E8=A7=A3=E8=83=BD=E8=8E=B7?= =?UTF-8?q?=E5=8F=96(id=E3=80=81username=E5=92=8Croles)=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomUserAuthenticationConverter.java | 95 +++++++++++++++++++ .../common/store/AuthJwtTokenStore.java | 19 +++- .../oauth2/common/store/ResJwtTokenStore.java | 4 + .../common/constant/SecurityConstants.java | 5 + .../resolver/TokenArgumentResolver.java | 2 + .../filter/pre/UserInfoHeaderFilter.java | 6 +- 6 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/converter/CustomUserAuthenticationConverter.java diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/converter/CustomUserAuthenticationConverter.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/converter/CustomUserAuthenticationConverter.java new file mode 100644 index 00000000..7bf24315 --- /dev/null +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/converter/CustomUserAuthenticationConverter.java @@ -0,0 +1,95 @@ +package com.central.oauth2.common.converter; + +import com.central.common.model.SysUser; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter; +import org.springframework.util.StringUtils; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 优化自org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter + * jwt返回的principal改为返回SysUser,增加扩展字段 + * + * @author zlt + * @date 2019/8/5 + */ +public class CustomUserAuthenticationConverter implements UserAuthenticationConverter { + private Collection defaultAuthorities; + + private UserDetailsService userDetailsService; + + /** + * Optional {@link UserDetailsService} to use when extracting an {@link Authentication} from the incoming map. + * + * @param userDetailsService the userDetailsService to set + */ + public void setUserDetailsService(UserDetailsService userDetailsService) { + this.userDetailsService = userDetailsService; + } + + /** + * Default value for authorities if an Authentication is being created and the input has no data for authorities. + * Note that unless this property is set, the default Authentication created by {@link #extractAuthentication(Map)} + * will be unauthenticated. + * + * @param defaultAuthorities the defaultAuthorities to set. Default null. + */ + public void setDefaultAuthorities(String[] defaultAuthorities) { + this.defaultAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils + .arrayToCommaDelimitedString(defaultAuthorities)); + } + + @Override + public Map convertUserAuthentication(Authentication authentication) { + Map response = new LinkedHashMap(); + response.put(USERNAME, authentication.getName()); + if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) { + response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities())); + } + return response; + } + + @Override + public Authentication extractAuthentication(Map map) { + if (map.containsKey(USERNAME)) { + Object principal = map.get(USERNAME); + Collection authorities = getAuthorities(map); + if (userDetailsService != null) { + UserDetails user = userDetailsService.loadUserByUsername((String) map.get(USERNAME)); + authorities = user.getAuthorities(); + principal = user; + } else { + Integer id = (Integer)map.get("id"); + SysUser user = new SysUser(); + user.setUsername((String)principal); + user.setId(Long.valueOf(id)); + principal = user; + } + return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities); + } + return null; + } + + private Collection getAuthorities(Map map) { + if (!map.containsKey(AUTHORITIES)) { + return defaultAuthorities; + } + Object authorities = map.get(AUTHORITIES); + if (authorities instanceof String) { + return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities); + } + if (authorities instanceof Collection) { + return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils + .collectionToCommaDelimitedString((Collection) authorities)); + } + throw new IllegalArgumentException("Authorities must be either a String or a Collection"); + } +} diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java index d778ca3d..0892a3da 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthJwtTokenStore.java @@ -1,7 +1,12 @@ package com.central.oauth2.common.store; +import com.central.common.model.SysUser; +import com.central.oauth2.common.converter.CustomUserAuthenticationConverter; import org.springframework.cloud.bootstrap.encrypt.KeyProperties; import org.springframework.context.annotation.Bean; +import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; +import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; @@ -9,6 +14,8 @@ import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFacto import javax.annotation.Resource; import java.security.KeyPair; +import java.util.HashMap; +import java.util.Map; /** * 认证服务器使用 JWT RSA 非对称加密令牌 @@ -38,6 +45,8 @@ public class AuthJwtTokenStore { (keyProperties.getKeyStore().getLocation(), keyProperties.getKeyStore().getSecret().toCharArray()) .getKeyPair(keyProperties.getKeyStore().getAlias()); converter.setKeyPair(keyPair); + DefaultAccessTokenConverter tokenConverter = (DefaultAccessTokenConverter)converter.getAccessTokenConverter(); + tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter()); return converter; } @@ -47,12 +56,18 @@ public class AuthJwtTokenStore { * * @return TokenEnhancer */ - /*@Bean + @Bean public TokenEnhancer tokenEnhancer() { return (accessToken, authentication) -> { final Map additionalInfo = new HashMap<>(1); + Object principal = authentication.getPrincipal(); + //增加id参数 + if (principal instanceof SysUser) { + SysUser user = (SysUser)principal; + additionalInfo.put("id", user.getId()); + } ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; }; - }*/ + } } diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java index 701f8fb8..39efc124 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/ResJwtTokenStore.java @@ -2,6 +2,7 @@ package com.central.oauth2.common.store; import cn.hutool.core.util.StrUtil; import com.central.common.constant.SecurityConstants; +import com.central.oauth2.common.converter.CustomUserAuthenticationConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; import org.springframework.context.annotation.Bean; @@ -10,6 +11,7 @@ import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; @@ -41,6 +43,8 @@ public class ResJwtTokenStore { public JwtAccessTokenConverter jwtAccessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setVerifierKey(getPubKey()); + DefaultAccessTokenConverter tokenConverter = (DefaultAccessTokenConverter)converter.getAccessTokenConverter(); + tokenConverter.setUserTokenConverter(new CustomUserAuthenticationConverter()); return converter; } diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java index 403075d4..0072234f 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/SecurityConstants.java @@ -16,6 +16,11 @@ public interface SecurityConstants { */ String USER_HEADER = "x-user-header"; + /** + * 用户id信息头 + */ + String USER_ID_HEADER = "x-userId-header"; + /** * 角色信息头 */ diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/TokenArgumentResolver.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/TokenArgumentResolver.java index b199e55a..acd59e2e 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/TokenArgumentResolver.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/TokenArgumentResolver.java @@ -58,6 +58,7 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver { LoginUser loginUser = methodParameter.getParameterAnnotation(LoginUser.class); boolean isFull = loginUser.isFull(); HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class); + String userId = request.getHeader(SecurityConstants.USER_ID_HEADER); String username = request.getHeader(SecurityConstants.USER_HEADER); String roles = request.getHeader(SecurityConstants.ROLE_HEADER); if (StrUtil.isBlank(username)) { @@ -69,6 +70,7 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver { user = userService.selectByUsername(username); } else { user = new SysUser(); + user.setId(Long.valueOf(userId)); user.setUsername(username); } List sysRoleList = new ArrayList<>(); diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java index 9240b293..a121487d 100644 --- a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java +++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/UserInfoHeaderFilter.java @@ -2,6 +2,7 @@ package com.central.gateway.filter.pre; import cn.hutool.core.collection.CollectionUtil; import com.central.common.constant.SecurityConstants; +import com.central.common.model.SysUser; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; @@ -40,12 +41,15 @@ public class UserInfoHeaderFilter extends ZuulFilter { public Object run() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) { - String username = authentication.getName(); + SysUser user = (SysUser)authentication.getPrincipal(); + Long userId = user.getId(); + String username = user.getUsername(); OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; 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.CLIENT_HEADER, clientId); ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));