子项目提供接口返回公钥内容
@RestController
public class TestController {
@RequestMapping("/test")
public String test() {
return "-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhIMk0muSD/UbVcvCrwPp\n" +
"PBr7lcKnuxmEo/IfL9NN4BfVwOkt+ghF7IvQRYKNng32yZzxRcklZbbjST0x8/Ze\n" +
"aVF4cdn6TKMKveGQV+MwIsfpifcHyEvPiauufvDTLzCyohWUVlRVSzJAU9vYa35+\n" +
"aAK9ALwuaKGPm7gM7mVhw7m0aiLFgmZ6WHulnaNOPVJb53ERq/XuRbK2YLf9o7\n" +
"zrU3r0d0Ldv0bXCDWp8hfkFeyt8hUPBI3OhlvKsC+Foar63JcMHgmOXAmHZGF5Gm\n" +
"WABHh8fH8HEW+gdtVukWUuMfOuTg9DKkiGO+1EM8ZE70XTUKKD2I2qST2b2k6dac\n" +
"OwIDAQAB\n" +
"-----END PUBLIC KEY-----";
}
}
注意:这里可以读取配置文件来实现;
jwt获取公钥
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
String publicKey = null;
try {
Properties properties = new Properties();
String property = null;
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream("application.yml"));
property = properties.getProperty("resource-url");
System.out.println("property:" + property);
} catch (IOException e) {
e.printStackTrace();
}
URL url = new URL(property);
URLConnection conn = url.openConnection();
// 读取超时
conn.setReadTimeout(1000);
// 链接超时
conn.setConnectTimeout(1000);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder receive = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
receive.append(line);
}
reader.close();
conn.connect();
publicKey = receive.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
converter.setVerifierKey(publicKey);
return converter;
}
初步实现功能;
现在,需要把controller 修改一下:
@RestController
public class TestController {
@RequestMapping("/test")
public String test() {
Resource resource = new ClassPathResource("public.cert");
String publicKey ;
try {
publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
return publicKey;
}
}
将public.cert 当做配置文件放到resources 资源路径下就可以了;