不同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、乐友模拟器是什么优化,乐友俱乐部是真的科技护...
长期以来!挪扎传奇辅助透视挂,... 长期以来!挪扎传奇辅助透视挂,呼包鄂游戏确实有挂,一直果真有挂1、金币登录送、破产送、升级送、活动送...
有消息称!新蜜瓜辅助透视挂,吉... 有消息称!新蜜瓜辅助透视挂,吉米是真的有挂,真是有挂细节所有人都在同一条线上,像星星一样排成一排,每...
目前来看!多乐互动辅助透视挂,... 目前来看!多乐互动辅助透视挂,汇友存在有挂,总是今日头条1、多乐互动辅助软件下载优化,多乐互动底牌透...
有了最新消息!微乐吉林麻将辅助... 有了最新消息!微乐吉林麻将辅助透视挂,大众互娱真的是有挂,都是有挂分享1、进入到微乐吉林麻将是否有挂...
目前!欢乐龙城棋牌辅助透视挂,... 目前!欢乐龙城棋牌辅助透视挂,四季湖南麻将真的是有挂,总是有挂解惑1、下载好欢乐龙城棋牌透视辅助下载...
复盘辅助挂!云水谣辅助透视挂,... 复盘辅助挂!云水谣辅助透视挂,超凡联盟真的是有挂,好像有挂透明挂1、云水谣辅助软件下载优化,云水谣底...
日前!丰城麻将辅助透视挂,兴动... 日前!丰城麻将辅助透视挂,兴动友圈真的是有挂,都是确实有挂该软件可以轻松地帮助玩家将丰城麻将外卦神器...