|
|
@@ -1,11 +1,10 @@
|
|
|
package com.td.boss.filter;
|
|
|
|
|
|
|
|
|
+import com.td.boss.common.pojo.IpVo;
|
|
|
import com.td.boss.common.pojo.Result;
|
|
|
import com.td.boss.config.enums.ResultEnum;
|
|
|
-import com.td.boss.util.JsonUtils;
|
|
|
-import com.td.boss.util.JwtTokenUtil;
|
|
|
-import com.td.boss.util.TokenRequestWrapper;
|
|
|
+import com.td.boss.util.*;
|
|
|
import io.jsonwebtoken.ExpiredJwtException;
|
|
|
import io.jsonwebtoken.SignatureException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -17,6 +16,7 @@ import javax.servlet.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
* @author:slambb
|
|
|
@@ -33,9 +33,11 @@ public class gameFilter implements Filter {
|
|
|
@Override
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public void destroy() {
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
|
|
//处理跨域请求
|
|
|
@@ -59,22 +61,45 @@ public class gameFilter implements Filter {
|
|
|
//登录,获取验证码,不用登录的接口
|
|
|
if (!(path.indexOf("game") > -1) // 存在game字段,需要检验token
|
|
|
|| path.indexOf("loginToken") > -1
|
|
|
- // || path.indexOf("publicKeyAndSys") > -1
|
|
|
- // || path.indexOf("playback")>-1
|
|
|
- || path.indexOf("game_websocket") > -1 // todo 暂时放行
|
|
|
- //这里三个接口给DApp后台调用
|
|
|
- || path.indexOf("getSNBAndFreeze") >-1
|
|
|
- || path.indexOf("setSNBAndUnfreeze") >-1
|
|
|
- || path.indexOf("cntPlayback") >-1
|
|
|
- ) {
|
|
|
+ // || path.indexOf("publicKeyAndSys") > -1
|
|
|
+ // || path.indexOf("playback")>-1
|
|
|
+ // || path.indexOf("game_websocket") > -1 // todo 暂时放行
|
|
|
+ //这里三个接口给DApp后台调用
|
|
|
+ //|| path.indexOf("getSNBAndFreeze") >-1
|
|
|
+ //|| path.indexOf("setSNBAndUnfreeze") >-1
|
|
|
+ //|| path.indexOf("cntPlayback") >-1
|
|
|
+ ) {
|
|
|
//登录情况直接放行
|
|
|
filterChain.doFilter(servletRequest, response);
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ //这里三个接口给DApp后台调用,需要指定ip访问
|
|
|
+ if (
|
|
|
+ path.indexOf("getSNBAndFreeze") > -1
|
|
|
+ || path.indexOf("setSNBAndUnfreeze") > -1
|
|
|
+ || path.indexOf("cntPlayback") > -1) {
|
|
|
+ //登陆IP不在白名单
|
|
|
+ String ipAddr = IpUtil.getIpAddr(req);
|
|
|
+ log.info("ipAddr:"+ipAddr);
|
|
|
+ //IpVo ipVo = IpUtil.getIpVoByRequest(req);
|
|
|
+ //log.info("ip2Addr:"+ ipVo.toString());
|
|
|
+ String limitedIp = SysSettingLimitUtil.getSysSettingLimit().getLimitedIp();
|
|
|
+ if (StringUtils.hasText(limitedIp) && !Arrays.asList(limitedIp.split(",")).contains(ipAddr)) {
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.getWriter().print(JsonUtils.toJson(Result.of(null, false, ResultEnum.LOGIN_IP_IS_ERROR.getMessage(), ResultEnum.LOGIN_IP_IS_ERROR.getCode())));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ filterChain.doFilter(servletRequest, response);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
if (null == token || token.isEmpty() || !token.startsWith("Bearer ")) {
|
|
|
//没有token信息
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
- response.getWriter().print( JsonUtils.toJson (Result.of(null,false,ResultEnum.TOKEN_DOES_NOT_EXIST.getMessage(),ResultEnum.TOKEN_DOES_NOT_EXIST.getCode())));
|
|
|
+ response.getWriter().print(JsonUtils.toJson(Result.of(null, false, ResultEnum.TOKEN_DOES_NOT_EXIST.getMessage(), ResultEnum.TOKEN_DOES_NOT_EXIST.getCode())));
|
|
|
return;
|
|
|
} else {
|
|
|
String authToken = token.substring(7);
|
|
|
@@ -84,7 +109,7 @@ public class gameFilter implements Filter {
|
|
|
//todo 后面需要校验时间,加入可切换 secret 校验
|
|
|
log.info("validateToken:{}", jwtTokenUtil.validateToken(authToken, userId));
|
|
|
// 加入secret校验
|
|
|
- if(!jwtTokenUtil.validateToken(authToken, userId)){
|
|
|
+ if (!jwtTokenUtil.validateToken(authToken, userId)) {
|
|
|
log.info("secret 不可靠,validateToken:{}", jwtTokenUtil.validateToken(authToken, userId));
|
|
|
}
|
|
|
if (!StringUtils.isEmpty(userId)) {
|
|
|
@@ -92,16 +117,16 @@ public class gameFilter implements Filter {
|
|
|
} else {
|
|
|
//不存在用户信息
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
- response.getWriter().print(JsonUtils.toJson(Result.of(authToken,false,ResultEnum.TOKEN_DOES_NOT_HAVE_USER_ID.getMessage(),ResultEnum.TOKEN_DOES_NOT_HAVE_USER_ID.getCode())));
|
|
|
+ response.getWriter().print(JsonUtils.toJson(Result.of(authToken, false, ResultEnum.TOKEN_DOES_NOT_HAVE_USER_ID.getMessage(), ResultEnum.TOKEN_DOES_NOT_HAVE_USER_ID.getCode())));
|
|
|
}
|
|
|
} catch (ExpiredJwtException e) {
|
|
|
//token过期
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
- response.getWriter().print(JsonUtils.toJson(Result.of(null,false,ResultEnum.TOKEN_OUT_OF_DATE.getMessage(), ResultEnum.TOKEN_OUT_OF_DATE.getCode())));
|
|
|
- } catch (SignatureException e){
|
|
|
+ response.getWriter().print(JsonUtils.toJson(Result.of(null, false, ResultEnum.TOKEN_OUT_OF_DATE.getMessage(), ResultEnum.TOKEN_OUT_OF_DATE.getCode())));
|
|
|
+ } catch (SignatureException e) {
|
|
|
//secret校验密钥不对
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
- response.getWriter().print(JsonUtils.toJson(Result.of(authToken,false,ResultEnum.TOKEN_IS_ILLEGAL.getMessage(),ResultEnum.TOKEN_IS_ILLEGAL.getCode())));
|
|
|
+ response.getWriter().print(JsonUtils.toJson(Result.of(authToken, false, ResultEnum.TOKEN_IS_ILLEGAL.getMessage(), ResultEnum.TOKEN_IS_ILLEGAL.getCode())));
|
|
|
}
|
|
|
|
|
|
}
|