简体   繁体   English

结合 OAuth2 与 http 基本认证 spring 安全

[英]Combining OAuth2 with http basic authentication spring security

I've implemented OAuth2 using spring boot and spring security.我已经使用 spring 引导和 spring 安全性实现了 OAuth2。 Now I've different set of APIs available and I want to use different authentication methods for it.现在我有不同的 API 可用,我想为它使用不同的身份验证方法。 For eg I want to use OAuth2 for /users/** apis and Http Basic Authentication for /admin/** APIs.例如,我想为/users/** ** API 使用 OAuth2,为/admin/** API 使用 Http 基本身份验证。

However, OAuth2 shouldn't work for /admin/** and HTTP basic shouldn't work for /users/** APIs.但是,OAuth2 不应该适用于/admin/**并且 HTTP basic 不应该适用于/users/** API。

Any help would be great!任何帮助都会很棒!

In Spring Security you can have multiple filter chains that handle different requests.在 Spring Security 中,您可以拥有多个过滤器链来处理不同的请求。 So you can have one that handles requests to the /users/** uri which will have the Basic Authentication filter, and one that handles requests to /admin/** uri which will have the Oauth2 filters.因此,您可以有一个处理对 /users/** uri 的请求,该 uri 将具有基本身份验证过滤器,另一个处理对 /admin/** uri 的请求,它将具有 Oauth2 过滤器。 To set this up, you need 2 instances of the WebSecurityConfigurerAdapter要进行此设置,您需要 2 个 WebSecurityConfigurerAdapter 实例

One for Oauth2一个用于 Oauth2

@Configuration
@Order(1)
public static class Oauth2ConfigurationAdapter extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception {
       http.mvcMatcher("/user/**")
       ...... 

And another for Basic:还有一个基本的:

@Configuration
@Order(2)
public static class BasicConfigurationAdapter extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity http) throws Exception {
       http.mvcMatcher("/admin/**")
       ...... 

This article explains it in more detail: https://www.baeldung.com/spring-security-multiple-entry-points本文更详细地解释它: https://www.baeldung.com/spring-security-multiple-entry-points

Also this code does something similar with Digest auth for /admin and Basic for all other.此外,此代码与 /admin 的 Digest auth 和所有其他的 Basic 执行类似的操作。 https://github.com/wlesniak/spring-security-authn-authz-course/tree/master/module_2/mod2_crypto_portfolio_digest https://github.com/wlesniak/spring-security-authn-authz-course/tree/master/module_2/mod2_crypto_portfolio_digest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM