不同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

相关内容

热门资讯

一次性透视!aapoker破解... 一次性透视!aapoker破解侠是真的“推荐开挂辅助器”1、aapoker破解侠是真的系统规律教程、...
第一次性发现!指尖四川辅助脚本... 第一次性发现!指尖四川辅助脚本“解谜开挂辅助插件”亲,关键说明,指尖四川辅助脚本赛季回归,指尖四川辅...
六次性领会!指尖四川辅助脚本视... 六次性领会!指尖四川辅助脚本视频“揭幕开挂辅助教程”1、上手简单,内置详细流程视频教学,新手小白可以...
9次性普及!掌电竞技辅助器“解... 9次性普及!掌电竞技辅助器“解密开挂辅助插件”一、掌电竞技辅助器AI软件牌型概率发牌机制”必胜“技巧...
第8次性掌握!赣牌圈修改器“推... 第8次性掌握!赣牌圈修改器“推荐开挂辅助app”1、上手简单,内置详细流程视频教学,新手小白可以快速...
三次性了解!大菠萝789辅助“... 三次性了解!大菠萝789辅助“解迷开挂辅助神器”1、首先打开大菠萝789辅助最新版本,在大菠萝789...
第二次性私人局!欢聚水鱼神器“... 第二次性私人局!欢聚水鱼神器“曝光开挂辅助器”1、超多福利:超高返利,海量正版游戏,欢聚水鱼神器系统...
3次性理解!闲逸辅助软件“专业... 3次性理解!闲逸辅助软件“专业开挂辅助插件”闲逸辅助软件辅助器中分为三种模型:闲逸辅助软件软件透明挂...
第五次性掌握!皮皮游戏辅助工具... 第五次性掌握!皮皮游戏辅助工具“揭露开挂辅助教程”在进入皮皮游戏辅助工具辅助挂后,参与本局比赛的八名...
1次性晓得!沧海十三水私人局辅... 1次性晓得!沧海十三水私人局辅助器“分享开挂辅助器”沧海十三水私人局辅助器是一种具有地方特色的麻将游...