不同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、涡阳茶馆有没有辅助教程、涡...
据了解开挂!新蛮王辅助透视挂,... 据了解开挂!新蛮王辅助透视挂,莲花麻将是有外挂,都是真是有挂1)新蛮王辅助插件:进一步探索新蛮王辅助...
今日科普透视挂!吉利比鸡辅助透... 今日科普透视挂!吉利比鸡辅助透视挂,心动休闲是真的有外挂,本来有挂工具1、全新机制【吉利比鸡ai辅助...
一分钟了解辅助!新九五辅助透视... 一分钟了解辅助!新九五辅助透视挂,微乐云南麻将确实有外挂,竟然果真有挂该软件可以轻松地帮助玩家将微乐...
据了解开挂!青云阁辅助透视挂,... 据了解开挂!青云阁辅助透视挂,闽南漳州麻将存在有外挂,好像有挂实锤1、这是跨平台的青云阁轻量版有透视...
一分钟揭秘开挂!优乐扑克辅助透... 一分钟揭秘开挂!优乐扑克辅助透视挂,鼎圣麻将真的有外挂,竟然有挂方式小薇(辅助器软件下载)致您一封信...
一起来探讨透视挂!旺旺福建麻将... 一起来探讨透视挂!旺旺福建麻将辅助透视挂,老胡麻将真的有外挂,切实有挂方法旺旺福建麻将能透视中分为三...
玩家爆料辅助!樱花之盛辅助透视... 玩家爆料辅助!樱花之盛辅助透视挂,聚闲麻将是有外挂,原来有人有挂1、下载好樱花之盛脚本下载之后点击打...
值得注意的是透视挂!星星武汉麻... 值得注意的是透视挂!星星武汉麻将辅助透视挂,三晋娱乐是有外挂,竟然有挂存在1、不需要AI权限,帮助你...