diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java index e70336773..1e13a359d 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/AuthorizationServerConfig.java @@ -38,6 +38,7 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Aut import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; import org.springframework.security.oauth2.provider.ClientDetailsService; import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices; import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator; import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenStore; @@ -60,6 +61,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap private final UserDetailsService pigxUserDetailsService; + private final AuthorizationCodeServices authorizationCodeServices; + private final TokenEnhancer pigxTokenEnhancer; @Override @@ -77,8 +80,8 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap public void configure(AuthorizationServerEndpointsConfigurer endpoints) { endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST).tokenStore(tokenStore()) .tokenEnhancer(pigxTokenEnhancer).userDetailsService(pigxUserDetailsService) - .authenticationManager(authenticationManagerBean).reuseRefreshTokens(false) - .pathMapping("/oauth/confirm_access", "/token/confirm_access") + .authorizationCodeServices(authorizationCodeServices).authenticationManager(authenticationManagerBean) + .reuseRefreshTokens(false).pathMapping("/oauth/confirm_access", "/token/confirm_access") .exceptionTranslator(new PigxWebResponseExceptionTranslator()); } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxAuthorizationCodeServicesImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxAuthorizationCodeServicesImpl.java new file mode 100644 index 000000000..684d8435c --- /dev/null +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxAuthorizationCodeServicesImpl.java @@ -0,0 +1,69 @@ +package com.pig4cloud.pigx.auth.service; + +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import lombok.Cleanup; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices; +import org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy; +import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy; +import org.springframework.stereotype.Service; + +/** + * @author lengleng + * @date 2020/9/3 + *

+ * 授权码模式支持集群 + */ +@Service +@RequiredArgsConstructor +public class PigxAuthorizationCodeServicesImpl extends RandomValueAuthorizationCodeServices { + + private final RedisConnectionFactory connectionFactory; + + @Setter + private String prefix = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_CODE_PREFIX; + + @Setter + private RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy(); + + /** + * 保存 code 和 认证信息 + * @param code 授权码模式: code + * @param authentication 认证信息 + */ + @Override + protected void store(String code, OAuth2Authentication authentication) { + @Cleanup + RedisConnection connection = connectionFactory.getConnection(); + connection.set(serializationStrategy.serialize(prefix + code), serializationStrategy.serialize(authentication)); + } + + /** + * 删除code 并返回认证信息 + * @param code 授权码模式: code + * @return + */ + @Override + protected OAuth2Authentication remove(String code) { + @Cleanup + RedisConnection connection = connectionFactory.getConnection(); + + byte[] key = serializationStrategy.serialize(prefix + code); + byte[] value = connection.get(key); + + if (value == null) { + return null; + } + + OAuth2Authentication oAuth2Authentication = serializationStrategy.deserialize(value, + OAuth2Authentication.class); + + connection.del(key); + return oAuth2Authentication; + } + +} \ No newline at end of file diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java index 162625f10..3a9af36b4 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java @@ -55,6 +55,11 @@ public interface SecurityConstants { */ String OAUTH_PREFIX = "oauth:"; + /** + * 授权码模式code key 前缀 + */ + String OAUTH_CODE_PREFIX = "oauth:code:"; + /** * 项目的license */ diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java index 59898e373..965438a9a 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysUserServiceImpl.java @@ -76,7 +76,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 保存用户信息 - * * @param userDto DTO 对象 * @return success/fail */ @@ -99,7 +98,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 通过查用户的全部信息 - * * @param sysUser 用户 * @return */ @@ -126,8 +124,7 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 分页查询用户信息(含有角色信息) - * - * @param page 分页对象 + * @param page 分页对象 * @param userDTO 参数列表 * @return */ @@ -138,7 +135,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 通过ID查询用户信息 - * * @param id 用户ID * @return 用户信息 */ @@ -149,7 +145,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 删除用户 - * * @param sysUser 用户 * @return Boolean */ @@ -205,7 +200,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 查询上级部门的用户信息 - * * @param username 用户名 * @return R */ @@ -224,7 +218,6 @@ public class SysUserServiceImpl extends ServiceImpl impl /** * 获取当前用户的子部门信息 - * * @return 子部门列表 */ private List getChildDepts() {