赞
踩
/**
key(按照签名算法的字节长度设置key)
*/
private final static String SECRET_KEY = “0123456789_0123456789_0123456789”;
private final static long TOKEN_EXPIRE_MILLIS = 1000 * 60 * 20;
/**
创建token
@param claimMap
@return
*/
public static String createToken(Map<String, Object> claimMap) {
long currentTimeMillis = System.currentTimeMillis();
return Jwts.builder()
.setId(UUID.randomUUID().toString())
.setIssuedAt(new Date(currentTimeMillis)) // 设置签发时间
.setExpiration(new Date(currentTimeMillis + TOKEN_EXPIRE_MILLIS)) // 设置过期时间
.addClaims(claimMap)
.signWith(generateKey())
.compact();
}
/**
验证token
@param token
@return 0 验证成功,1、2、3、4、5 验证失败
*/
public static int verifyToken(String token) {
try {
Jwts.parser().setSigningKey(generateKey()).parseClaimsJws(token);
return 0;
} catch (ExpiredJwtException e) {
e.printStackTrace();
return 1;
} catch (UnsupportedJwtException e) {
e.printStackTrace();
return 2;
} catch (MalformedJwtException e) {
e.printStackTrace();
return 3;
} catch (SignatureException e) {
e.printStackTrace();
return 4;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return 5;
}
}
/**
解析token
@param token
@return
*/
public static Map<String, Object> parseToken(String token) {
return Jwts.parser() // 得到DefaultJwtParser
.setSigningKey(generateKey()) // 设置签名密钥
.parseClaimsJws(token)
.getBody();
}
/**
生成安全密钥
@return
*/
public static Key generateKey() {
return new SecretKeySpec(SECRET_KEY.getBytes(), SignatureAlgorithm.HS256.getJcaName());
}
}
3.鉴权注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Authentication {
}
4.鉴权切面
@Slf4j
@Aspect
@Component
public class AuthenticationAop {
@Pointcut(“@annotation( com.sohu.mptvad.equipment_monitoring.annotation.Authentication)”)
public void pointCutMethodBefore() {
}
@Before("pointCutMethodBefore()") public void doBefore(JoinPoint point) throws Exception { MethodInvocationProceedingJoinPoint mjp = (MethodInvocationProceedingJoinPoint) point; MethodSignature signature = (MethodSignature) mjp.getSignature(); Method method = signature.getMethod(); Authentication annotation = method.getAnnotation(Authentication.class); if (annotation != null) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); if (request ==null) { throw new AuthenticationException("【鉴权失败】,获取HttpServletRequest"); } String token = request.getParameter("token"); if (StringUtils.isEmpty(token)) { throw new AuthenticationException("【鉴权失败】,token不存在"); } Integer verifyToken = TokenUtil.verifyToken(token); if (verifyToken == 1) { throw new AuthenticationException("【鉴权失败】,token已过期"); } if (verifyToken != 0) { throw new AuthenticationException("【鉴权失败】,token错误"); } } } @AfterThrowing(value = "@annotation( com.sohu.mptvad.equipment_monitoring.annotation.Authentication)",throwing = "ex") public void handleException(Exception ex) { log.error(ex.getMessage()); }
}
5.使用注解在接口上
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。