public class JwtUtil {
private static final long EXPIER_TIME = 15 * 60 * 1000;
private static final String TOKEN_SECRET = "123456";
public static String sign(String userName, String userId,String roleName,String[] listCode) {
Date data = new Date(System.currentTimeMillis() + EXPIER_TIME);
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
Map<String, Object> header = new HashMap<>(2);
header.put("typ","JWT");
header.put("alg","HS256");
return JWT.create()
.withHeader(header)
.withClaim("userId",userId)
.withClaim("userName",userName)
.withClaim("roleName",roleName)
.withAudience(listCode)
.withExpiresAt(data).sign(algorithm);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String verify(String token){
try {
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT jwt = verifier.verify(token);
return "认证通过";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "认证失败";
} catch (TokenExpiredException e) {
e.printStackTrace();
return "token已失效,请重新登录";
} catch (Exception e) {
e.printStackTrace();
return "认证失败";
}
}
public static String getUserName(String token){
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("userName").asString();
}
public static String getUserId(String token){
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("userId").asString();
}
public static String getroleName(String token){
DecodedJWT jwt = JWT.decode(token);
return jwt.getClaim("roleName").asString();
}
public static List<String> getlistCode(String token){
DecodedJWT jwt = JWT.decode(token);
return jwt.getAudience();
}
首先自定义token工具类
设置token有效期,以及**和token中存的值
认证token的方法
获取token中存放的值
/**
* 登录
* AW
**/
@ResponseBody
@PostMapping(value = "/login", produces = {"application/json;charset=UTF-8"})
public HttpResponseEntity login(@RequestBody Map<String,Object> map){
return mkUsersService.login(map);
}
@ResponseBody
@PostMapping(value = "/test", produces = {"application/json;charset=UTF-8"})
public HttpResponseEntity aa(@RequestBody(required = false) Map<String,Object> map, HttpServletRequest request){
String token = request.getHeader("accessToken");
String flag = null;
if(!token.isEmpty()) {
flag = JwtUtil.verify(token);
}
String aa = JwtUtil.getroleName(token);
String bb = JwtUtil.getUserId(token);
String cc = JwtUtil.getUserName(token);
List<String> dd = JwtUtil.getlistCode(token);
Map<String,Object> hsmap = new HashMap();
hsmap.put("roleName",aa);
hsmap.put("UserId",bb);
hsmap.put("UserName",cc);
hsmap.put("listCode",dd);
hsmap.put("flag",flag);
return HttpResponseEntity.seccuss(hsmap);
}
@Override
public HttpResponseEntity login(Map<String,Object> map) {
String NAME = map.get("NAME").toString();
String PASSWD = map.get("PASSWD").toString();
MkUsersEntity user = mkUsersEntityMapper.selectPSW(NAME);
if(user == null){
return HttpResponseEntity.seccuss("用户不存在");
} else {
String str = MD5Utils.getMD5Code(PASSWD);
String password = user.getPasswd();
if(str.equals(password)){
Map<String,Object> role = mkUserRoleEntityMapper.selectRole(user.getId());
List<String> list = mkRoleFunEntityMapper.selectCode(Long.valueOf(role.get("ID").toString()));
String[] arr = list.toArray(new String[list.size()]);
String token = JwtUtil.sign(NAME,user.getId(),role.get("NAME").toString(),arr);
return HttpResponseEntity.seccuss(token);
} else {
return HttpResponseEntity.seccuss("密码不正确");
}
}
}
在login接口获取到token
在test接口测试
这个token就算是加进去了,可以用来区分权限