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

相关内容

热门资讯

玩家实测!西兵互娱辅助(指尖四... 玩家实测!西兵互娱辅助(指尖四川小程序辅助)确实有作弊器开挂辅助下载(发现有挂);一、指尖四川小程序...
最新研发!樱花之盛免费挂(边锋... 最新研发!樱花之盛免费挂(边锋老友辅助)一向有作弊器开挂辅助黑科技(真的有挂)是一款可以让一直输的玩...
玩家爆料!九游破解辅助插件(多... 玩家爆料!九游破解辅助插件(多乐游戏辅助脚本)真是有作弊器开挂辅助黑科技(有挂详细);1、很好的九游...
盘点十款!樱花之盛辅助软件(随... 盘点十款!樱花之盛辅助软件(随意玩有辅助)总是有作弊挂开挂辅助脚本(有挂详情);小薇(透视辅助)致您...
总算了解!新超圣辅助器(欢乐达... 总算了解!新超圣辅助器(欢乐达人程序源码)原来有作弊器开挂辅助脚本(了解有挂);是一款可以让一直输的...
我来向大家传授!超凡辅助app... 我来向大家传授!超凡辅助app链接(xpoker可以作弊)一直有作弊器开挂辅助神器(了解有挂);超凡...
热点讨论!九游辅助破解版(po... 您好:pokemmo脚本辅助下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
新手必备!518互游辅助(ag... 新手必备!518互游辅助(agpoker辅助)从来有作弊器开挂辅助安装(有挂技巧);518互游辅助简...
一分钟揭秘!樱花之盛有挂(陕麻... 一分钟揭秘!樱花之盛有挂(陕麻圈插件开挂)切实有作弊器开挂辅助插件(有挂方针);详细陕麻圈插件开挂攻...
我来分享!新518互游脚本(哈... 我来分享!新518互游脚本(哈糖大菠萝攻略)确实有作弊挂开挂辅助工具(有挂教学)是一款可以让一直输的...