Commit 21f1112c authored by yanzg's avatar yanzg

添加对Android的支持

parent 066e7d74
...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop; ...@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.CloudContans; import com.yanzuoguang.cloud.CloudContans;
import com.yanzuoguang.cloud.helper.CookiesHelper; import com.yanzuoguang.cloud.helper.CookiesHelper;
import com.yanzuoguang.cloud.service.TokenService;
import com.yanzuoguang.util.base.ObjectHelper; import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.contants.ResultConstants; import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.exception.ExceptionHelper;
...@@ -18,6 +19,7 @@ import org.aspectj.lang.annotation.Pointcut; ...@@ -18,6 +19,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
...@@ -37,6 +39,9 @@ public class WebAspect extends BaseRequestAspect { ...@@ -37,6 +39,9 @@ public class WebAspect extends BaseRequestAspect {
private static final Logger logger = LoggerFactory.getLogger(BaseRequestAspect.class); private static final Logger logger = LoggerFactory.getLogger(BaseRequestAspect.class);
@Autowired
private TokenService tokenService;
/** /**
* exec aop point aspect * exec aop point aspect
*/ */
...@@ -53,13 +58,13 @@ public class WebAspect extends BaseRequestAspect { ...@@ -53,13 +58,13 @@ public class WebAspect extends BaseRequestAspect {
@Around(value = "webAspect()") @Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable { public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Log.threadBegin(); Log.threadBegin();
CookiesHelper.tokenInit();
// 用户数据库记录 // 用户数据库记录
ResponseResult responseResult = null; ResponseResult responseResult = null;
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
String name = joinPoint.getSignature().getName(); String name = joinPoint.getSignature().getName();
try { try {
logger.info("[ {} ] request params is {}", name, joinPoint.getArgs()); logger.info("[ {} ] request params is {}", name, joinPoint.getArgs());
tokenService.tokenInit();
Object result = executeMethod(joinPoint, name); Object result = executeMethod(joinPoint, name);
if (result instanceof ResponseResult) { if (result instanceof ResponseResult) {
...@@ -82,7 +87,7 @@ public class WebAspect extends BaseRequestAspect { ...@@ -82,7 +87,7 @@ public class WebAspect extends BaseRequestAspect {
throw e; throw e;
} }
} finally { } finally {
CookiesHelper.tokenFinish(); tokenService.tokenFinish();
Log.threadCommit(); Log.threadCommit();
saveInterLogs(joinPoint, responseResult); saveInterLogs(joinPoint, responseResult);
} }
......
package com.yanzuoguang.cloud.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
* Token服务
*
* @author 颜佐光
*/
@Component
public class TokenService {
@Autowired
private ApplicationContext context;
public void tokenInit() {
}
public void tokenFinish() {
}
}
...@@ -13,11 +13,36 @@ import com.yanzuoguang.util.vo.MapRow; ...@@ -13,11 +13,36 @@ import com.yanzuoguang.util.vo.MapRow;
* @author 颜佐光 * @author 颜佐光
*/ */
public class TokenHelper { public class TokenHelper {
/**
* 动态加载token支持对象
*/
private static TokenLoad tokenLoad = null;
/**
* 获取动态加载支持对象
*
* @return 动态加载支持对象
*/
public static TokenLoad getTokenLoad() {
return tokenLoad;
}
/**
* 设置动态加载支持对象
*
* @param tokenLoad 设置值
*/
public static void setTokenLoad(TokenLoad tokenLoad) {
TokenHelper.tokenLoad = tokenLoad;
}
/** /**
* 内存缓存 * 内存缓存
*/ */
protected static MemoryCache<TokenData> cache = new MemoryCache<>(); protected static MemoryCache<TokenData> cache = new MemoryCache<>();
/** /**
* 获取当前线程编号 * 获取当前线程编号
* *
...@@ -49,12 +74,25 @@ public class TokenHelper { ...@@ -49,12 +74,25 @@ public class TokenHelper {
public static <T extends Object> T get(boolean checkFlag, Class<T> cls) { public static <T extends Object> T get(boolean checkFlag, Class<T> cls) {
String id = getCurrentId(); String id = getCurrentId();
TokenData tokenData = cache.get(id); TokenData tokenData = cache.get(id);
if (tokenData == null || tokenData.getData() == null) { if (tokenData == null) {
if (checkFlag) {
throw new CodeException("请先登录");
}
return null;
}
if (!StringHelper.isEmpty(tokenData.getToken())) {
tokenData = tokenLoad.load(tokenData.getToken());
if (tokenData != null) {
cache.put(id, tokenData);
}
}
if (tokenData.getData() == null) {
if (checkFlag) { if (checkFlag) {
throw new CodeException("请先登录"); throw new CodeException("请先登录");
} }
return null; return null;
} }
if (ObjectHelper.isSub(cls, tokenData.getData().getClass())) { if (ObjectHelper.isSub(cls, tokenData.getData().getClass())) {
return (T) tokenData.getData(); return (T) tokenData.getData();
} else { } else {
......
package com.yanzuoguang.token;
import com.yanzuoguang.util.vo.MapRow;
/**
* 动态加载Token
*
* @author 动态加载Token
*/
public interface TokenLoad {
/**
* 动态加载token
*
* @param token  加载标记
* @return 加载内容
*/
TokenData load(String token);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment