From 2b613b2d740a0b490bc43f8f6f10744c80b97496 Mon Sep 17 00:00:00 2001 From: zlt Date: Fri, 12 Jul 2019 14:15:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Token=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=88=E4=BB=85=E9=99=90=E4=BA=8Eredis=20t?= =?UTF-8?q?oken=E6=A8=A1=E5=BC=8F=EF=BC=89=EF=BC=9A"=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86-token=E7=AE=A1=E7=90=86"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/store/AuthRedisTokenStore.java | 1 - .../common/store/CustomRedisTokenStore.java | 49 +++++------- .../common/annotation/LoginClient.java | 16 ++++ .../common/config/LoginArgResolverConfig.java | 4 + .../common/constant/SecurityConstants.java | 17 ++++ .../resolver/ClientArgumentResolver.java | 53 ++++++++++++ zlt-doc/sql/user-center.sql | 4 +- .../filter/pre/UserInfoHeaderFilter.java | 19 ++--- .../src/main/resources/application.yml | 4 +- .../oauth/controller/TokensController.java | 31 +++++++ .../java/com/central/oauth/model/TokenVo.java | 32 ++++++++ .../central/oauth/service/ITokensService.java | 18 +++++ .../service/impl/RedisTokensServiceImpl.java | 75 +++++++++++++++++ zlt-uaa/src/main/resources/application.yml | 2 +- .../src/main/resources/static/module/admin.js | 10 ++- .../src/main/resources/static/module/index.js | 2 +- .../resources/static/pages/system/tokens.html | 80 +++++++++++++++++++ 17 files changed, 366 insertions(+), 51 deletions(-) create mode 100644 zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/LoginClient.java create mode 100644 zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/service/ITokensService.java create mode 100644 zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java create mode 100644 zlt-web/back-web/src/main/resources/static/pages/system/tokens.html diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java index 12c4c773..ada4c027 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java @@ -4,7 +4,6 @@ import com.central.oauth2.common.properties.SecurityProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.security.oauth2.provider.ClientDetailsService; import org.springframework.security.oauth2.provider.token.TokenStore; /** diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java index 1567bbea..024e1834 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java @@ -34,7 +34,7 @@ import java.util.List; * 优化自Spring Security的RedisTokenStore * 1. 支持redis cluster模式 * 2. 非cluster模式时使用pipeline减少连接次数 - * 3. CLIENT_ID_TO_ACCESS集合改为list,方便业务顺序遍历 + * 3. CLIENT_ID_TO_ACCESS和UNAME_TO_ACCESS集合改为list,方便业务顺序遍历 * 4. 自动续签token(可配置是否开启) * * @author zlt @@ -43,13 +43,10 @@ import java.util.List; public class CustomRedisTokenStore implements TokenStore { private static final String ACCESS = "access:"; private static final String AUTH_TO_ACCESS = "auth_to_access:"; - private static final String AUTH = "auth:"; private static final String REFRESH_AUTH = "refresh_auth:"; private static final String ACCESS_TO_REFRESH = "access_to_refresh:"; private static final String REFRESH = "refresh:"; private static final String REFRESH_TO_ACCESS = "refresh_to_access:"; - private static final String CLIENT_ID_TO_ACCESS = "client_id_to_access:"; - private static final String UNAME_TO_ACCESS = "uname_to_access:"; /** * 续签时间比例,当前剩余时间小于小于过期总时长的50%则续签 */ @@ -231,7 +228,7 @@ public class CustomRedisTokenStore implements TokenStore { byte[] bytes; RedisConnection conn = getConnection(); try { - bytes = conn.get(serializeKey(AUTH + token)); + bytes = conn.get(serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token)); } finally { conn.close(); } @@ -266,17 +263,17 @@ public class CustomRedisTokenStore implements TokenStore { byte[] serializedAccessToken = serialize(token); byte[] serializedAuth = serialize(authentication); byte[] accessKey = serializeKey(ACCESS + token.getValue()); - byte[] authKey = serializeKey(AUTH + token.getValue()); + byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token.getValue()); byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication)); - byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication)); - byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId()); + byte[] approvalKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication)); + byte[] clientId = serializeKey(SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId()); RedisConnection conn = getConnection(); try { - byte[] oldAccessToken = null; - //续签的话需要查询旧的token,用于集合删除 - if (isRenew) { - oldAccessToken = conn.get(accessKey); + byte[] oldAccessToken = conn.get(accessKey); + //如果token已存在,并且不是续签的话直接返回 + if (!isRenew && oldAccessToken != null) { + return; } this.openPipeline(conn); @@ -296,12 +293,12 @@ public class CustomRedisTokenStore implements TokenStore { //如果是续签token,需要先删除集合里旧的值 if (oldAccessToken != null) { if (!authentication.isClientOnly()) { - conn.sRem(approvalKey, oldAccessToken); + conn.lRem(approvalKey, 1, oldAccessToken); } conn.lRem(clientId, 1, oldAccessToken); } if (!authentication.isClientOnly()) { - conn.sAdd(approvalKey, serializedAccessToken); + conn.rPush(approvalKey, serializedAccessToken); } conn.rPush(clientId, serializedAccessToken); if (token.getExpiration() != null) { @@ -367,7 +364,7 @@ public class CustomRedisTokenStore implements TokenStore { public void removeAccessToken(String tokenValue) { byte[] accessKey = serializeKey(ACCESS + tokenValue); - byte[] authKey = serializeKey(AUTH + tokenValue); + byte[] authKey = serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + tokenValue); byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + tokenValue); RedisConnection conn = getConnection(); try { @@ -384,11 +381,11 @@ public class CustomRedisTokenStore implements TokenStore { if (authentication != null) { String key = authenticationKeyGenerator.extractKey(authentication); byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + key); - byte[] unameKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication)); - byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId()); + byte[] unameKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(authentication)); + byte[] clientId = serializeKey(SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId()); this.openPipeline(conn); conn.del(authToAccessKey); - conn.sRem(unameKey, access); + conn.lRem(unameKey, 1, access); conn.lRem(clientId, 1, access); conn.del(serialize(ACCESS + key)); this.closePipeline(conn); @@ -499,30 +496,20 @@ public class CustomRedisTokenStore implements TokenStore { @Override public Collection findTokensByClientIdAndUserName(String clientId, String userName) { - byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(clientId, userName)); + byte[] approvalKey = serializeKey(SecurityConstants.REDIS_UNAME_TO_ACCESS + getApprovalKey(clientId, userName)); List byteList; RedisConnection conn = getConnection(); try { - byteList = getByteLists(approvalKey, conn); + byteList = conn.lRange(approvalKey, 0, -1); } finally { conn.close(); } return getTokenCollections(byteList); } - private List getByteLists(byte[] approvalKey, RedisConnection conn) { - List byteList; - Long size = conn.sCard(approvalKey); - byteList = new ArrayList<>(size.intValue()); - Cursor cursor = conn.sScan(approvalKey, ScanOptions.NONE); - while(cursor.hasNext()) { - byteList.add(cursor.next()); - } - return byteList; - } @Override public Collection findTokensByClientId(String clientId) { - byte[] key = serializeKey(CLIENT_ID_TO_ACCESS + clientId); + byte[] key = serializeKey(SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + clientId); List byteList; RedisConnection conn = getConnection(); try { diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/LoginClient.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/LoginClient.java new file mode 100644 index 00000000..d1fa6266 --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/annotation/LoginClient.java @@ -0,0 +1,16 @@ +package com.central.common.annotation; + +import java.lang.annotation.*; + +/** + * 请求的方法参数上添加该注解,则注入当前登录账号的应用id + * 例:public void test(@LoginClient String clientId) //注入webApp + * + * @author zlt + * @date 2018/7/24 16:44 + */ +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface LoginClient { +} diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/LoginArgResolverConfig.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/LoginArgResolverConfig.java index 79251172..5506c433 100644 --- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/LoginArgResolverConfig.java +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/LoginArgResolverConfig.java @@ -1,6 +1,7 @@ package com.central.common.config; import com.central.common.feign.UserService; +import com.central.common.resolver.ClientArgumentResolver; import com.central.common.resolver.TokenArgumentResolver; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; @@ -26,6 +27,9 @@ public class LoginArgResolverConfig implements WebMvcConfigurer { */ @Override public void addArgumentResolvers(List argumentResolvers) { + //注入用户信息 argumentResolvers.add(new TokenArgumentResolver(userService)); + //注入应用信息 + argumentResolvers.add(new ClientArgumentResolver()); } } 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 10db1691..f6ad2ba5 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 @@ -21,6 +21,11 @@ public interface SecurityConstants { */ String ROLE_HEADER = "x-role-header"; + /** + * 应用信息头 + */ + String CLIENT_HEADER = "x-client-header"; + /** * 基础角色 */ @@ -140,4 +145,16 @@ public interface SecurityConstants { * 默认token过期时间(1小时) */ Integer ACCESS_TOKEN_VALIDITY_SECONDS = 60 * 60; + /** + * redis中授权token对应的key + */ + String REDIS_TOKEN_AUTH = "auth:"; + /** + * redis中应用对应的token集合的key + */ + String REDIS_CLIENT_ID_TO_ACCESS = "client_id_to_access:"; + /** + * redis中用户名对应的token集合的key + */ + String REDIS_UNAME_TO_ACCESS = "uname_to_access:"; } diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java new file mode 100644 index 00000000..aeb21b0a --- /dev/null +++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/resolver/ClientArgumentResolver.java @@ -0,0 +1,53 @@ +package com.central.common.resolver; + +import cn.hutool.core.util.StrUtil; +import com.central.common.annotation.LoginClient; +import com.central.common.constant.SecurityConstants; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.MethodParameter; +import org.springframework.web.bind.support.WebDataBinderFactory; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.ModelAndViewContainer; + +import javax.servlet.http.HttpServletRequest; + +/** + * head中的应用参数注入clientId中 + * + * @author zlt + * @date 2019/7/10 + */ +@Slf4j +public class ClientArgumentResolver implements HandlerMethodArgumentResolver { + /** + * 入参筛选 + * + * @param methodParameter 参数集合 + * @return 格式化后的参数 + */ + @Override + public boolean supportsParameter(MethodParameter methodParameter) { + return methodParameter.hasParameterAnnotation(LoginClient.class) && methodParameter.getParameterType().equals(String.class); + } + + /** + * @param methodParameter 入参集合 + * @param modelAndViewContainer model 和 view + * @param nativeWebRequest web相关 + * @param webDataBinderFactory 入参解析 + * @return 包装对象 + */ + @Override + public Object resolveArgument(MethodParameter methodParameter, + ModelAndViewContainer modelAndViewContainer, + NativeWebRequest nativeWebRequest, + WebDataBinderFactory webDataBinderFactory) { + HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class); + String clientId = request.getHeader(SecurityConstants.CLIENT_HEADER); + if (StrUtil.isBlank(clientId)) { + log.warn("resolveArgument error clientId is empty"); + } + return clientId; + } +} diff --git a/zlt-doc/sql/user-center.sql b/zlt-doc/sql/user-center.sql index a3abf70c..9ed57792 100644 --- a/zlt-doc/sql/user-center.sql +++ b/zlt-doc/sql/user-center.sql @@ -121,7 +121,7 @@ INSERT INTO `sys_menu` VALUES (9, 37, '文件中心', '#!files', 'files/files.ht INSERT INTO `sys_menu` VALUES (10, 37, '文档中心', '#!swagger', 'http://127.0.0.1:9900/swagger-ui.html', NULL, 'layui-icon-app', 4, '2017-11-17 16:56:59', '2019-01-17 20:18:48', 1, 0); INSERT INTO `sys_menu` VALUES (11, 12, '我的信息', '#!myInfo', 'system/myInfo.html', NULL, '', 10, '2017-11-17 16:56:59', '2018-09-02 06:12:24', 1, 1); INSERT INTO `sys_menu` VALUES (12, -1, '认证管理', 'javascript:;', '', NULL, 'layui-icon-set', 1, '2017-11-17 16:56:59', '2018-12-13 15:02:49', 1, 0); -INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 9, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0); +INSERT INTO `sys_menu` VALUES (35, 12, '应用管理', '#!app', 'attestation/app.html', NULL, 'layui-icon-link', 5, '2017-11-17 16:56:59', '2019-01-14 15:35:15', 1, 0); INSERT INTO `sys_menu` VALUES (37, -1, '系统管理', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2018-08-25 10:41:58', '2019-01-23 14:01:58', 1, 0); INSERT INTO `sys_menu` VALUES (62, 63, '应用监控', '#!admin', 'http://127.0.0.1:6500/#/wallboard', NULL, 'layui-icon-chart-screen', 3, '2019-01-08 15:32:19', '2019-01-17 20:22:44', 1, 0); INSERT INTO `sys_menu` VALUES (63, -1, '系统监控', 'javascript:;', '', NULL, 'layui-icon-set', 2, '2019-01-10 18:35:05', '2019-01-10 18:35:05', 1, 0); @@ -135,6 +135,7 @@ INSERT INTO `sys_menu` VALUES (70, 63, 'APM监控', '#!apm', 'http://127.0.0.1:8 INSERT INTO `sys_menu` VALUES (71, -1, '搜索管理', 'javascript:;', '', NULL, 'layui-icon-set', 3, '2018-08-25 10:41:58', '2019-01-23 15:07:07', 1, 0); INSERT INTO `sys_menu` VALUES (72, 71, '索引管理', '#!index', 'search/index_manager.html', NULL, 'layui-icon-template', 1, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0); INSERT INTO `sys_menu` VALUES (73, 71, '用户搜索', '#!userSearch', 'search/user_search.html', NULL, 'layui-icon-user', 2, '2019-01-10 18:35:55', '2019-01-12 00:27:20', 1, 0); +INSERT INTO `sys_menu` VALUES (74, 12, 'token管理', '#!tokens', 'system/tokens.html', NULL, 'layui-icon-unlink', 6, '2019-07-11 16:56:59', '2019-07-11 16:56:59', 1, 0); -- ---------------------------- -- Table structure for sys_role_menu @@ -170,6 +171,7 @@ INSERT INTO `sys_role_menu` VALUES (1, 70); INSERT INTO `sys_role_menu` VALUES (1, 71); INSERT INTO `sys_role_menu` VALUES (1, 72); INSERT INTO `sys_role_menu` VALUES (1, 73); +INSERT INTO `sys_role_menu` VALUES (1, 74); INSERT INTO `sys_role_menu` VALUES (2, 2); INSERT INTO `sys_role_menu` VALUES (2, 3); INSERT INTO `sys_role_menu` VALUES (2, 4); 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 4b6bc707..9240b293 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,13 +2,13 @@ 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; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.stereotype.Component; import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORM_BODY_WRAPPER_FILTER_ORDER; @@ -40,17 +40,14 @@ public class UserInfoHeaderFilter extends ZuulFilter { public Object run() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) { - Object principal = authentication.getPrincipal(); - String userInfo; - if (principal instanceof SysUser) { - SysUser user = (SysUser) principal; - userInfo = user.getUsername(); - } else { - //jwt的token只有name - userInfo = authentication.getName(); - } + String username = authentication.getName(); + + OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; + String clientId = oauth2Authentication.getOAuth2Request().getClientId(); + RequestContext ctx = RequestContext.getCurrentContext(); - ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, userInfo); + ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, username); + ctx.addZuulRequestHeader(SecurityConstants.CLIENT_HEADER, clientId); ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); } return null; diff --git a/zlt-gateway/zuul-gateway/src/main/resources/application.yml b/zlt-gateway/zuul-gateway/src/main/resources/application.yml index 89af3d05..777367c3 100644 --- a/zlt-gateway/zuul-gateway/src/main/resources/application.yml +++ b/zlt-gateway/zuul-gateway/src/main/resources/application.yml @@ -42,7 +42,9 @@ zlt: menusPaths: /api-user/menus/current auth: # 配置必需认证的url - httpUrls: /api-uaa/clients/** + httpUrls: > + /api-uaa/clients/**, + /api-uaa/tokens/** #是否开启url级别权限 urlEnabled: false renew: diff --git a/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java b/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java new file mode 100644 index 00000000..258a4c59 --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/controller/TokensController.java @@ -0,0 +1,31 @@ +package com.central.oauth.controller; + +import com.central.common.annotation.LoginClient; +import com.central.common.model.PageResult; +import com.central.oauth.model.TokenVo; +import com.central.oauth.service.ITokensService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +/** + * token管理接口 + * + * @author zlt + */ +@Api(tags = "Token管理") +@RestController +@RequestMapping("/tokens") +public class TokensController { + @Autowired + private ITokensService tokensService; + + @GetMapping("/list") + @ApiOperation(value = "token列表") + public PageResult list(@RequestParam Map params, @LoginClient String clientId) { + return tokensService.listTokens(params, clientId); + } +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java b/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java new file mode 100644 index 00000000..bb1f2112 --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/model/TokenVo.java @@ -0,0 +1,32 @@ +package com.central.oauth.model; + +import lombok.Getter; +import lombok.Setter; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author zlt + */ +@Setter +@Getter +public class TokenVo implements Serializable { + private static final long serialVersionUID = -6656955957477645319L; + /** + * token的值 + */ + private String tokenValue; + /** + * 到期时间 + */ + private Date expiration; + /** + * 用户名 + */ + private String username; + /** + * 所属应用 + */ + private String clientId; +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/ITokensService.java b/zlt-uaa/src/main/java/com/central/oauth/service/ITokensService.java new file mode 100644 index 00000000..3ff7d974 --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/service/ITokensService.java @@ -0,0 +1,18 @@ +package com.central.oauth.service; + +import com.central.common.model.PageResult; +import com.central.oauth.model.TokenVo; + +import java.util.Map; + +/** + * @author zlt + */ +public interface ITokensService { + /** + * 查询token列表 + * @param params 请求参数 + * @param clientId 应用id + */ + PageResult listTokens(Map params, String clientId); +} diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java new file mode 100644 index 00000000..2bcad51f --- /dev/null +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/RedisTokensServiceImpl.java @@ -0,0 +1,75 @@ +package com.central.oauth.service.impl; + +import cn.hutool.core.util.PageUtil; +import cn.hutool.core.util.StrUtil; +import com.central.common.constant.SecurityConstants; +import com.central.common.model.PageResult; +import com.central.common.redis.template.RedisRepository; +import com.central.oauth.model.TokenVo; +import com.central.oauth.service.ITokensService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.MapUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * token管理服务(redis token) + * + * @author zlt + * @date 2019/7/12 + */ +@Slf4j +@Service +public class RedisTokensServiceImpl implements ITokensService { + @Autowired + private RedisRepository redisRepository; + + @Override + public PageResult listTokens(Map params, String clientId) { + Integer page = MapUtils.getInteger(params, "page"); + Integer limit = MapUtils.getInteger(params, "limit"); + int[] startEnds = PageUtil.transToStartEnd(page, limit); + //根据请求参数生成redis的key + String redisKey = getRedisKey(params, clientId); + long size = redisRepository.length(redisKey); + List result = new ArrayList<>(limit); + //查询token集合 + List tokenObjs = redisRepository.getList(redisKey, startEnds[0], startEnds[1]-1); + if (tokenObjs != null) { + for (Object obj : tokenObjs) { + DefaultOAuth2AccessToken accessToken = (DefaultOAuth2AccessToken)obj; + //获取用户信息 + Object authObj = redisRepository.get(SecurityConstants.REDIS_TOKEN_AUTH + accessToken.getValue()); + OAuth2Authentication authentication = (OAuth2Authentication)authObj; + //构造token对象 + TokenVo tokenVo = new TokenVo(); + tokenVo.setClientId(clientId); + tokenVo.setTokenValue(accessToken.getValue()); + tokenVo.setExpiration(accessToken.getExpiration()); + tokenVo.setUsername(authentication.getName()); + result.add(tokenVo); + } + } + return PageResult.builder().data(result).code(0).count(size).build(); + } + + /** + * 根据请求参数生成redis的key + */ + private String getRedisKey(Map params, String clientId) { + String result; + String username = MapUtils.getString(params, "username"); + if (StrUtil.isNotEmpty(username)) { + result = SecurityConstants.REDIS_UNAME_TO_ACCESS + clientId + ":" + username; + } else { + result = SecurityConstants.REDIS_CLIENT_ID_TO_ACCESS + clientId; + } + return result; + } +} diff --git a/zlt-uaa/src/main/resources/application.yml b/zlt-uaa/src/main/resources/application.yml index 4a962222..e158d708 100644 --- a/zlt-uaa/src/main/resources/application.yml +++ b/zlt-uaa/src/main/resources/application.yml @@ -29,8 +29,8 @@ zlt: # 忽略认证的地址 httpUrls: > /validata/**, - /clients, /clients/**, + /tokens/**, /login.html, /css/**, /images/**, diff --git a/zlt-web/back-web/src/main/resources/static/module/admin.js b/zlt-web/back-web/src/main/resources/static/module/admin.js index e6ba34e6..afeb16b9 100644 --- a/zlt-web/back-web/src/main/resources/static/module/admin.js +++ b/zlt-web/back-web/src/main/resources/static/module/admin.js @@ -84,7 +84,7 @@ layui.define(['config', 'layer'], function (exports) { return layer.open(param); }, // 封装ajax请求,返回数据类型为json - req: function (url, data, success, method) { + req: function (url, data, success, method, noHeaderToken) { if ('put' == method.toLowerCase()) { method = 'PUT'; } else if ('delete' == method.toLowerCase()) { @@ -99,9 +99,11 @@ layui.define(['config', 'layer'], function (exports) { contentType: "application/json", success: success, beforeSend: function (xhr) { - var token = config.getToken(); - if (token) { - xhr.setRequestHeader('Authorization', 'bearer ' + token.access_token); + if (!noHeaderToken) { + let token = config.getToken(); + if (token) { + xhr.setRequestHeader('Authorization', 'bearer ' + token.access_token); + } } } }); diff --git a/zlt-web/back-web/src/main/resources/static/module/index.js b/zlt-web/back-web/src/main/resources/static/module/index.js index 175d144b..1b632f63 100644 --- a/zlt-web/back-web/src/main/resources/static/module/index.js +++ b/zlt-web/back-web/src/main/resources/static/module/index.js @@ -217,7 +217,7 @@ layui.define(['config', 'admin', 'layer', 'laytpl', 'element', 'form'], function $('#btnLogout').click(function () { layer.confirm('确定退出登录?', function () { //通过认证中心 tuic - admin.req('api-uaa/oauth/remove/token?access_token='+config.getToken().access_token, {}, function (data) { + admin.req('api-uaa/oauth/remove/token', {}, function (data) { config.removeToken(); location.replace('login.html'); }, 'POST'); diff --git a/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html new file mode 100644 index 00000000..b02d855d --- /dev/null +++ b/zlt-web/back-web/src/main/resources/static/pages/system/tokens.html @@ -0,0 +1,80 @@ +
+
+

token管理

+ + 首页 + token管理 + +
+
+
+ 搜索:  + +
+ + +
+
+
+ + + + + \ No newline at end of file