本文主要jwt出现的异常问题,如果有什么需要改进的地方还请大佬指出⛺️
作者简介:大家好,我是青衿
☁️博客首页:CSDN主页放风讲故事
每日一句:努力一点,优秀一点
使用封装好的 JwtUtil 的工具类时,自定义设置秘钥明文,启动后报错:
public class JwtUtil {
//有效期为
public static final Long JWT_TTL = 72*60 * 60 *1000L;// 60 * 60 *1000 一个小时
//设置秘钥明文
public static final String JWT_KEY = "qingzhiqc";
public static String getUUID(){
String token = UUID.randomUUID().toString().replaceAll("-", "");
return token;
}
/**
* 生成jtw
* @param subject token中要存放的数据(json格式)
* @return
*/
public static String createJWT(String subject) {
JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间
return builder.compact();
}
报错信息:
java.lang.IllegalArgumentException: Last unit does not have enough valid bits
at java.util.Base64$Decoder.decode0(Base64.java:734) ~[na:1.8.0_281]
at java.util.Base64$Decoder.decode(Base64.java:526) ~[na:1.8.0_281]
at java.util.Base64$Decoder.decode(Base64.java:549) ~[na:1.8.0_281]
at com.qingzhi.utils.JwtUtil.generalKey(JwtUtil.java:91) ~[classes/:na]
at com.qingzhi.utils.JwtUtil.getJwtBuilder(JwtUtil.java:51) ~[classes/:na]
at com.qingzhi.utils.JwtUtil.createJWT(JwtUtil.java:34) ~[classes/:na]
at com.qingzhi.service.impl.BlogLoginServiceImpl.login(BlogLoginServiceImpl.java:53) ~[classes/:na]
at com.qingzhi.controller.BlogLoginController.login(BlogLoginController.java:34) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.7.jar:5.3.7]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.3.7.jar:5.3.7]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.3.7.jar:5.3.7]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [tomcat-embed-core-9.0.46.jar:4.0.FR]
`
报错信息整整齐齐,如图所示,在进行base64加密和解密的过程中会出现这个问题,报错网上找了一些文章,根本原因为:编码后的字符串长度不符合要求(要求长度是4的倍数)
所以,这里我设置的秘钥明文有问题,最初设置的为 qingzhiqc ,我将 qingzhiqc 改为 qingzhi,再次执行正常生成 jwt
"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1ZWRlNzcyNWE3ZTQ0ODAzYTU3YjM1MzNmNjdjMzYxMCIsInN1YiI6IjEiLCJpc3MiOiJIRiIsImlhdCI6MTcwMDA0MTUxMywiZXhwIjoxNzAwMzAwNzEzfQ.cICUhn1KU7Ghego92x2h1vv8IOyfWb5zZA0IhdcxGWU"