gpt4 book ai didi

java - 带有自定义安全过滤器的 Spring Boot OAuth2

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:28 25 4
gpt4 key购买 nike

我有一个带有 OAuth2 授权和资源服务器的 spring boot 设置。用户可以通过向 /oauth/token 发出 POST 请求来获取 token 。到目前为止,还不错。

但是,我不想通过 BASIC 身份验证保护 /oauth/token,而是通过自定义安全过滤器。

我尝试了以下方法,但从未调用过 DemoAuthenticationFilter:

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
// ...
@Override
public void configure(HttpSecurity http) throws Exception {
// ...
http.addFilterBefore(new DemoAuthenticationFilter(), BasicAuthenticationFilter.class);
http.authorizeRequests().antMatchers("/oauth/token").authenticated();
}
}

此外,如果我尝试将其添加到 WebSecurityConfigurerAdapter,则仅在请求通过 OAuth2 进行身份验证后 才调用过滤器:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
protected void configure(HttpSecurity http) throws Exception {
// ...
http.addFilterBefore(new DemoAuthenticationFilter(), BasicAuthenticationFilter.class);
http.authorizeRequests().antMatchers("/oauth/token").authenticated();
}
}

如何实现这一点的一些简单示例将非常有帮助。谢谢!

最佳答案

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.allowFormAuthenticationForClients().addTokenEndpointAuthenticationFilter(new AligenieFilter());
}

//....
}

它对我有用。

关于java - 带有自定义安全过滤器的 Spring Boot OAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40345889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com