mirror of https://gitee.com/maxjhandsome/pig
Resolver Cache处理
This commit is contained in:
parent
178f85be9a
commit
69f20d3d8d
|
@ -44,7 +44,6 @@
|
|||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<!--<version>2.7.4</version>-->
|
||||
</dependency>
|
||||
<!--myabtis-plus 代码生成依赖-->
|
||||
<dependency>
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
|
||||
/**
|
||||
|
@ -16,6 +17,7 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.E
|
|||
@EnableResourceServer
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@ComponentScan(basePackages = {"com.github.pig.auth", "com.github.pig.common.bean"})
|
||||
public class PigAuthServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package com.github.pig.auth.controller;
|
||||
|
||||
import com.github.pig.common.constant.SecurityConstants;
|
||||
import com.github.pig.common.util.R;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -32,6 +35,7 @@ public class UserController {
|
|||
* @return true/false
|
||||
*/
|
||||
@PostMapping("/removeToken")
|
||||
@CacheEvict(value = SecurityConstants.TOKEN_USER_DETAIL, key = "#accesstoken")
|
||||
public R<Boolean> removeToken(String accesstoken, String refreshToken) {
|
||||
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
|
||||
tokenStore.removeRefreshToken(refreshToken);
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.github.pig.common.bean.aop;
|
||||
|
||||
import com.github.pig.common.constant.SecurityConstants;
|
||||
import com.github.pig.common.util.R;
|
||||
import com.github.pig.common.util.UserUtils;
|
||||
import com.github.pig.common.util.exception.CheckException;
|
||||
import com.github.pig.common.util.exception.UnloginException;
|
||||
import com.github.pig.common.vo.UserVo;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
|
@ -11,12 +13,15 @@ import org.aspectj.lang.annotation.Aspect;
|
|||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
|
@ -28,6 +33,8 @@ import java.util.Arrays;
|
|||
@Component
|
||||
public class ControllerAop {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ControllerAop.class);
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Pointcut("execution(public com.github.pig.common.util.R *(..))")
|
||||
public void pointCutR() {
|
||||
|
@ -65,10 +72,23 @@ public class ControllerAop {
|
|||
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
String username = UserUtils.getUserName(request);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
|
||||
String token = UserUtils.getToken(request);
|
||||
UserVo userVo = null;
|
||||
if (StringUtils.isNotEmpty(token)) {
|
||||
userVo = cacheManager.getCache(SecurityConstants.TOKEN_USER_DETAIL).get(token, UserVo.class);
|
||||
}
|
||||
String username;
|
||||
if (userVo == null) {
|
||||
username = UserUtils.getUserName(request);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
UserUtils.setUser(username);
|
||||
}
|
||||
} else {
|
||||
username = userVo.getUsername();
|
||||
UserUtils.setUser(username);
|
||||
}
|
||||
logger.info("Controller AOP get username:{}", username);
|
||||
|
||||
logger.info("URL : " + request.getRequestURL().toString());
|
||||
logger.info("HTTP_METHOD : " + request.getMethod());
|
||||
|
@ -79,7 +99,7 @@ public class ControllerAop {
|
|||
Object result;
|
||||
|
||||
try {
|
||||
result = pjp.proceed();
|
||||
result = pjp.proceed();
|
||||
logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
|
||||
} catch (Throwable e) {
|
||||
result = handlerException(pjp, e);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.github.pig.common.bean.config;
|
||||
|
||||
import com.github.pig.common.bean.resolver.TokenArgumentResolver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
@ -14,8 +16,11 @@ import java.util.List;
|
|||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig extends WebMvcConfigurerAdapter {
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
||||
argumentResolvers.add(new TokenArgumentResolver());
|
||||
argumentResolvers.add(new TokenArgumentResolver(cacheManager));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.github.pig.common.bean.resolver;
|
||||
|
||||
import com.github.pig.common.constant.SecurityConstants;
|
||||
import com.github.pig.common.util.UserUtils;
|
||||
import com.github.pig.common.vo.SysRole;
|
||||
import com.github.pig.common.vo.UserVo;
|
||||
|
@ -7,6 +8,7 @@ import com.xiaoleilu.hutool.util.CollectionUtil;
|
|||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
|
@ -17,6 +19,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
|
@ -26,34 +29,70 @@ import java.util.List;
|
|||
@Configuration
|
||||
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
private CacheManager cacheManager;
|
||||
|
||||
public TokenArgumentResolver(CacheManager cacheManager) {
|
||||
this.cacheManager = cacheManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 入参筛选
|
||||
*
|
||||
* @param methodParameter 参数集合
|
||||
* @return 格式化后的参数
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
||||
return methodParameter.getParameterType().equals(UserVo.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. 先从 cache 中判断token 是否已经有缓存
|
||||
* 2. 不存在缓存情况,解析token 获取用户信息
|
||||
* 3. 不存在缓存情况,在AOP进行缓存添加,因为这里添加只会对入参含有 UserVo的生效,而不是全局
|
||||
*
|
||||
* @param methodParameter 入参集合
|
||||
* @param modelAndViewContainer model 和 view
|
||||
* @param nativeWebRequest web相关
|
||||
* @param webDataBinderFactory 入参解析
|
||||
* @return 包装对象
|
||||
* @throws Exception exception
|
||||
*/
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter methodParameter,
|
||||
ModelAndViewContainer modelAndViewContainer,
|
||||
NativeWebRequest nativeWebRequest,
|
||||
WebDataBinderFactory webDataBinderFactory) throws Exception {
|
||||
HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
|
||||
|
||||
String token = UserUtils.getToken(request);
|
||||
if (StringUtils.isBlank(token)) {
|
||||
logger.error("resolveArgument error token is empty");
|
||||
return null;
|
||||
}
|
||||
Optional<UserVo> optional = Optional.ofNullable(cacheManager.getCache(SecurityConstants.TOKEN_USER_DETAIL).get(token, UserVo.class));
|
||||
if (optional.isPresent()) {
|
||||
logger.info("return cache user vo,token :{}", token);
|
||||
return optional.get();
|
||||
}
|
||||
return optional.orElseGet(() -> generatorByToken(request, token));
|
||||
}
|
||||
|
||||
private UserVo generatorByToken(HttpServletRequest request, String token) {
|
||||
String username = UserUtils.getUserName(request);
|
||||
List<String> roles = UserUtils.getRole(request);
|
||||
logger.info("Auth-Token-User:{}-Roles:{}", username, roles);
|
||||
UserVo userVo = new UserVo();
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
userVo.setUsername(username);
|
||||
}
|
||||
if (CollectionUtil.isNotEmpty(roles)) {
|
||||
List<SysRole> sysRoleList = new ArrayList<>();
|
||||
for (String roleName : roles) {
|
||||
SysRole sysRole = new SysRole();
|
||||
sysRole.setRoleName(roleName);
|
||||
sysRoleList.add(sysRole);
|
||||
}
|
||||
userVo.setRoleList(sysRoleList);
|
||||
userVo.setUsername(username);
|
||||
List<SysRole> sysRoleList = new ArrayList<>();
|
||||
for (String roleName : roles) {
|
||||
SysRole sysRole = new SysRole();
|
||||
sysRole.setRoleName(roleName);
|
||||
sysRoleList.add(sysRole);
|
||||
}
|
||||
userVo.setRoleList(sysRoleList);
|
||||
cacheManager.getCache(SecurityConstants.TOKEN_USER_DETAIL).put(token, userVo);
|
||||
return userVo;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -65,4 +65,9 @@ public interface SecurityConstants {
|
|||
* 验证码文字大小
|
||||
*/
|
||||
String DEFAULT_IMAGE_FONT_SIZE = "30";
|
||||
|
||||
/**
|
||||
* token-uservo
|
||||
*/
|
||||
String TOKEN_USER_DETAIL = "token-user-detail";
|
||||
}
|
||||
|
|
|
@ -74,22 +74,32 @@ public class UserUtils {
|
|||
/**
|
||||
* 根据请求heard中的token获取用户角色
|
||||
*
|
||||
* @param httpServletRequest request
|
||||
* @return 角色名
|
||||
*/
|
||||
public static List<String> getRole(HttpServletRequest httpServletRequest) {
|
||||
String authorization = httpServletRequest.getHeader(CommonConstant.REQ_HEADER);
|
||||
String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);
|
||||
String token = getToken(httpServletRequest);
|
||||
String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
|
||||
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
|
||||
List<String> roleNames = (List<String>) claims.get("authorities");
|
||||
return roleNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求中token
|
||||
*
|
||||
* @param httpServletRequest request
|
||||
* @return token
|
||||
*/
|
||||
public static String getToken(HttpServletRequest httpServletRequest) {
|
||||
String authorization = httpServletRequest.getHeader(CommonConstant.REQ_HEADER);
|
||||
return StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户信息
|
||||
*
|
||||
* @param username
|
||||
* @param username 用户名
|
||||
*/
|
||||
public static void setUser(String username) {
|
||||
THREAD_LOCAL_USER.set(username);
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.github.pig.common.util;
|
||||
|
||||
import com.github.pig.common.constant.CommonConstant;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2017/12/22
|
||||
*/
|
||||
public class UserUtilsTest {
|
||||
@Test
|
||||
public void getToken() throws Exception {
|
||||
String authorization = null;
|
||||
System.out.println(StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optionalTest() {
|
||||
Optional<String> optional = Optional.ofNullable("");
|
||||
System.out.println(optional.isPresent());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue