【问题标题】:how to implement AuthenticationEntryPoint and AuthenticationProvider together in spring security如何在spring security中同时实现AuthenticationEntryPoint和AuthenticationProvider
【发布时间】:2012-12-23 13:07:59
【问题描述】:

您好 Spring Security 专家。

我的要求。

我有两组用户界面。一组是登录和注销,需要使用基本身份验证(使用用户名密码凭据)由 spring security 保护。我使用HybridAuthenticationProvider 实现AuthenticationProvider 并实现了它。

需要通过在 HTTP Header 中传递令牌来支持第二个和其余 UI。我使用CustomAuthenticationEntryPoint 实现AuthenticationEntryPoint + GenericFilterBean 并且可以实现它。

现在我想制作单个 spring-security.xml 来实现以上两个功能。最终,我结合了一组 UI 页面,其中我想通过凭据 (AuthenticationProvider) 保护登录/注销页面和我想用令牌保护的其余 UI (AuthenticationEntryPoint)。

当我将所有内容放在 spring-security.xml(如下所述)中时,出现以下异常。

例外:

例外是 org.springframework.beans.factory.BeanCreationException: 创建具有名称的 bean 时出错 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': 无法解析对 bean 的引用 'org.springframework.security.authentication.ProviderManager#0' 同时 设置 bean 属性“authenticationManager”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean 'org.springframework.security.authentication.ProviderManager#0':可以 不解析匹配的构造函数(提示:指定索引/类型/名称 简单参数的参数以避免类型歧义)在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

示例 Spring-security.xml

<security:http auto-config="true" authentication-manager-ref="hybridAuthenticationProvider">
    <security:intercept-url pattern="/auth/login" access="ROLE_USER" />
</security:http>

<security:http realm="Protected API" use-expressions="true"
    auto-config="false" create-session="stateless" entry-point-ref="CustomAuthenticationEntryPoint">
    <security:custom-filter ref="authenticationTokenProcessingFilter"
        position="FORM_LOGIN_FILTER" />
    <security:intercept-url pattern="/welcome"
        access="isAuthenticated()" />
</security:http>


<bean id="CustomAuthenticationEntryPoint"
    class="com.ckatudia.tutorial.authentrypoint.CustomAuthenticationEntryPoint" />

<bean id="authenticationTokenProcessingFilter"
   class="com.ckatudia.tutorial.authentrypoint.AuthenticationTokenProcessingFilter" />

<bean id="TokenUtils"
    class="com.ckatudia.tutorial.authentrypoint.TokenUtils" />

<bean id="authenticationManager"
    class="com.ckatudia.tutorial.auth.TokenAuthenticationProvider" />

<bean id="hybridAuthenticationProvider"
      class="com.ckatudia.tutorial.auth.HybridAuthenticationProvider">
</bean>  
    
<security:authentication-manager>
    <security:authentication-provider ref="hybridAuthenticationProvider"/>
</security:authentication-manager>

我删除了 authentication-manager-ref="hybridAuthenticationProvider" 然后我在部署时遇到了以下异常。

例外:

org.springframework.beans.factory.BeanCreationException: 错误 用名字创建bean 'org.springframework.security.filterChainProxy':初始化的调用 方法失败;嵌套异常是 java.lang.IllegalArgumentException: 通用匹配模式 ('/**') 在其他模式之前定义 过滤器链,导致它们被忽略。请检查 在你的 security:http 命名空间或 FilterChainProxy bean 中排序 配置

【问题讨论】:

    标签: spring authentication spring-security


    【解决方案1】:

    您有这个例外,因为 http 元素的 de“模式”属性默认为“/**”。由于您有两个没有特定模式的“http”元素,因此采用默认值,它们每个都覆盖相同的 uis 集。

    尝试在两者上添加不同的模式:

    <http pattern="/foo/**" ... >
      ...
    </http>
    
    <http pattern="/bar/**" ... >
      ...
    </http>
    

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 2016-07-19
      • 2019-10-10
      • 2018-09-16
      • 1970-01-01
      • 2011-01-20
      • 2011-12-22
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多