✨ Introducing new features. #1632 扩展支持授权码模式支持认证中心集群
✨ Introducing new features. #1632 扩展支持授权码模式支持认证中心集群
This commit is contained in:
1 parent
2340fe87c1
commit
ded5fe44e1
4 files changed
+80
-10
No files matched your search
+5
-2
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
+69
@@ -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
|
||||
* <p>
|
||||
* 授权码模式支持集群
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -55,6 +55,11 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String OAUTH_PREFIX = "oauth:";
|
||||
|
||||
/**
|
||||
* 授权码模式code key 前缀
|
||||
*/
|
||||
String OAUTH_CODE_PREFIX = "oauth:code:";
|
||||
|
||||
/**
|
||||
* 项目的license
|
||||
*/
|
||||
|
||||
+1
-8
@@ -76,7 +76,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 保存用户信息
|
||||
*
|
||||
* @param userDto DTO 对象
|
||||
* @return success/fail
|
||||
*/
|
||||
@@ -99,7 +98,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 通过查用户的全部信息
|
||||
*
|
||||
* @param sysUser 用户
|
||||
* @return
|
||||
*/
|
||||
@@ -126,8 +124,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 分页查询用户信息(含有角色信息)
|
||||
*
|
||||
* @param page 分页对象
|
||||
* @param page 分页对象
|
||||
* @param userDTO 参数列表
|
||||
* @return
|
||||
*/
|
||||
@@ -138,7 +135,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 通过ID查询用户信息
|
||||
*
|
||||
* @param id 用户ID
|
||||
* @return 用户信息
|
||||
*/
|
||||
@@ -149,7 +145,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param sysUser 用户
|
||||
* @return Boolean
|
||||
*/
|
||||
@@ -205,7 +200,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 查询上级部门的用户信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return R
|
||||
*/
|
||||
@@ -224,7 +218,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
|
||||
/**
|
||||
* 获取当前用户的子部门信息
|
||||
*
|
||||
* @return 子部门列表
|
||||
*/
|
||||
private List<Integer> getChildDepts() {
|
||||
|
||||
Reference in new issue
Block a user