Rut*_*uth 5 spring-security oauth-2.0 spring-boot
我们尝试将spring-security-oauth2-client版本5.3.0.RELEASE与客户端凭据流程一起使用。因此,我们尝试纯粹在客户端使用它,所有端点(例如执行器)根本不应该受到保护。
基本上我们添加了这样的客户端凭据:
spring:
security:
oauth2:
client:
registration:
my-client:
authorization-grant-type: client_credentials
client-id: foo
client-secret: bar
provider: my-provider
provider:
my-provider:
tokenUri: http://token.uri/
Run Code Online (Sandbox Code Playgroud)
我们的拦截器如下所示:
class OAuth2AuthorizedClientInterceptor implements ClientHttpRequestInterceptor {
OAuth2AuthorizedClientManager manager
String clientId
AnonymousAuthenticationToken PRINCIPAL = new AnonymousAuthenticationToken("key", "anonymous", createAuthorityList("ROLE_ANONYMOUS"))
OAuth2AuthorizedClientInterceptor(OAuth2AuthorizedClientManager manager, String clientId) {
this.manager = manager
this.clientId = clientId
}
@Override
ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
OAuth2AuthorizeRequest authorizedRequest = OAuth2AuthorizeRequest
.withClientRegistrationId(clientId)
.principal(PRINCIPAL)
.build()
OAuth2AuthorizedClient authorizedClient = this.manager.authorize(authorizedRequest)
if (!authorizedClient?.accessToken?.tokenValue) {
throw new IllegalArgumentException("No access token for client '${clientId}'")
}
request.headers.setBearerAuth(authorizedClient?.accessToken?.tokenValue)
return execution.execute(request, body)
}
}
Run Code Online (Sandbox Code Playgroud)
它是一个 spring-boot 服务,并包含spring-boot-autoconfigureversion 中的依赖项2.2.5.RELEASE。客户端功能工作正常,但我们遇到了问题,执行器端点不再可以自由访问,但也受到 oauth 的保护。
这很令人困惑,因为我们刚刚包含了 spring-security-oauth2-client 依赖项,而不是资源服务器依赖项。
我们找到了这个适配器。我们不确定这是否是在服务器端添加安全性的唯一地方,但要禁用安全性,我们必须添加以下配置:
@Configuration
@Order(1)
class ManagedEndpointsAuthenticationConfig extends WebSecurityConfigurerAdapter {
private static final String NOOP_PASSWORD_PREFIX = "{noop}"
@Autowired
SecurityProperties properties
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/actuator/**")
.authorizeRequests()
.requestMatchers(EndpointRequest.to("info")).permitAll()
.requestMatchers(EndpointRequest.to("health")).permitAll()
.requestMatchers(EndpointRequest.to("prometheus")).permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
.and()
.httpBasic()
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
SecurityProperties.User user = properties.getUser()
List<String> roles = user.getRoles()
auth.inMemoryAuthentication()
.withUser(user.name)
.password(NOOP_PASSWORD_PREFIX + user.password)
.roles(StringUtils.toStringArray(roles))
}
}
Run Code Online (Sandbox Code Playgroud)
这对我们来说感觉像是一个奇怪的黑客,因为我们只想在客户端使用 oauth2。因此问题是:如何在客户端将 oauth2 与 spring-security 一起使用?
mar*_*rco -1
怎么样:
@Configuration
@EnableWebSecurity
class ManagedEndpointsAuthenticationConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1440 次 |
| 最近记录: |