不同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私人局开挂视频,德扑之星透视软件免费入口官网,一贯确实有挂(有挂解惑)1、上手...
三分钟了解!填大坑辅助器攻略,... 三分钟了解!填大坑辅助器攻略,同乡游辅助工具,详细神器(有挂规律)1、许多玩家不知道填大坑辅助器攻略...
2026版软件!wepoker... 2026版软件!wepoker免费脚本弱密码,德普之星辅助器,确实确实有挂(有挂工具)1、该软件可以...
总算了解!乐酷副厅辅助,闲逸辅... 总算了解!乐酷副厅辅助,闲逸辅助器下载,详细软件(有挂透视)1、总算了解!乐酷副厅辅助,闲逸辅助器下...
网友热议!wepoker辅助器... 网友热议!wepoker辅助器免费,德扑之心免费透视,确实是真的有挂(有挂透明挂)网友热议!wepo...
实测交流!广东雀神挂件脚本开挂... 实测交流!广东雀神挂件脚本开挂,新畅游辅助器,详细教程(有挂工具)广东雀神挂件脚本开挂是不是有人用挂...
今天下午!fishpoker透... 今天下午!fishpoker透视底牌,德普之星辅助器app,果然存在有挂(存在有挂)德普之星辅助器a...
重大消息!来几局辅助器开挂,链... 重大消息!来几局辅助器开挂,链接大厅辅助插件有哪些,详细脚本(有挂方法)1、该软件可以轻松地帮助玩家...
黑科技辅助!hh poker软... 黑科技辅助!hh poker软件,德普辅助软件,一直真的是有挂(有挂方式)1、上手简单,内置详细流程...
终于清楚!吉祥填大坑辅助器攻略... 终于清楚!吉祥填大坑辅助器攻略,博乐填大坑有没有buff,详细挂(有挂教程)亲,关键说明,吉祥填大坑...