java - Spring Security 3.0.3 及自定义认证处理过滤器

标签 java spring jakarta-ee spring-security

我使用 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.doFiltermyFilter.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/

相关文章:

java - 如何配置 OC4J 以支持阿拉伯语?

java - 与 ignite 相关的构建问题

java - 在继续之前强制方法完成

java - 必需的字符串参数不存在 Spring MVC

mysql - 如何使用 Eclipse IDE 设置 MySQL?

java - 未能延迟初始化角色 [] 的集合,无法初始化代理 - 无 session

swift - 向 Spring 服务器发出 post 请求时出现 400 错误

java - Spring Data REST + JPA 从 OneToMany 集合中删除 [不是所有者端]

eclipse - 没有加载 apache-tomcat-8.0.17 的管理器/html

javascript - 如何复制表中的特定行(JS/JQuery)?