kaiyu
2020-09-17 613dd76a3aded439f1002d904d85d8332ddb03d1
src/main/java/com/moral/common/interceptor/WebInterceptor.java
@@ -7,6 +7,8 @@
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.moral.common.exception.WebAuthException;
import com.moral.common.util.RedisHashUtil;
import com.moral.common.util.WebTokenUtils;
import com.moral.common.webAnno.PassToken;
import com.moral.common.webAnno.UserLoginToken;
import com.moral.entity.Account;
@@ -35,13 +37,15 @@
    @Resource
    AccountService accountService;
    @Resource
    RedisHashUtil redisHashUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
        response.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        String token = request.getHeader("token");
        Enumeration<String> headerNames = request.getHeaderNames();
        //如果不是映射到方法则直接通过
        if(!(o instanceof HandlerMethod)){
            return true;
@@ -68,9 +72,7 @@
                //获取ID
                String id = "";
                try {
                    Map<String, Claim> claims = JWT.decode(token).getClaims();
                    Claim accountId = claims.get("aid");
                    id = accountId.asString();
                    id = WebTokenUtils.getIdBytoken(token);
                }catch (JWTDecodeException e){
                    throw new WebAuthException("401,token无效");
                }
@@ -90,6 +92,11 @@
                    throw new WebAuthException("401,token过期或者无效");
                }
                //判断token是否在退出黑名单
                String redisToken = (String)redisHashUtil.getMapVal("webToken",id);
                if(token.equals(redisToken))
                    throw new WebAuthException("401,token过期");
                return true;
            }
        }