优化oauth-starter

This commit is contained in:
朱毅骏 2023-02-09 09:38:59 +08:00
parent 837d8eb00b
commit 352938bff4

View File

@ -4,6 +4,7 @@ import cn.zyjblogs.starter.common.autoconfigure.rsa.RsaKeyProperties;
import cn.zyjblogs.starter.common.entity.constant.CommonRedisKeyConstant;
import cn.zyjblogs.starter.redis.utils.RedisTemplateHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -12,6 +13,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.io.IOException;
@ -21,6 +23,7 @@ import java.nio.file.Paths;
/**
* @author zhuyijun
*/
@Log4j2
@Configuration
@RequiredArgsConstructor
public class TokenConfig {
@ -48,19 +51,25 @@ public class TokenConfig {
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
try {
if (rsaKeyProperties.getEnable()) {
String publicKey = redisTemplateHandler.get(CommonRedisKeyConstant.REDIS_KEY_PUBLIC_RSA);
String publicKey="";
try{
publicKey = redisTemplateHandler.get(CommonRedisKeyConstant.REDIS_KEY_PUBLIC_RSA);
}catch (Exception e){
log.error("redis连接失败,无法获取数据");
}
if (!StringUtils.hasLength(publicKey)) {
try {
publicKey = IOUtils.toString(Paths.get(rsaKeyProperties.getPubKeyPath()).toUri(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("rsa秘钥读取失败读取路径如下"+rsaKeyProperties.getPubKeyPath());
}
}
// 公钥验签
converter.setVerifierKey(publicKey);
}
Assert.notNull(oauthAccessTokenConverter, "oauthAccessTokenConverter is required");
converter.setAccessTokenConverter(oauthAccessTokenConverter);
return converter;
} catch (final IOException e) {
throw new RuntimeException("获取不到公私密钥");
}
}
}