加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:17:18 所属栏目:Java 来源:网络整理
导读:我有一个带有OAuth2授权和资源服务器的 spring boot设置.用户可以通过向/ oauth / token发出POST请求来获取令牌.到现在为止还挺好. 但是,我不想通过BASIC auth保护/ oauth / token,而是通过自定义安全过滤器. 我尝试了以下内容,但从未调用过DemoAuthenticati
我有一个带有OAuth2授权和资源服务器的 spring boot设置.用户可以通过向/ oauth / token发出POST请求来获取令牌.到现在为止还挺好.

但是,我不想通过BASIC auth保护/ 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());
    }

    //....
}

这个对我有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读