TokenAop 优化

v1.4.1
Parker 4 years ago
parent ed39f38b0c
commit 43f28a8da4

@ -61,24 +61,31 @@ public class TokenAop {
@Around("requestMapping()")
public Object tokenAop(ProceedingJoinPoint point) throws Throwable {
// 将 Token放入 线程缓存
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if(sra != null) {
HttpServletRequest request = sra.getRequest();
String requestToken = UserTokenUtil.getRequestToken(request);
if(StringUtils.isNotEmpty(requestToken)){
// 放入当前线程缓存中
TokenThreadLocal.put(requestToken);
// Token
String requestToken = TokenThreadLocal.get();
// 如果 ThreadLocal为空 则去当前request中获取
if(StringUtils.isEmpty(requestToken)){
// 将 Token放入 线程缓存
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if(sra != null) {
HttpServletRequest request = sra.getRequest();
requestToken = UserTokenUtil.getRequestToken(request);
if(StringUtils.isNotEmpty(requestToken)){
// 放入当前线程缓存中
TokenThreadLocal.put(requestToken);
}
}
}catch (ServiceException e){
throw e;
}catch (Exception e){
log.error(e.getMessage(),e);
}
}catch (ServiceException e){
throw e;
}catch (Exception e){
log.error(e.getMessage(),e);
}
// 计时器
TimeInterval timer = DateUtil.timer();
// 执行
@ -96,8 +103,11 @@ public class TokenAop {
long timerCount = timer.interval();
//保存日志
LogUtil.saveLog(point, exception, timerCount);
// 线程销毁时 删除 token
TokenThreadLocal.remove();
if(StringUtils.isNotEmpty(requestToken)){
TokenThreadLocal.remove();
}
}
return returnValue;

@ -2,6 +2,7 @@ package org.opsli.core.listener;
import lombok.extern.slf4j.Slf4j;
import org.opsli.core.general.StartPrint;
import org.opsli.core.utils.OptionsUtil;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@ -19,6 +20,9 @@ public class ApplicationReadyEventListener implements ApplicationListener<Applic
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
event.getApplicationContext();
// 加载配置参数
OptionsUtil.loadAllOption();
// 输出启动日志
StartPrint.getInstance().successPrint();
}

@ -255,6 +255,47 @@ public class OptionsUtil {
return count == 0;
}
/**
*
* @return boolean
*/
public static boolean loadAllOption(){
try {
// 分布式加锁
if(!DistributedLockUtil.lock(PREFIX_CODE)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return false;
}
// 数据库查询数据
ResultVo<List<OptionsModel>> optionsApiAll = optionsApi.findAll();
if(optionsApiAll.isSuccess()){
// 处理数据
Map<String, OptionsModel> optionsModelMap = convertOptionsMap(optionsApiAll.getData());
if(CollUtil.isNotEmpty(optionsModelMap)){
// 保存至缓存
for (Map.Entry<String, OptionsModel> entry : optionsModelMap.entrySet()) {
String optionCode = entry.getKey();
OptionsModel model = entry.getValue();
CacheUtil.putHash(PREFIX_CODE, optionCode, model);
}
// 返回数据
return true;
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
return false;
}finally {
// 释放锁
DistributedLockUtil.unlock(PREFIX_CODE);
}
return false;
}
/**
*
* @param optionsMap List

@ -34,7 +34,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@ -75,6 +79,19 @@ public class UserUtil {
public static UserModel getUser(){
String token = TokenThreadLocal.get();
// 如果 token 为空 则尝试去 request 获取
if(StringUtils.isEmpty(token)){
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if (sra != null) {
HttpServletRequest request = sra.getRequest();
token = UserTokenUtil.getRequestToken(request);
}
}catch (Exception ignored){}
}
// 如果还是没获取到token 则抛出异常
if(StringUtils.isEmpty(token)){
// Token失效请重新登录
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_LOSE_EFFICACY);

Loading…
Cancel
Save