Spring安全性-OAuth2和CustomAuthenticationProvider。如何为每个URL配置不同的URL模式?

Spring安全性-OAuth2和CustomAuthenticationProvider。如何为每个URL配置不同的URL模式?,spring,spring-boot,spring-security,spring-security-oauth2,Spring,Spring Boot,Spring Security,Spring Security Oauth2,我的项目有两个身份验证提供程序:Google OAuth2客户端(OAuth2 starter依赖项)和第二个自定义的身份验证提供程序 我有两个antMatcher:/api/**和/app/** 是否可以使用OAuth2授权/app/**,使用我的自定义身份验证提供商授权/api/** 因为我不希望为RESTAPI启用OAuth2,但希望为应用程序的其余部分启用OAuth SSO 如何为不同的身份验证提供程序指定不同的URL模式 编辑 遵循我的配置(Spring Boot 2.0.2): 尝试

我的项目有两个身份验证提供程序:Google OAuth2客户端(OAuth2 starter依赖项)和第二个自定义的
身份验证提供程序

我有两个
antMatcher
/api/**
/app/**

是否可以使用OAuth2授权
/app/**
,使用我的自定义身份验证提供商授权
/api/**

因为我不希望为RESTAPI启用OAuth2,但希望为应用程序的其余部分启用OAuth SSO

如何为不同的身份验证提供程序指定不同的URL模式

编辑

遵循我的配置(Spring Boot 2.0.2):


尝试了不同的配置,但均无效

AuthenticationProvider有一个方法:支持(类身份验证) 它接受身份验证令牌,如果返回false,则AuthenticationManager将不会调用该提供程序

因此,您可以在身份验证令牌中放入一个自定义字段,以指示正在调用哪个URI。身份验证接口有一个getDetails()方法,该方法返回一个对象,前提是您可以提供附加信息

为此,您需要创建自定义AuthenticationDetails和AuthenticationDetailsSource,您可以扩展WebAuthenticationDetails和WebAuthenticationDetailsSource。
WebAuthenticationDetailsSource有一个buildDetails方法,允许您访问HttpServletRequest。

由于您有两个身份验证提供程序,因此需要配置两个身份验证管理器。以下是一个示例XML配置,供您参考:

<security:authentication-manager id="appAuthenticationManager">
    <security:authentication-provider ref="appAuthenticationProvider"/>
</security:authentication-manager>

<security:authentication-manager id="apiAuthenticationManager">
    <security:authentication-provider ref="apiAuthenticationProvider"/>
</security:authentication-manager>
最后一步是注册这些过滤器bean:

<bean id="appServerSecurityFilterRegistration" class="org.springframework.boot.web.servlet.FilterRegistrationBean">
    <property name="filter" ref="appSecurityInterceptorFilter"/>
    <property name="enabled" value="false"/>
</bean>
创建一个空筛选器链以绕过
/api/**
请求的所有筛选器

<bean id="apiRequestMatcher" class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
    <constructor-arg index="0" value="/api/**"/>
</bean>
<bean id="apiFilterChain" class="org.springframework.security.web.DefaultSecurityFilterChain">
    <constructor-arg name="requestMatcher" ref="apiRequestMatcher"/>
    <constructor-arg name="filters">
        <list/>
    </constructor-arg>
</bean>

最后,将其注册到过滤器链代理

<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
    <constructor-arg>
        <list>
            <ref bean="apiFilterChain"/>
        </list>
    </constructor-arg>
</bean>

要将这些请求委托给您的自定义提供者,请遵循我前面共享的步骤


您也可以尝试,
绕过过滤器链。Spring 3.1将
filters=“none”
替换为
security=“none”

谢谢。我可以在我的自定义身份验证提供程序上实现这一点,但如何禁用OAuth的某些URL模式?您是否考虑过设置两个单独的筛选器链?我过去就是这样做的。如果创建两个实现WebSecurity的安全类,则ConfigureAdapter spring将配置两个筛选器链。一个过滤器链可以用于/api和there/app。它保持了事物的良好性和独立性,您可以使用@Order注释来确定应该首先对哪一个进行求值。Spring将针对uri尝试每个筛选器链,直到有匹配项,然后再不匹配任何其他筛选器链。我尝试了两个独立的过滤器链,但似乎oauth过滤器正在全球应用。将oauth2提供程序限制为antMatcher e的一个子集,让其他人使用定制jwt身份验证提供程序的antMatcher是行不通的。oauth2提供程序仍然处理
/api/**
匹配器。有没有办法让oauth2提供程序完全忽略
/api/**
匹配器,而spring security将此匹配器委托给CustomProvider?但是现在对
/api/**
的所有请求都是不安全的,这就是它要做的。我在编辑之前共享的步骤应该可以完成预期的工作。现在,我想人们在不检查身份验证配置的情况下很难帮助您。仅仅通过web安全配置是不可能解决问题的。请您尝试一下配置并分享一个具体的问题。您能添加OAuth2和自定义身份验证配置吗?还显示如何注册筛选器、身份验证管理和提供程序。可能是某些设置不正确。您的spring安全oauth和spring启动版本是什么?@user2683814我已经编辑了我的问题,并提供了更多详细信息
<bean id="apiRequestMatcher" class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
    <constructor-arg index="0" value="/api/**"/>
</bean>
<bean id="apiFilterChain" class="org.springframework.security.web.DefaultSecurityFilterChain">
    <constructor-arg name="requestMatcher" ref="apiRequestMatcher"/>
    <constructor-arg name="filters">
        <list/>
    </constructor-arg>
</bean>
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
    <constructor-arg>
        <list>
            <ref bean="apiFilterChain"/>
        </list>
    </constructor-arg>
</bean>