不同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、吉祥麻将辅助软件下载优化,吉祥麻...
我来教大家!PX在线透视挂脚本... 我来教大家!PX在线透视挂脚本软件,PX在线好像真的是有透视挂详情工具PX在线透视方法中分为三种模型...
今日百科!天天遇难麻将透视脚本... 今日百科!天天遇难麻将透视脚本软件,天天遇难麻将总是有透视科普工具1、操作简单,无需天天遇难麻将手机...
科普常识!爱玩麻将辅助脚本软件... 科普常识!爱玩麻将辅助脚本软件,爱玩麻将其实存在有辅助揭幕app1、实时爱玩麻将透视辅助更新:用户可...
我来教教大家!云雀麻将辅助器脚... 我来教教大家!云雀麻将辅助器脚本软件,云雀麻将竟然存在有辅助器关于脚本1、云雀麻将公共底牌简单,云雀...
关于!雀友汇透视脚本软件,雀友... 关于!雀友汇透视脚本软件,雀友汇真是真的是有透视解迷方法1、下载好雀友汇脚本下载之后点击打开,先需要...
玩家必用!乐友透视脚本软件,乐... 玩家必用!乐友透视脚本软件,乐友果然是真的有透视普及方法1、完成乐友有辅助插件,帮助玩家取得所有比赛...
总算明白!云烟休闲透视脚本软件... 总算明白!云烟休闲透视脚本软件,云烟休闲本来存在有透视有挂app1、下载好云烟休闲正确养号方法之后点...