要实现不同OAuth客户端的多个Springfox 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();
}
}
@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();
}
}
}
ClientResources
类来保存每个OAuth客户端的配置属性。public class ClientResources {
private String clientId;
private String clientSecret;
private String accessTokenUri;
private String userAuthorizationUri;
private String redirectUri;
// getters and setters
}
security:
oauth2:
client1:
clientId: client1-id
clientSecret: client1-secret
accessTokenUri: https