java - Spring OAuth2 安全 - 客户端凭据 - 自定义 AuthenticationProvider
问题描述
我正在我们的服务架构中编写一个身份验证服务,它基本上是 Spring OAuth2 Authorization Server 的一个实现。因此,我需要针对许多不同的来源对提供的凭据进行身份验证,以支持遗留环境。
我主要专注于使用客户端凭据流,用户(其他服务)将使用他们自己的凭据来获取令牌,并且暂时不需要 OAuth2 的授权或刷新。
我已经成功地启动了一个受 Spring Security ( @EnableWebSecurity
) 保护的 Spring Boot 应用程序。我还成功设置了授权服务器 ( @EnableAuthorizationServer
),它提供了必要的端点 ( /oauth/token
) 来提供令牌。我已经能够使用内存和自定义 ClientDetailsService 配置授权服务器以成功获取令牌。这一切只是为了向自己证明我可以得到一些工作。
我的问题是我需要根据自定义源对凭据进行身份验证。我无法直接访问密码,当然也不知道它们是如何编码的。
翻阅Spring代码后,我可以看到,通过DaoAuthenticationProvider
调用PasswordEncoder.matches()
. 不幸的是,我没有从 获得密码,ClientDetailsService
如果我这样做了,我也不知道密码是如何编码的,所以自定义PasswordEncoder
对我没有多大好处(我也需要以多种方式匹配只有一个PasswordEncoder
)。所以,我只剩下定义我自己的AuthenticationProvider
(或AuthenticationManager
)了。
我能够实现自己的AuthenticationProvider
并将其提供给 Spring Security 配置。这非常有效,我能够将身份验证推迟到我自己的提供商,该提供商可以做任何我认为合适的事情(例如委托给另一个服务进行身份验证),但这仅适用于非 OAuth2 端点。
现在这就是一切开始崩溃的地方。无论出于何种原因,我都无法让/oauth/token
端点使用我提供的定义的AuthenticationManager
or AuthenticationProvider
。它始终默认为AuthenticationManger
withAnonymousAuthenticationProvider
和DaoAuthenticationProvider
。
网络安全
这里没什么意思。我正在全局公开身份验证管理器,试图在 OAuth2 配置中注册它。我留下了一些注释代码来展示我尝试过的其他一些东西,但它们几乎都完成了同样的事情:它适用于除 OAuth2 端点之外的所有东西。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public AuthenticationProvider customAuthenticationProvider;
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
// return new ProviderManager(Arrays.asList(customAuthenticationProvider));
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.authenticationProvider(customAuthenticationProvider);
}
// @Autowired
// public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// auth.authenticationProvider(customAuthenticationProvider);
// }
}
认证服务器
我已将其简化为我认为应该是正确的配置。我故意省略了它,ClientDetailsService
因为它是提供程序配置的一部分并且它工作正常。我会忽略TokenStore
并使用默认值,直到我可以通过身份验证。同样,authenticationManager
这里应该是从 WebSecurity 公开的全局。我也尝试ProviderManager
在端点配置器中创建一个新的,但这对我也不起作用。
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.tokenKeyAccess("permitAll()")
.checkTokenAccess("permitAll()")
;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
我也尝试过扩展AuthorizationServerSecurityConfiguration
但没有成功:
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig2 extends AuthorizationServerSecurityConfiguration {
@Autowired
public AuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
}
我希望我的自定义AuthenticationProvider
会显示在列表中,但它不会出现在我尝试过的所有内容中。
这可能是故意的,但如果是这样的话,提供自定义身份验证方案的正确方法是什么?
我真的很想避免它,但我真的需要实现自定义过滤器并绕过 Spring 提供的所有好东西吗?如果是,我将如何去做?
解决方案
我最终配置了一个附加BasicAuthenticationFilter
到令牌端点。对我来说,这有点混乱,因为现在安全过滤器链中有两个相同的过滤器,但它可以解决问题。
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private UserService userService;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.tokenKeyAccess("isAuthenticated()")
.checkTokenAccess("isAuthenticated()");
// override the default basic authentication filter in order to provide
// a custom authentication manager
oauthServer.addTokenEndpointAuthenticationFilter(new BasicAuthenticationFilter(authenticationManager, new BasicAuthenticationEntryPoint()));
}
}
推荐阅读
- android - CardView 背景颜色被修剪
- css - React RC-Slider Grab 与标签上的指针
- java - 尝试将灰度滤镜应用于 ImageView 时我错过了什么?
- java - 从 javalin/java 中的文件格式化文本
- java - 我怎样才能最好地用 Java 实现战争迷雾?
- javascript - React:使用带有 React 钩子的 localStorage,除非单击按钮,否则如何使我的组件不重新加载?
- python - 如何在python3中迭代两个不同长度的文件
- windows - Docker for windows 无法启动
- mongodb - 如何在 Golang 中跨包管理 MongoDB 客户端
- javascript - 如果日期出现在“2020 年 5 月 20 日 16:00:00”中,如何将日期格式化为 javascript 中的 mm/dd/yyyy?