我使用 Spring Security 3.0.3.RELEASE。我想创建一个自定义身份验证处理过滤器。
我创建了一个这样的过滤器:
// imports ommited
public class myFilter extends AbstractAuthenticationProcessingFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
// some code here
}
}
我按以下方式配置我的 security.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config="true">
<!--<session-management session-fixation-protection="none"/>-->
<custom-filter ref="ipFilter" before="FORM_LOGIN_FILTER"/>
<intercept-url pattern="/login.jsp*" filters="none"/>
<intercept-url pattern="/**" access="ROLE_USER"/>
<form-login login-page="/login.jsp" always-use-default-target="true"/>
<logout logout-url="/logout" logout-success-url="/login.jsp" invalidate-session="true"/>
</http>
<beans:bean id="ipFilter" class="myFilter">
<beans:property name="authenticationManager" ref="authenticationManager"/>
</beans:bean>
<authentication-manager alias="authenticationManager" />
</beans:beans>
一切似乎都是正确的,但是当我尝试访问名为 myFilter.doFilter
的 myFilter.attemptAuthentication
protected 页面时。
有什么想法吗?
最佳答案
当然,servlet 容器会调用 MyFilter.doFilter
- 毕竟,这是 entry point进入过滤器。
在您的特定情况下,servlet 容器应该在 AbstractAuthenticationProcessingFilter 上调用 doFilter()
,不在 MyFilter
上。
AbstractAuthenticationProcessingFilter.doFilter()
依次负责调用 MyFilter.attemptAuthentication()
。
如果不是这种情况,也许您覆盖 MyFilter
中的doFilter()
?如果是,最好删除它(或至少调用 super.doFilter()
)。参见 AbstractAuthenticationProcessingFilter 的 JavaDocs|了解更多详情。
编辑:MinimeDJ 澄清说一切都按照我上面的建议进行。在这种情况下,我建议检查 filterProcessesUrl
属性的值。来自JavaDocs :
This filter will intercept a request and attempt to perform authentication from that request if the request URL matches the value of the filterProcessesUrl property. This behaviour can modified by overriding the method requiresAuthentication.
所以你可以:
- 要么设置适当的
filterProcessesUrl
值(通常类似于/j_spring_security_check
) - 或者您可以覆盖
requiresAuthentication
方法以始终返回true
。
如果您仍然遇到问题,那么我建议您使用调试器并单步执行 spring-security(和您的)代码,以查看问题到底发生在哪里。
关于java - Spring Security 3.0.3 及自定义认证处理过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146449/