不同oauth客户端的多个Springfox SecurityConfigurations
创始人
2025-01-09 12:30:27
0

要实现不同OAuth客户端的多个Springfox SecurityConfigurations,您可以按照以下步骤进行操作:

  1. 创建多个SecurityConfigurations类:根据您的需要,为每个OAuth客户端创建一个单独的SecurityConfigurations类。每个类都应继承自WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
public class SecurityConfigurations1 extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/endpoint1").hasRole("ROLE_USER1")
            .and()
            .oauth2Login();
    }
}
@Configuration
@EnableWebSecurity
public class SecurityConfigurations2 extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/endpoint2").hasRole("ROLE_USER2")
            .and()
            .oauth2Login();
    }
}
  1. 在每个SecurityConfigurations类中配置OAuth客户端:使用@EnableOAuth2Client注解为每个SecurityConfigurations类启用OAuth2客户端。然后使用@ConfigurationProperties注解将OAuth2客户端的配置属性注入到配置类中。
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class SecurityConfigurations1 extends WebSecurityConfigurerAdapter {

    @Autowired
    private OAuth2ClientContext oauth2ClientContext;

    @Bean
    @ConfigurationProperties("security.oauth2.client1")
    public ClientResources clientResources1() {
        return new ClientResources();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/endpoint1").hasRole("ROLE_USER1")
            .and()
            .oauth2Login()
            .clientConfigurations(clientResources1());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user1").password(passwordEncoder().encode("password")).roles("USER1");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConfigurationProperties("security.oauth2.client2")
    public ClientResources clientResources2() {
        return new ClientResources();
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/endpoint1")
                .authorizeRequests().anyRequest().authenticated();
        }
    }
}
@Configuration
@EnableWebSecurity
@EnableOAuth2Client
public class SecurityConfigurations2 extends WebSecurityConfigurerAdapter {

    @Autowired
    private OAuth2ClientContext oauth2ClientContext;

    @Bean
    @ConfigurationProperties("security.oauth2.client2")
    public ClientResources clientResources2() {
        return new ClientResources();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/api/endpoint2").hasRole("ROLE_USER2")
            .and()
            .oauth2Login()
            .clientConfigurations(clientResources2());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("user2").password(passwordEncoder().encode("password")).roles("USER2");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/endpoint2")
                .authorizeRequests().anyRequest().authenticated();
        }
    }
}
  1. 创建ClientResources类:创建一个ClientResources类来保存每个OAuth客户端的配置属性。
public class ClientResources {

    private String clientId;
    private String clientSecret;
    private String accessTokenUri;
    private String userAuthorizationUri;
    private String redirectUri;

    // getters and setters

}
  1. 在application.yml文件中配置OAuth客户端的属性:
security:
  oauth2:
    client1:
      clientId: client1-id
      clientSecret: client1-secret
      accessTokenUri: https

相关内容

热门资讯

今年以来!模拟器打开hhpok... 今年以来!模拟器打开hhpoker,德普之星私人局透视,确实真的是有挂(有人有挂)1)德普之星私人局...
据文件显示!大菠萝免费辅助,德... 据文件显示!大菠萝免费辅助,德普之星透视免费,本来确实有挂(有挂规律);1、上手简单,内置详细流程视...
2026版复盘!wepoker... 2026版复盘!wepoker可以免费玩吗,德扑之星私人局透视,好像存在有挂(有挂讲解)在进入德扑之...
现有说明如下!epoker底牌... 现有说明如下!epoker底牌透视,德扑之星怎么开辅助,一贯确实有挂(有挂讲解)1)德扑之星怎么开辅...
据权威媒体报道!pokerma... 据权威媒体报道!pokermaster破解版,德扑软件,一直真的是有挂(有挂细节)1、据权威媒体报道...
2026版技巧!agpoker... 2026版技巧!agpoker辅助,德普之星透视免费,好像真的是有挂(有挂讲解);该软件可以轻松地帮...
第三方技巧!德州透视竞技联盟,... 第三方技巧!德州透视竞技联盟,德扑之心免费透视,都是是有挂(有挂分析)1、任何德扑之心免费透视透视是...
截至发稿!wpk安卓下载辅助,... 截至发稿!wpk安卓下载辅助,德普之星辅助软件,确实是真的有挂(有挂存在)1、在德普之星辅助软件插件...
这一问题亟待解决!微扑克辅助,... 这一问题亟待解决!微扑克辅助,智星德州插件最新版本更新内容详解,其实是真的有挂(有挂规律)1、智星德...
明白辅助挂!hardrock透... 明白辅助挂!hardrock透视工具,德普之星的辅助工具介绍,总是存在有挂(有挂秘诀)1)德普之星的...