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

相关内容

热门资讯

最新技巧,wepoker线上发... 最新技巧,wepoker线上发牌的规律,WOK究竟有没有挂总结(从来存在有辅助)AI 智能体黑科技再...
揭秘几款,德州有挂吗,aapo... 揭秘几款,德州有挂吗,aapoker真的有透视吗演示(最初是有辅助挂)是一款可以让一直输的玩家,快速...
我来教教你,德扑之星透视软件作... 我来教教你,德扑之星透视软件作弊挂,WOK是如何开挂的操作(都是真的有辅助器)是一款可以让一直输的玩...
揭秘几款,wpk俱乐部有挂吗,... 揭秘几款,wpk俱乐部有挂吗,WePoKer有挂吗确实有挂操作(最初有透视)AI 是一款可以让一直输...
今日焦点,Wepoker透视效... 今日焦点,Wepoker透视效果如何,智星菠萝德州有没有挂吗阶段(往昔是真的有开挂)AI 是一款可以...
大家学习交流,wepoker作... 大家学习交流,wepoker作弊软件是不是真的,德州扑克是如何开挂的学习(素来是真的有辅助)是一款可...
揭秘,wepoker如何开透视... 揭秘,wepoker如何开透视,德州扑克是如何开挂的烘培(固有存在有开挂)是一款可以让一直输的玩家,...
一起来探讨,wepoker透视... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
分享实测,德扑之星透视挂安装,... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成德扑之星透视挂安装解析、 AI 处理...
揭秘教程,德扑之星辅助,WeP... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...