报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jwtAccessTokenConverter' defined in class path resource [com/qyj/config/ResourceServerConfig.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at com.qyj.config.ResourceServerConfig.tokenStore(ResourceServerConfig.java:29) ~[ego-common-1.0.jar!/:1.0]
Caused by: java.lang.NullPointerException: null
原因:打包后触发bug,默认的编码格式不是UTF-8导致


方法一(无效 依然不行):代码写死为UTF-8 防止环境不同而发生意外
//like12 find,bug,20220928,打包后运行报错,默认编码变为GBK,开发环境为UTF-8,导致解析配置文件失败
publicKey = FileUtil.readString(resource.getFile(), Charset.forName("UTF-8"));
//publicKey = FileUtil.readString(resource.getFile(), Charset.defaultCharset());方法二(无效):修改IDEA默认编码
全局修改编码格式


方法三:不再读取配置文件 直接写死在Java中
/**
* 做公钥的解析
*
* @return
*/
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
/*// 设置公钥
ClassPathResource resource = new ClassPathResource("publicKey.txt");
String publicKey = null;
try {
//like12 find bug,20220928,打包后运行报错,默认编码变为GBK,开发环境为UTF-8(实测还是不行)
//publicKey = FileUtil.readString(resource.getFile(), Charset.defaultCharset());
publicKey = FileUtil.readString(resource.getFile(), Charset.forName("UTF-8"));
System.out.println(publicKey);
} catch (IOException e) {
e.printStackTrace();
}
// 设置公钥
jwtAccessTokenConverter.setVerifierKey(publicKey);*/
//like12 find bug,20220928,打包后运行报错,common项目无法读取配置文件publicKey.txt,无法解决 只能写死在代码中了
String publicKey = "-----BEGIN PUBLIC KEY-----\n" +
"MIIBIj*****************************************S+1Gc\n" +
"NwIDAQAB\n" +
"-----END PUBLIC KEY-----";
jwtAccessTokenConverter.setVerifierKey(publicKey);
return jwtAccessTokenConverter;
}版权声明:本文为tanzongbiao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。