Java servlet过滤器无法登录

时间:2009-03-26 19:00:42

标签: java servlets servlet-filters

我编写了一个过滤器类,为每个页面添加一个P3P标头。我把它添加到我的web.xml:

<filter>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <filter-class>com.mycompany.AddP3pHeaderFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

它会为每个页面请求添加标头,但在用户首次登录时不起作用。用户将表单提交到j_security_check,但响应不包含标头。如何使我的过滤器适用于登录请求?

5 个答案:

答案 0 :(得分:1)

登录请求转发到相应的页面。默认情况下,过滤器仅适用于REQUEST调度。您需要按如下方式修改web.xml:

<filter>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <filter-class>com.mycompany.AddP3pHeaderFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

编辑:我认为这已经解决了,但我错了。

答案 1 :(得分:1)

Doesn't work in Tomcat.

我最终不得不使用Tomcat阀门。

答案 2 :(得分:0)

由于安全原因,大多数servletcontainers确实不允许/j_security_check请求挂钩。一些旧版本可以,但应该在较新版本中修复。

无论如何,最好的方法是检查HttpSession中是否存在您手动放在那里的用户主体(如果没有)。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    UserPrincipal user = httpRequest.getUserPrincipal();
    HttpSession session = httpRequest.getSession();
    if (user != null && session.getAttribute("user") == null) {
        session.setAttribute("user", user);

        // First-time login. You can do your intercepting thing here.
    }
    chain.doFilter(request, response);
}

答案 3 :(得分:0)

不知道tomcat,但是它适用于我的码头,只需将你的过滤器放在春天过滤器之前:

<filter>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <filter-class>com.mycompany.AddP3pHeaderFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AddP3pHeaderFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

答案 4 :(得分:0)

我的web.xml

<filter>
        <description>
        FBIS Filter</description>
        <display-name>Fbisfilter</display-name>
        <filter-name>Fbisfilter</filter-name>
        <filter-class>fbis.filter.Fbisfilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Fbisfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

和myfilter是

package fbis.filter;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sqlconnection.SQLConnect;
import com.sun.security.auth.UserPrincipal;

import fbis.interfaces.abstractclasses.Connections;

/**
 * Servlet Filter implementation class Fbisfilter
 */
public class Fbisfilter extends Object implements Filter,Connections {
    public void destroy() {
    //  System.out.println("Filter Service finished");
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpresponse = (HttpServletResponse) response;
        System.out.println("FILTER INSIDE");
    // place your code here
        HttpSession session = httpRequest.getSession(true);
        String usrid = httpRequest.getParameter("txtuname");
        String pwd = httpRequest.getParameter("txtpassword");
        PreparedStatement pstmt;
        try {
            pstmt = con
                    .prepareStatement("SELECT l.Userid,n.First_name from Login_details_dtls l join New_Registration_dtls n on l.Userid=n.Userid where n.Userid=? AND l.pwd=?");
        pstmt.setString(1, usrid);
        pstmt.setString(2, pwd);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            session.setAttribute("usr", rs.getString("Userid"));
            session.setAttribute("usrname", rs.getString("First_name"));
        }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        chain.doFilter(httpRequest, httpresponse);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("Filter Service has started");
    }

}