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