From b604e4665a05e49d2f35ba1877046679d9d078ca Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Mon, 4 Dec 2023 17:24:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/BaseSecurityConfig.java | 9 +++++--- .../oauth2/common/enums/TokenType.java | 23 +++++++++++++++++++ .../properties/ResourceServerProperties.java | 8 +++++-- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/enums/TokenType.java diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/BaseSecurityConfig.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/BaseSecurityConfig.java index 305cb4c0..b342b95f 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/BaseSecurityConfig.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/BaseSecurityConfig.java @@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil; import com.central.common.constant.SecurityConstants; import com.central.common.utils.ResponseUtil; import com.central.common.utils.WebfluxResponseUtil; +import com.central.oauth2.common.enums.TokenType; import com.central.oauth2.common.properties.SecurityProperties; import com.central.oauth2.common.service.impl.RedisOAuth2AuthorizationService; import com.fasterxml.jackson.databind.ObjectMapper; @@ -12,22 +13,20 @@ import com.nimbusds.jose.jwk.RSAKey; import com.nimbusds.jose.jwk.source.ImmutableJWKSet; import com.nimbusds.jose.jwk.source.JWKSource; import com.nimbusds.jose.proc.SecurityContext; -import jakarta.servlet.http.HttpServletRequest; import org.redisson.api.RBucket; import org.redisson.api.RedissonClient; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpStatus; import org.springframework.security.oauth2.jwt.JwtDecoder; import org.springframework.security.oauth2.jwt.JwtEncoder; import org.springframework.security.oauth2.jwt.NimbusJwtEncoder; +import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationService; import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.access.AccessDeniedHandler; import org.springframework.security.web.server.ServerAuthenticationEntryPoint; import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler; -import org.springframework.web.server.ServerWebExchange; import java.security.KeyPair; import java.security.KeyPairGenerator; @@ -47,6 +46,10 @@ import java.util.UUID; public class BaseSecurityConfig { @Bean public OAuth2AuthorizationService oAuth2AuthorizationService(SecurityProperties securityProperties, RedissonClient redisson) { + String tokenType = securityProperties.getResourceServer().getTokenType(); + if (TokenType.MEMORY.getName().equals(tokenType)) { + return new InMemoryOAuth2AuthorizationService(); + } return new RedisOAuth2AuthorizationService(securityProperties, redisson); } diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/enums/TokenType.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/enums/TokenType.java new file mode 100644 index 00000000..a93630d3 --- /dev/null +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/enums/TokenType.java @@ -0,0 +1,23 @@ +package com.central.oauth2.common.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * token 类型 + * + * @author: zlt + * @date: 2023/12/4 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Getter +@AllArgsConstructor +public enum TokenType { + REDIS("redis"), + MEMORY("inMemory"), + JWT("jwt"), + REF("reference"); + private final String name; +} diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/ResourceServerProperties.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/ResourceServerProperties.java index dcab6931..8b9567c1 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/ResourceServerProperties.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/ResourceServerProperties.java @@ -16,7 +16,11 @@ import lombok.Setter; @Getter public class ResourceServerProperties { /** - * token格式: reference 引用令牌(不透明), self-contained 自包含令牌(jwt)) + * token格式: + * - redis: 通过 redis 来验证 + * - inMemory: 通过本地内存来验证 + * - jwt: 自包含令牌,自己解析内容 + * - reference: 引用令牌,通过远程调用授权服务器进行验证 */ - private String tokenFormat = "reference"; + private String tokenType = "redis"; }