6 changed files with 288 additions and 5 deletions
@ -0,0 +1,53 @@ |
|||||
|
package com.zdxt.auth.framework.shiro.jwt; |
||||
|
|
||||
|
import com.auth0.jwt.JWTVerifier; |
||||
|
import com.auth0.jwt.exceptions.AlgorithmMismatchException; |
||||
|
import com.auth0.jwt.exceptions.SignatureGenerationException; |
||||
|
import com.auth0.jwt.exceptions.TokenExpiredException; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import org.springframework.web.servlet.HandlerInterceptor; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* @Author: kylin |
||||
|
* @Date: 2024/4/30 11:37 下午 |
||||
|
* @Version: 1.0 |
||||
|
* @Desc: TODO |
||||
|
*/ |
||||
|
public class JWTInterceptor implements HandlerInterceptor { |
||||
|
|
||||
|
@Override |
||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
||||
|
String token = request.getHeader("token"); |
||||
|
Map<String, Object> map = new HashMap<>(); |
||||
|
|
||||
|
try { |
||||
|
JWTVerifier verify = JWTUtils.verify(token); |
||||
|
map.put("state", true); |
||||
|
map.put("msg", "请求成功"); |
||||
|
}catch (SignatureGenerationException e){ |
||||
|
e.printStackTrace(); |
||||
|
map.put("msg","无效签名"); |
||||
|
}catch (TokenExpiredException e){ |
||||
|
e.printStackTrace(); |
||||
|
map.put("msg","token过期"); |
||||
|
}catch (AlgorithmMismatchException e){ |
||||
|
e.printStackTrace(); |
||||
|
map.put("msg","token算法不一致"); |
||||
|
} |
||||
|
catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
map.put("msg","token无效"); |
||||
|
} |
||||
|
map.put("state", false); // 设置状态
|
||||
|
// 将map转为json
|
||||
|
String json = new ObjectMapper().writeValueAsString(map); |
||||
|
response.setContentType("application/json;charset=UTF-8"); |
||||
|
response.getWriter().println(json); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,171 @@ |
|||||
|
package com.zdxt.auth.framework.shiro.jwt; |
||||
|
|
||||
|
/** |
||||
|
* @ClassName JwtUtil |
||||
|
* @Description TODO |
||||
|
* @Author Administrator |
||||
|
* @Date 2021/02/01 15:04:33 |
||||
|
* @Verison 1.0 |
||||
|
*/ |
||||
|
import com.auth0.jwt.JWT; |
||||
|
import com.auth0.jwt.JWTCreator; |
||||
|
import com.auth0.jwt.JWTVerifier; |
||||
|
import com.auth0.jwt.algorithms.Algorithm; |
||||
|
import com.auth0.jwt.exceptions.TokenExpiredException; |
||||
|
import com.auth0.jwt.interfaces.Claim; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
|
||||
|
import java.util.Base64; |
||||
|
import java.util.Date; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
|
||||
|
public class JWTUtils { |
||||
|
//过期时间 3天
|
||||
|
private static final long EXPIRE_TIME = 3 * 24 * 60 * 60 * 1000; |
||||
|
//私钥
|
||||
|
private static final String TOKEN_SECRET = "huiming@7122^$"; |
||||
|
|
||||
|
/** |
||||
|
* 生成签名,15分钟过期 |
||||
|
* 根据内部改造,支持6中类型,Integer,Long,Boolean,Double,String,Date |
||||
|
* @param map |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String sign(Map<String,Object> map) { |
||||
|
try { |
||||
|
// 设置过期时间
|
||||
|
Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); |
||||
|
// 私钥和加密算法
|
||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); |
||||
|
// 设置头部信息
|
||||
|
Map<String, Object> header = new HashMap<>(2); |
||||
|
header.put("typ", "jwt"); |
||||
|
// 返回token字符串
|
||||
|
JWTCreator.Builder builder = JWT.create() |
||||
|
.withHeader(header) |
||||
|
.withIssuedAt(new Date()) //发证时间
|
||||
|
.withExpiresAt(date); //过期时间
|
||||
|
// .sign(algorithm); //密钥
|
||||
|
|
||||
|
map.entrySet().forEach(entry -> { |
||||
|
if (entry.getValue() instanceof Integer) { |
||||
|
builder.withClaim( entry.getKey(),(Integer)entry.getValue()); |
||||
|
} else if (entry.getValue() instanceof Long) { |
||||
|
builder.withClaim( entry.getKey(),(Long)entry.getValue()); |
||||
|
} else if (entry.getValue() instanceof Boolean) { |
||||
|
|
||||
|
builder.withClaim( entry.getKey(),(Boolean) entry.getValue()); |
||||
|
} else if (entry.getValue() instanceof String) { |
||||
|
builder.withClaim( entry.getKey(),String.valueOf(entry.getValue())); |
||||
|
} else if (entry.getValue() instanceof Double) { |
||||
|
builder.withClaim( entry.getKey(),(Double)entry.getValue()); |
||||
|
} else if (entry.getValue() instanceof Date) { |
||||
|
builder.withClaim( entry.getKey(),(Date)entry.getValue()); |
||||
|
} |
||||
|
}); |
||||
|
return builder.sign(algorithm); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 检验token是否正确 |
||||
|
* @param **token** |
||||
|
* @return |
||||
|
*/ |
||||
|
public static JWTVerifier verify(String token){ |
||||
|
|
||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); |
||||
|
JWTVerifier verifier = JWT.require(algorithm).build(); |
||||
|
verifier.verify(token); |
||||
|
return verifier; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
*获取用户自定义Claim集合 |
||||
|
* @param token |
||||
|
* @return |
||||
|
*/ |
||||
|
public static Map<String, Claim> getClaims(String token){ |
||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); |
||||
|
JWTVerifier verifier = JWT.require(algorithm).build(); |
||||
|
Map<String, Claim> jwt = verifier.verify(token).getClaims(); |
||||
|
return jwt; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取过期时间 |
||||
|
* @param token |
||||
|
* @return |
||||
|
*/ |
||||
|
public static Date getExpiresAt(String token){ |
||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); |
||||
|
return JWT.require(algorithm).build().verify(token).getExpiresAt(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取jwt发布时间 |
||||
|
*/ |
||||
|
public static Date getIssuedAt(String token){ |
||||
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET); |
||||
|
return JWT.require(algorithm).build().verify(token).getIssuedAt(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 验证token是否失效 |
||||
|
* |
||||
|
* @param token |
||||
|
* @return true:过期 false:没过期 |
||||
|
*/ |
||||
|
public static boolean isExpired(String token) { |
||||
|
try { |
||||
|
final Date expiration = getExpiresAt(token); |
||||
|
return expiration.before(new Date()); |
||||
|
}catch (TokenExpiredException e) { |
||||
|
// e.printStackTrace();
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 直接Base64解密获取header内容 |
||||
|
* @param token |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getHeaderByBase64(String token){ |
||||
|
if (StringUtils.isEmpty(token)){ |
||||
|
return null; |
||||
|
}else { |
||||
|
byte[] header_byte = Base64.getDecoder().decode(token.split("\\.")[0]); |
||||
|
String header = new String(header_byte); |
||||
|
return header; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 直接Base64解密获取payload内容 |
||||
|
* @param token |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String getPayloadByBase64(String token){ |
||||
|
|
||||
|
if (StringUtils.isEmpty(token)){ |
||||
|
return null; |
||||
|
}else { |
||||
|
byte[] payload_byte = Base64.getDecoder().decode(token.split("\\.")[1]); |
||||
|
String payload = new String(payload_byte); |
||||
|
return payload; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
Loading…
Reference in new issue