Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';Spring Security X.509身份验证中的t故障处理工作?_Java_Spring_Spring Security - Fatal编程技术网

Java 为什么';Spring Security X.509身份验证中的t故障处理工作?

Java 为什么';Spring Security X.509身份验证中的t故障处理工作?,java,spring,spring-security,Java,Spring,Spring Security,我有一个带有X509过滤器的自定义过滤器(X509AuthenticationFilter的子类)。X509筛选器的唯一任务是从请求中提取证书。我有一个相应的AuthenticationProvider,如果凭据无效,它会抛出BadCredentialsException。我的意图是,当AuthenticationProvider抛出BadCredentialsException时,BadCredentialsException将请求转发到message.jsp。但是,它没有这样做。调试时,我注

我有一个带有X509过滤器的自定义过滤器(X509AuthenticationFilter的子类)。X509筛选器的唯一任务是从请求中提取证书。我有一个相应的
AuthenticationProvider
,如果凭据无效,它会抛出
BadCredentialsException
。我的意图是,当
AuthenticationProvider
抛出
BadCredentialsException
时,
BadCredentialsException
将请求转发到
message.jsp
。但是,它没有这样做。调试时,我注意到异常被抛出、捕获并存储在WebAttributes下。。。但是其他Spring安全类似乎都没有利用这个异常,我也找不到任何方法来使用故障处理程序

<security:http auto-config="false" pattern="/role/**" access-decision-manager-ref="adm" entry-point-ref="http403EntryPoint">
        <security:anonymous enabled="false"/>
        <security:access-denied-handler error-page="/message.jsp"/>
        <security:custom-filter ref="authFilter" position="PRE_AUTH_FILTER" />
</security:http>

<bean id="http403EntryPoint" class="..." />

<bean id="authFilter" class="...">
   <property name="authenticationManager" ref="authenticationManager" />
   <property name="continueFilterChainOnUnsuccessfulAuthentication" value="false" />
</bean>

我尝试了
authFilter
的PRE_AUTH和X509过滤器选项。我还尝试了
authFilter
使用和不使用
continueFilterChainOnUnsuccessfulAuthentication
选项。两者都没有任何区别,spring security不会处理错误

我想要的是:


一种使用spring安全框架提供的错误处理进行X509身份验证的方法。我不在乎它是否被认为是“预授权”,尽管我认为Spring Security可能要求将过滤器放置在预授权或X509位置。我想了解如何使此功能正常工作的提示或查看内容的详细信息。

通常在Spring Security中,由于未捕获的
身份验证异常而重定向到页面(它
BadCredentialsException
扩展)的工作方式如下:

<security:http auto-config="false" pattern="/role/**" entry-point-ref="failureMessageEntryPoint">
    <security:anonymous enabled="false"/>
    <security:custom-filter ref="authFilter" position="PRE_AUTH_FILTER"/> <!-- authenticationManager will throw a BadCredentialsException, resulting in a null Authentication for the request -->
    <security:intercept-url pattern="/role/**" access="isAuthenticated"/> <!-- will reject requests with a null Authentication by throwing an AuthenticationCredentialsNotFoundException -->
                                                                          <!-- AuthenticationExceptions will be caught by auto-configured ExceptionTranslationFilter, and sent to entry point -->
</security:http>

<bean id="failureMessageEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <!-- not actually for login, but will work as an entry point that redirects to a page -->
    <constructor-arg value="/message.jsp"/>
</bean>

<bean id="authFilter" class="...">
    <property name="authenticationManager" ref="authenticationManager"/>
    <!-- let continueFilterChainOnUnsuccessfulAuthentication default to true to allow the intercept-url to reject the request -->
</bean>
  • FilterSecurityInterceptor
    抛出一个
    AuthenticationException
  • AuthenticationException
    ExceptionTranslationFilter
    捕获
  • ExceptionTranslationFilter
    将请求发送到
    入口点ref
    进行身份验证
  • 这一事件循环发生在附近。但是,您的自定义
    X509AuthenticationFilter
    AuthenticationProvider
    正在链中更早地验证证书。要获取要重定向的失败证书验证,请执行以下操作:

  • 让您的
    AuthenticationManager
    /
    AuthenticationProvider
    抛出
    BadCredentialsException
    ,就像您当前所做的那样
  • 允许您的自定义
    X509AuthenticationFilter
    将与请求相关联的
    身份验证
    设置为空(这应该是它从
    AbstractPreAuthenticationdProcessingFilter
    继承的行为)
  • 稍后在链中安装一个单独的
    过滤器安全interceptor
    ,该链会对没有
    身份验证的请求引发
    身份验证异常
    ,从而将请求发送到入口点
  • 这可以通过以下方式完成:

    <security:http auto-config="false" pattern="/role/**" entry-point-ref="failureMessageEntryPoint">
        <security:anonymous enabled="false"/>
        <security:custom-filter ref="authFilter" position="PRE_AUTH_FILTER"/> <!-- authenticationManager will throw a BadCredentialsException, resulting in a null Authentication for the request -->
        <security:intercept-url pattern="/role/**" access="isAuthenticated"/> <!-- will reject requests with a null Authentication by throwing an AuthenticationCredentialsNotFoundException -->
                                                                              <!-- AuthenticationExceptions will be caught by auto-configured ExceptionTranslationFilter, and sent to entry point -->
    </security:http>
    
    <bean id="failureMessageEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <!-- not actually for login, but will work as an entry point that redirects to a page -->
        <constructor-arg value="/message.jsp"/>
    </bean>
    
    <bean id="authFilter" class="...">
        <property name="authenticationManager" ref="authenticationManager"/>
        <!-- let continueFilterChainOnUnsuccessfulAuthentication default to true to allow the intercept-url to reject the request -->
    </bean>
    
    
    
    为您解答两个相关问题。关于过滤器排序()的Spring安全部分说ExceptionTranslationFilter应该在“身份验证处理机制”之后。这与你的回答不矛盾吗?第二个问题:spring教程说使用http403EntryPoint。但是在AuthenticationException(或者根本不需要)的情况下,http403EntryPoint实际上从未被调用。您是否同意不需要http403EntryPoint?我同意不需要
    http403EntryPoint
    。它只是一个占位符入口点,因为通常对于X509证书,身份验证已经在Spring Security之外进行,因此没有身份验证的入口点。是的,在
    ExceptionTranslationFilter
    之后有一个auth机制有点不太好。我已经修改了我的答案,将auth机制保留在
    例外TranslationFilter
    之前,并使用一个单独的筛选器来强制重定向。感谢深思熟虑的回答。我喜欢这种方法。我缺少的一点是,如果存在空身份验证主体,截取url将抛出AuthenticationException。Spring安全团队的设计选择似乎很奇怪,FilterSecurityInterceptor忽略了AbstractPreAuthenticatedProcessingFilter在unsuccessfulAuthentication方法中捕获并存储在WebAttributes.AUTHENTICATION\u异常密钥下的先前异常。同意,当
    WebAttributes.AUTHENTICATION\u EXCEPTION
    不为空时,在链中的某个位置出现自动故障将是更明智的默认行为。标记为接受答案。最后一点需要注意的是,access=“isAuthenticated”级别将要求用户主体的授权权限包含该授权权限。然后,访问决策管理器将接受用户主体作为已验证的,但是如果引发了AuthenticationException,您将被转发到该答案中所述的所需页面。再次感谢!总之,Abraham下面的回答确实有效,但是这个问题很快将在4.1版本中正式解决,请查看Spring安全团队的记录。