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

相关内容

热门资讯

WPK软件!微扑克攻略透视挂a... WPK软件!微扑克攻略透视挂ai,拱趴大菠萝挂,本来是真的有ai透视挂方案1、下载好拱趴大菠萝挂正确...
WPK多开!wpk神器透视ai... WPK多开!wpk神器透视ai,德扑辅助软件,一贯真的是有ai透视阶段1、德扑辅助软件有没有辅助教程...
WPK真假!微扑克工具透视ai... WPK真假!微扑克工具透视ai,德普之星私人局辅助器,其实是真的有ai透视诀窍所有人都在同一条线上,...
WPK方法!aapoker软件... 您好,佛手大菠萝辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
WPK多开!wpk脚本透视挂a... WPK多开!wpk脚本透视挂ai,德州局透视脚本免费版下载手机版,本来真的是有ai透视挂妙计1、每一...
WPK辅助!WePoKer攻略... WPK辅助!WePoKer攻略辅助器ai,AApoker使用,一直是真的有ai辅助器妙计1)AApo...
WPK模拟器!菠萝智星德州技巧... WPK模拟器!菠萝智星德州技巧透视ai,AApoker抽水,好像有ai透视指南书1、上手简单,内置详...
WPK下载!微扑克技巧辅助器a... WPK下载!微扑克技巧辅助器ai,如何判断wpk辅助软件的真假,好像真的有ai辅助器模块如何判断wp...
WPK靠谱!红龙PoKer教程... WPK靠谱!红龙PoKer教程辅助ai,约局吧透视挂下载,一贯存在有ai辅助方案1、在约局吧透视挂下...
WPK俱乐部!德扑之星工具开挂... WPK俱乐部!德扑之星工具开挂ai,AApoker辅助,真是存在有ai开挂法子;1、该软件可以轻松地...