Commit 8f3721d0 by fukai

提交访问计数器

parent c719ffce
......@@ -29,6 +29,8 @@ public class ErrorCodes {
public static final String UNKNOEW_TRANS = "R0017";
public static final String FORBIDDEN_TRANS = "R0018";
public static final String GT_MAX_CURR_NUM = "R9990";
public static final String INTERRUPTED_ERROR = "R9997";
public static final String REDIS_CONNECTION_ERROR = "R9998";
public static final String ERROR = "R9999";
......
......@@ -4,6 +4,8 @@ public class NoUiVersion {
private String version = "1.0.0";
private int curr_max_num= 3; //最大并发数
private String rootFilePath;
public String getRootFilePath() {
......@@ -21,4 +23,12 @@ public class NoUiVersion {
public void setVersion(String version) {
this.version = version;
}
public int getCurr_max_num() {
return curr_max_num;
}
public void setCurr_max_num(int curr_max_num) {
this.curr_max_num = curr_max_num;
}
}
package org.sss.presentation.noui.jwt;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
......@@ -22,10 +24,13 @@ import com.google.gson.Gson;
public class TokenInterceptor implements HandlerInterceptor {
private static Map<String, Integer> CounterMap = new ConcurrentHashMap<>(); //计数map
@Autowired
private NoUiVersion noUiVersion;
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception arg3) throws Exception {
addCount(NoUiUtils.getToken(),-1); //计算器减1
NoUiUtils.clearLoginInfo();
}
......@@ -58,7 +63,6 @@ public class TokenInterceptor implements HandlerInterceptor {
return true;
}
JwtLogin login = JWT.unsign(token, JwtLogin.class);
if (login == null || (!userId.equals((login.getUserId())))) {
Result rt = new Result(ErrorCodes.LOGIN_TOKEN_CHECKERROR, "用户token或ID验证不通过", null, noUiVersion.getVersion());
......@@ -86,7 +90,13 @@ public class TokenInterceptor implements HandlerInterceptor {
responseMessage(response, response.getWriter(), rt);
return false;
}
//超过最大并发数限制
if(compareAndSet(noUiRequest.getToken(),1,noUiVersion.getCurr_max_num()) < 0)
{
Result rt = new Result(ErrorCodes.GT_MAX_CURR_NUM, "超过单个会话并发数", null, noUiVersion.getVersion());
responseMessage(response, response.getWriter(), rt);
return false;
}
// 重新刷入登陆时间
RedisLoginInfo nweRedisLoginInfo = new RedisLoginInfo(userId, token, NumericUtil.sessionTimeOut(), redisLoginInfo.getSysmod(),noUiRequest.getTerminalType());
RedisUtil.set(Constants.SESSION + "." + userId + "." + terminalType, nweRedisLoginInfo);
......@@ -105,4 +115,38 @@ public class TokenInterceptor implements HandlerInterceptor {
out.close();
}
public static int compareAndSet(String token,int cnt,int max_curr_cnt)
{
synchronized (CounterMap) {
int count = getCount(token);
if(count >= max_curr_cnt)
return -1;
count += cnt;
if(count <= 0)
CounterMap.remove(token);
else
CounterMap.put(token,count);
return count;
}
}
public static int addCount(String token,int cnt)
{
synchronized (CounterMap) {
int count = getCount(token);
count += cnt;
if(count <= 0)
CounterMap.remove(token);
else
CounterMap.put(token,count);
return count;
}
}
public static int getCount(String token)
{
Integer count = CounterMap.get(token);
if(count == null)
return 0;
return count;
}
}
\ No newline at end of file
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