| | |
| | | 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; |
| | |
| | | @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; |
| | |
| | | //获取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无效"); |
| | | } |
| | |
| | | throw new WebAuthException("401,token过期或者无效"); |
| | | } |
| | | |
| | | //判断token是否在退出黑名单 |
| | | String redisToken = (String)redisHashUtil.getMapVal("webToken",id); |
| | | if(token.equals(redisToken)) |
| | | throw new WebAuthException("401,token过期"); |
| | | |
| | | return true; |
| | | } |
| | | } |