From e235b1e301bb9d3beb1a1f9e4d885e6fecbd8f93 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Fri, 13 Oct 2017 16:01:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../authorization/AuthenticationManager.java | 2 ++ ...nager.java => DefaultUserTokenManager.java} | 18 +++++++++++++----- .../authorization/token/SimpleUserToken.java | 2 +- .../AuthorizingHandlerAutoConfiguration.java | 4 ++-- .../feign/FeignAuthenticationManager.java | 4 ++-- .../src/main/resources/application.yml | 1 + .../src/main/resources/application.yml | 8 ++++---- .../authorization/simple/CacheConstants.java | 4 +++- 8 files changed, 28 insertions(+), 15 deletions(-) rename hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/{MemoryUserTokenManager.java => DefaultUserTokenManager.java} (92%) diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationManager.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationManager.java index 7d2f85f95..c77861678 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationManager.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationManager.java @@ -28,6 +28,8 @@ import java.util.Map; * @see 3.0 */ public interface AuthenticationManager { + String USER_AUTH_CACHE_NAME = "user-auth-"; + /** * 根据用户ID获取权限信息 * diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/MemoryUserTokenManager.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java similarity index 92% rename from hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/MemoryUserTokenManager.java rename to hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java index 8e7736c54..57e0ae2ce 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/MemoryUserTokenManager.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/DefaultUserTokenManager.java @@ -32,9 +32,17 @@ import java.util.stream.Collectors; * @author zhouhao * @since 3.0 */ -public class MemoryUserTokenManager implements UserTokenManager { +public class DefaultUserTokenManager implements UserTokenManager { - private final ConcurrentMap tokenUserStorage = new ConcurrentHashMap<>(256); + protected final ConcurrentMap tokenUserStorage; + + public DefaultUserTokenManager() { + this(new ConcurrentHashMap<>(256)); + } + + public DefaultUserTokenManager(ConcurrentMap storage) { + tokenUserStorage = storage; + } //令牌超时事件,默认3600秒 private long timeout = 3600; @@ -178,9 +186,9 @@ public class MemoryUserTokenManager implements UserTokenManager { @Override public void touch(String token) { - SimpleUserToken detail = tokenUserStorage.get(token); - if (null != detail) - detail.touch(); + SimpleUserToken userToken = tokenUserStorage.get(token); + if (null != userToken) + userToken.touch(); } } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/SimpleUserToken.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/SimpleUserToken.java index d38a56065..f5ea602fe 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/SimpleUserToken.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/SimpleUserToken.java @@ -98,7 +98,7 @@ public class SimpleUserToken implements UserToken { requestTimesCounter.set(requestTimes); } - void touch() { + public void touch() { requestTimesCounter.addAndGet(1); lastRequestTime = System.currentTimeMillis(); } diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/configuration/AuthorizingHandlerAutoConfiguration.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/configuration/AuthorizingHandlerAutoConfiguration.java index 7b3f5c656..95165a2f9 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/configuration/AuthorizingHandlerAutoConfiguration.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/configuration/AuthorizingHandlerAutoConfiguration.java @@ -8,7 +8,7 @@ import org.hswebframework.web.authorization.access.DataAccessHandler; import org.hswebframework.web.authorization.basic.handler.DefaultAuthorizingHandler; import org.hswebframework.web.authorization.basic.handler.access.DefaultDataAccessController; import org.hswebframework.web.authorization.basic.web.*; -import org.hswebframework.web.authorization.token.MemoryUserTokenManager; +import org.hswebframework.web.authorization.token.DefaultUserTokenManager; import org.hswebframework.web.authorization.token.UserTokenManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanPostProcessor; @@ -61,7 +61,7 @@ public class AuthorizingHandlerAutoConfiguration { @ConditionalOnMissingBean(UserTokenManager.class) @ConfigurationProperties(prefix = "hsweb.authorize") public UserTokenManager userTokenManager() { - return new MemoryUserTokenManager(); + return new DefaultUserTokenManager(); } @Bean diff --git a/hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/client/feign/FeignAuthenticationManager.java b/hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/client/feign/FeignAuthenticationManager.java index 7c7088547..ecd3b5256 100644 --- a/hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/client/feign/FeignAuthenticationManager.java +++ b/hsweb-authorization/hsweb-authorization-cloud/src/main/java/org/hswebframework/web/authorization/cloud/client/feign/FeignAuthenticationManager.java @@ -10,10 +10,10 @@ import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "${hsweb.cloud.user-center.name:user-center}") public interface FeignAuthenticationManager extends AuthenticationManager { @Override - @RequestMapping(value = "/user-auth/{userId}", method = RequestMethod.GET) + @RequestMapping(value = "${hsweb.cloud.user-center.prefix:/}user-auth/{userId}", method = RequestMethod.GET) Authentication getByUserId(@PathVariable("userId") String userId); @Override - @RequestMapping(value = "/user-auth", method = RequestMethod.PUT) + @RequestMapping(value = "${hsweb.cloud.user-center.prefix:/}user-auth", method = RequestMethod.PUT) Authentication sync(Authentication authentication); } diff --git a/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml b/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml index 0100902ff..87d497d0f 100644 --- a/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml +++ b/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-gateway/src/main/resources/application.yml @@ -18,6 +18,7 @@ zuul: sensitive-headers: Cookies host: connect-timeout-millis: 10000 + ribbon: eureka: enabled: true diff --git a/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml b/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml index b95d06b32..99a3b2375 100644 --- a/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml +++ b/hsweb-examples/hsweb-examples-cloud/hsweb-examples-cloud-service01/src/main/resources/application.yml @@ -11,10 +11,10 @@ hsweb: app: name: 权限管理测试 version: 3.0.0 -# cloud: -# user-center: -# name: gateway -# prefix: /api/user-center/ + cloud: + user-center: + name: gateway + prefix: /api/user-center/ server: port: 9001 logging: diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/CacheConstants.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/CacheConstants.java index a98ae09a3..dc6d3bb0c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/CacheConstants.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/CacheConstants.java @@ -18,6 +18,8 @@ package org.hswebframework.web.service.authorization.simple; +import org.hswebframework.web.authorization.AuthenticationManager; + /** * 缓存所需常量 * @@ -28,6 +30,6 @@ public interface CacheConstants { String USER_CACHE_NAME = "user-"; - String USER_AUTH_CACHE_NAME = "user-auth-"; + String USER_AUTH_CACHE_NAME = AuthenticationManager.USER_AUTH_CACHE_NAME; }