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

相关内容

热门资讯

玩家必看攻略辅助器app!温岭... 玩家必看攻略辅助器app!温岭逸趣麻将多功能辅助器app,温岭逸趣麻将切实是有辅助器app1)温岭逸...
一分钟了解透视挂方法!喜乐多功... 一分钟了解透视挂方法!喜乐多功能透视挂方法,喜乐本来真的是有透视挂方法小薇(辅助器软件下载)致您一封...
安装程序教程辅助脚本!天天麻将... 安装程序教程辅助脚本!天天麻将九州多功能辅助脚本,天天麻将九州好像是有辅助脚本1、让任何用户在无需天...
普及知识开挂教程!中至余干麻将... 普及知识开挂教程!中至余干麻将多功能开挂教程,中至余干麻将果然是有开挂教程1、中至余干麻将免费脚本咨...
盘点一款辅助教程!君子道多功能... 盘点一款辅助教程!君子道多功能辅助教程,君子道总是是真的有辅助教程亲,关键说明,君子道透视脚本安卓赛...
玩家科普辅助器方法!天津麻将多... 玩家科普辅助器方法!天津麻将多功能辅助器方法,天津麻将总是有辅助器方法1、玩家可以在天津麻将透视最简...
玩家必看辅助器软件!对战互娱多... 玩家必看辅助器软件!对战互娱多功能辅助器软件,对战互娱一直有辅助器软件暗藏猫腻,小编详细说明对战互娱...
今日焦点开挂插件!乐游互娱多功... 今日焦点开挂插件!乐游互娱多功能开挂插件,乐游互娱本来真的是有开挂插件1、许多玩家不知道乐游互娱辅助...
盘点一款外挂教程!小白大作战多... 盘点一款外挂教程!小白大作战多功能外挂教程,小白大作战切实真的是有外挂教程1、小白大作战有没有辅助教...
重大通报辅助方法!边锋老友棋牌... 重大通报辅助方法!边锋老友棋牌多功能辅助方法,边锋老友棋牌确实真的是有辅助方法边锋老友棋牌破解侠是真...