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

相关内容

热门资讯

终于发现!兴动互娱辅助脚本io... 终于发现!兴动互娱辅助脚本ios下载,佛手在线修改器(详细辅助解密教程)1、在兴动互娱辅助脚本ios...
透视有挂"拱趴大菠萝... 透视有挂"拱趴大菠萝有什么挂"详细辅助教你教程(往昔存在有挂)1、点击下载安装,微扑克wpk插件透视...
专业讨论!福建大玩家辅助透视,... 专业讨论!福建大玩家辅助透视,小程序游戏辅助必赢(详细辅助2025版教程);无聊就玩这款福建大玩家辅...
透视辅助"wepok... 《透视辅助"wepoker私人局俱乐部怎么进"详细辅助wpk教程(从来是有挂)》 wepoker私人...
重大推荐!多乐辅助器使用教程,... 重大推荐!多乐辅助器使用教程,大菠萝免费辅助器(详细辅助高科技教程)1、很好的工具软件,可以解锁游戏...
揭秘关于!温州茶苑手机版辅助器... 揭秘关于!温州茶苑手机版辅助器,心悦填大坑辅助器下载(详细辅助透牌教程)1、金币登录送、破产送、升级...
透视黑科技"德普之星... 1、透视黑科技"德普之星透视软件免费入口官网"详细辅助可靠教程(总是存在有挂)。2、德普之星透视软件...
让我来分享经验!潮汕掌上娱透视... 让我来分享经验!潮汕掌上娱透视怎么买,哈灵脚本微信小程序(详细辅助2025新版技巧)1、潮汕掌上娱透...
透视模拟器"hhpo... 1、透视模拟器"hhpoker真能买到挂吗"详细辅助高科技教程(果然是真的有挂)。2、hhpoker...
一分钟秒懂!微信小程序打哈儿脚... 一分钟秒懂!微信小程序打哈儿脚本,爱来辅助器(详细辅助科技教程),您好,微信小程序打哈儿脚本这款游戏...