✨ 添加 OAuth2ClientConfigurer 抽象接口,方便用户替换 ClientDetailsService 的配置方式
This commit is contained in:
@@ -5,6 +5,8 @@ import com.hccake.ballcat.auth.CustomAccessTokenConverter;
|
||||
import com.hccake.ballcat.auth.OAuth2AuthorizationServerProperties;
|
||||
import com.hccake.ballcat.auth.authentication.TokenGrantBuilder;
|
||||
import com.hccake.ballcat.auth.configurer.CustomAuthorizationServerConfigurer;
|
||||
import com.hccake.ballcat.auth.configurer.JdbcOAuth2ClientConfigurer;
|
||||
import com.hccake.ballcat.auth.configurer.OAuth2ClientConfigurer;
|
||||
import com.hccake.ballcat.common.redis.config.CachePropertiesHolder;
|
||||
import com.hccake.ballcat.common.security.component.CustomRedisTokenStore;
|
||||
import com.hccake.ballcat.common.security.constant.SecurityConstants;
|
||||
@@ -26,6 +28,8 @@ import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 授权服务器需要的一些 Bean 信息注册
|
||||
*
|
||||
@@ -109,4 +113,15 @@ public class AuthorizationAutoConfiguration {
|
||||
return new TokenGrantBuilder(authenticationManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth2 客户端配置类,默认使用 jdbc 从数据库获取 OAuth2 Client 信息
|
||||
* @param dataSource 数据源
|
||||
* @return JdbcOAuth2ClientConfigurer
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public OAuth2ClientConfigurer oAuth2ClientConfigurer(DataSource dataSource) {
|
||||
return new JdbcOAuth2ClientConfigurer(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.hccake.ballcat.auth.configurer;
|
||||
|
||||
import com.hccake.ballcat.auth.authentication.TokenGrantBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
@@ -20,8 +21,6 @@ import org.springframework.security.oauth2.provider.token.TokenEnhancer;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
@@ -30,16 +29,14 @@ import javax.sql.DataSource;
|
||||
@RequiredArgsConstructor
|
||||
public class CustomAuthorizationServerConfigurer implements AuthorizationServerConfigurer {
|
||||
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final OAuth2ClientConfigurer clientConfigurer;
|
||||
|
||||
private final DataSource dataSource;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
|
||||
private final TokenStore tokenStore;
|
||||
|
||||
private final UserDetailsService userDetailsService;
|
||||
|
||||
private final TokenEnhancer tokenEnhancer;
|
||||
|
||||
private final AccessTokenConverter accessTokenConverter;
|
||||
|
||||
private final WebResponseExceptionTranslator<OAuth2Exception> webResponseExceptionTranslator;
|
||||
@@ -48,6 +45,9 @@ public class CustomAuthorizationServerConfigurer implements AuthorizationServerC
|
||||
|
||||
private final TokenGrantBuilder tokenGrantBuilder;
|
||||
|
||||
@Autowired(required = false)
|
||||
private TokenEnhancer tokenEnhancer;
|
||||
|
||||
/**
|
||||
* 定义资源权限控制的配置
|
||||
* @param security AuthorizationServerSecurityConfigurer
|
||||
@@ -70,22 +70,18 @@ public class CustomAuthorizationServerConfigurer implements AuthorizationServerC
|
||||
*/
|
||||
@Override
|
||||
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
|
||||
// 启用 jdbc 方式获取客户端配置信息
|
||||
clients.jdbc(dataSource);
|
||||
clientConfigurer.configure(clients);
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权服务的访问路径相关配置
|
||||
* @param endpoints AuthorizationServerEndpointsConfigurer
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
|
||||
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
|
||||
// @formatter:off
|
||||
endpoints.tokenStore(tokenStore).userDetailsService(userDetailsService)
|
||||
.authenticationManager(authenticationManager)
|
||||
// 自定义token
|
||||
.tokenEnhancer(tokenEnhancer)
|
||||
// 强制刷新token时,重新生成refreshToken
|
||||
.reuseRefreshTokens(false)
|
||||
// 自定义的认证时异常转换
|
||||
@@ -95,6 +91,11 @@ public class CustomAuthorizationServerConfigurer implements AuthorizationServerC
|
||||
// 使用自定义的 TokenConverter,方便在 checkToken 时,返回更多的信息
|
||||
.accessTokenConverter(accessTokenConverter);
|
||||
// @formatter:on
|
||||
|
||||
// 自定义token
|
||||
if (tokenEnhancer != null) {
|
||||
endpoints.tokenEnhancer(tokenEnhancer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +103,10 @@ public class CustomAuthorizationServerConfigurer implements AuthorizationServerC
|
||||
*/
|
||||
@Order(1)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
private class AuthorizeServerConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
||||
@RequiredArgsConstructor
|
||||
static class AuthorizeServerConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
||||
|
||||
private final AuthenticationManager authenticationManager;
|
||||
|
||||
private static final String AUTHORIZE_ENDPOINT_PATH = "/oauth/authorize";
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.hccake.ballcat.auth.configurer;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* 启用 jdbc 方式获取客户端配置信息
|
||||
*
|
||||
* @author hccake
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcOAuth2ClientConfigurer implements OAuth2ClientConfigurer {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Override
|
||||
public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
|
||||
configurer.jdbc(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.hccake.ballcat.auth.configurer;
|
||||
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
|
||||
/**
|
||||
* 授权服务器的 OAuth2 Client 相关配置类
|
||||
*
|
||||
* @author hccake
|
||||
*/
|
||||
public interface OAuth2ClientConfigurer {
|
||||
|
||||
/**
|
||||
* 配置 clientDetailsService
|
||||
* @param clientDetailsServiceConfigurer clientDetailsService 配置类
|
||||
* @exception Exception 异常信息
|
||||
*/
|
||||
void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user