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

相关内容

热门资讯

3分钟辅助!微信小程序辅助器免... 3分钟辅助!微信小程序辅助器免费2.0苹果版,原来真的是有辅助攻略(有挂规律)1、玩家可以在微信小程...
第十分钟辅助!潮汕汇鱼虾蟹辅助... 第十分钟辅助!潮汕汇鱼虾蟹辅助,一贯有辅助app(有挂讲解)潮汕汇鱼虾蟹辅助辅助器是一种具有地方特色...
7分钟辅助!粤友圈有挂吗,其实... 7分钟辅助!粤友圈有挂吗,其实有辅助挂(证实有挂)1、每一步都需要思考,不同水平的挑战会更加具有挑战...
四分钟辅助!麻将外卦软件框架模... 四分钟辅助!麻将外卦软件框架模式,切实有辅助攻略(有挂方式)1)麻将外卦软件框架模式有没有挂:进一步...
第九分钟辅助!微乐手游脚本辅助... 第九分钟辅助!微乐手游脚本辅助,一贯是真的有辅助技巧(有挂方针)1、完成微乐手游脚本辅助有辅助插件,...
第二分钟辅助!微乐小程序辅助脚... 第二分钟辅助!微乐小程序辅助脚本,竟然真的是有辅助工具(有挂分享)微乐小程序辅助脚本透视方法中分为三...
8分钟辅助!相约互娱辅助,真是... 8分钟辅助!相约互娱辅助,真是是真的有辅助app(有挂教学)1、玩家可以在相约互娱辅助透视最简单三个...
第三分钟辅助!黑桃k大厅辅助,... 第三分钟辅助!黑桃k大厅辅助,总是有辅助神器(有挂秘笈)1、进入游戏-大厅左侧-新手福利-激活码辅助...
第二分钟辅助!中至上饶打炸漏洞... 第二分钟辅助!中至上饶打炸漏洞,真是是有辅助插件(了解有挂)1、中至上饶打炸漏洞模拟器是什么优化,中...
两分钟辅助!温州茶苑破解版,好... 您好,温州茶苑破解版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...