JWT
1. jwt工具类
public class JwtUtils {
private static final String sign = "@dnajnasaskdnkasn";
public static String getToken(Map<String,String> map){
Calendar instance = Calendar.getInstance();
instance.add(Calendar.DATE, 7);
JWTCreator.Builder builder = JWT.create();
map.forEach((k,v)->{
builder.withClaim(k,v);
});
String token = builder.withExpiresAt(instance.getTime())
.sign(Algorithm.HMAC256(JwtUtils.sign));
return token;
}
public static void verifyToken(String token){
JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
}
public static DecodedJWT getTokenInfo(String token){
DecodedJWT verify = JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
return verify;
}
}
2. JWT与SpringBoot整合
2.1 引入依赖
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
2.2 书写jwt拦截器
public class JwtInterceptors implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
HashMap<String, Object> hashMap = new HashMap<>();
try {
DecodedJWT decodedJWT = JwtUtils.verfitToken(token);
hashMap.put("state",true);
hashMap.put("msg","认证成功");
return true;
}catch (SignatureVerificationException e){
e.printStackTrace();
hashMap.put("msg","无效签名");
}catch (TokenExpiredException e){
e.printStackTrace();
hashMap.put("msg","token以过期");
}catch (AlgorithmMismatchException e){
e.printStackTrace();
hashMap.put("msg","两次算法不一致");
}catch (NullPointerException e){
e.printStackTrace();
hashMap.put("msg","token为空");
}catch (Exception e){
e.printStackTrace();
hashMap.put("msg","token异常");
}
hashMap.put("state",false);
String json = new ObjectMapper().writeValueAsString(hashMap);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(json);
return false;
}
}
2.3 书写SpringBoot配置类
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new JwtInterceptors())
.addPathPatterns("/test")
.excludePathPatterns("/login");
}
}
2.4 书写Controller层进行测试
@RestController
@Slf4j
public class UserController {
@Autowired
UserService userService;
@PostMapping("/login")
public Map<String,Object> login(User user){
HashMap<String, Object> hashMap = new HashMap<>();
try{
User login = userService.login(user);
HashMap<String, String> map = new HashMap<>();
map.put("id", String.valueOf(login.getId()));
map.put("name",login.getUsername());
String token = JwtUtils.getToken(map);
hashMap.put("state",true);
hashMap.put("msg","认证成功");
hashMap.put("token",token);
return hashMap;
}catch (Exception e) {
e.printStackTrace();
hashMap.put("state",false);
hashMap.put("msg","认证失败");
return hashMap;
}
}
@PostMapping("/test")
public Map<String,Object> test(){
System.out.println("进入了");
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("state",true);
hashMap.put("msg","认证成功");
return hashMap;
}
}