Spring Security中如何为不同的蚂蚁匹配器使用不同的身份验证提供程序?

时间:2019-10-18 15:22:25

标签: java spring-boot spring-security spring-security-oauth2

我正在尝试为Google OAuth2背后的应用程序配置Spring Security。

问题是我想通过Webhooks将来自Github和Bitbucket的某些API调用列入白名单,为此我编写了自定义身份验证提供程序。 我已经在春季安全链中添加了这些身份验证提供程序,但是这些调用似乎没有被调用。

对Google OAuth2的请求运行正常,我可以登录。但是,来自Github和Bitbucket的所有请求都将导致401,而不会影响身份验证提供程序。

这里真的需要一些帮助。

谢谢!

@Configuration
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

  @Autowired
  private OAuth2UserServiceImpl oAuth2UserService;

  @Autowired
  private GithubIpAuthenticationProvider githubIpAuthenticationProvider;

  @Autowired
  private BitbucketIpAuthenticationProvider bitbucketIpAuthenticationProvider;

  protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()
            .antMatchers("/api/*/applications/*/webhooks/github")
            .authenticated()
            .and()
            .authenticationProvider(githubIpAuthenticationProvider)
            .authorizeRequests()
            .antMatchers("/api/*/applications/*/webhooks/bitbucket")
            .authenticated()
            .and()
            .authenticationProvider(bitbucketIpAuthenticationProvider)
            .authorizeRequests()
            .antMatchers("/api/**")
            .authenticated()
            .and()
            .oauth2Login()
            .userInfoEndpoint()
            .userService(oAuth2UserService)
            .and()
            .and()
            .csrf().disable()
            .exceptionHandling()
            .authenticationEntryPoint(
                    (a,b,c) -> {b.sendError(HttpServletResponse.SC_UNAUTHORIZED);}
            )
            .and()
            .cors();
  }
}

0 个答案:

没有答案