不同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透视辅助版本稳定性对比与推荐‌:德扑自定义!德扑线上创建房间,德扑之星ai代打原来是有...
德扑之星比赛!德扑软件决策,德... 德扑之星比赛!德扑软件决策,德扑开发真是是真的有挂(详细ai代打教程);亲真的是有正版授权,小编(透...
德州ai辅助软件!德扑之星开桌... 德州ai辅助软件!德扑之星开桌怎么设置,德扑之星ai软件其实有挂(详细功能教程);德州ai辅助软件软...
aapoker透明挂!wepo... 【福星临门,好运相随】;aapoker透明挂!wepoke有软件吗(Wepoke是真的)本来是真的有...
德扑ai软件!德扑人工智能软件... 德扑ai软件!德扑人工智能软件,德扑之星内部都是真的有挂(详细有挂吗教程);超受欢迎的稳赢小游戏,经...
微扑克有辅助挂!微扑克有辅助挂... 微扑克有辅助挂!微扑克有辅助挂吗,(微扑克苹果版)都是存在有挂(详细有辅助挂教程);是一款可以让一直...
we辅助poker德之星!wo... 1、we辅助poker德之星!wopoker游戏辅助器(wepoke)本来存在有挂(详细透明挂教程)...
红龙扑克辅助!红龙扑克是正规的... 红龙扑克辅助!红龙扑克是正规的吗,(红龙扑克)确实真的有挂(详细辅助教程);是一款可以让一直输的玩家...