Java:在进行登录身份验证之前拦截所有请求

时间:2015-12-23 06:34:32

标签: java authentication servlets web.xml servlet-filters

我想先拦截过滤器中的所有请求。我还有一个登录验证,它适用于所有请求,即过滤器和登录验证都配置为拦截所有请求。

然而,当发出任何请求时,它首先被尝试呈现登录页面的登录验证拦截。我希望首先通过过滤器拦截请求,然后通过登录验证拦截请求。

以下是相关代码。

  

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Tango</display-name>

    <filter>
        <filter-name>SalsaValidationFilter</filter-name>
        <filter-class>net.semandex.salsa.validationFilters.SalsaValidationFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>SalsaValidationFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <!-- <servlet-name>SalsaValidationServlet</servlet-name> -->
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Login page images</web-resource-name>
            <url-pattern>/images/salsadb-logo2.png</url-pattern>
            <url-pattern>/images/salsa-icon.png</url-pattern>
            <url-pattern>/images/shadow_box.png</url-pattern>
            <url-pattern>/images/header.png</url-pattern>
            <url-pattern>/images/bg.png</url-pattern>
            <url-pattern>/css/splash.css</url-pattern>
            <url-pattern>/WEB-INF/licenseValidation.html</url-pattern>
            <url-pattern>/auth/licenseValidation.html</url-pattern>
        </web-resource-collection>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>The entire webapp</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>SalsaUser</role-name>
        </auth-constraint>
    </security-constraint>

    <security-role>
        <role-name>SalsaUser</role-name>
    </security-role>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
          <form-login-page>/auth/login.jsp</form-login-page>
          <form-error-page>/auth/loginError.jsp</form-error-page>
        </form-login-config>

        <realm-name>mongo_login</realm-name>
    </login-config>
</web-app>

更多细节: 这是发生的事件流。假设主页请求,它首先由尝试呈现登录页面的登录验证处理。登录页面有一些图像和CSS。因此,请求这些图像。这些请求被过滤器截获。

  

过滤

public class SalsaValidationFilter implements Filter {

    private ServletContext context;

    public void init(FilterConfig fConfig) throws ServletException {
        this.context = fConfig.getServletContext();
        this.context.log("SalsaValidationFilter initialized");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        String uri = req.getRequestURI();
        this.context.log("Requested Resource::"+uri);

        HttpSession session = req.getSession(false);

        boolean licenseValid = false;
        if( !licenseValid && !uri.endsWith("licenseValidation.html") ){
            this.context.log("NO valid license was found");
            // pass the request along the filter chain
            res.sendRedirect( req.getContextPath() +  "/auth/licenseValidation.html");
            return;
        }
        //else{
            chain.doFilter(req, res);
        //}
    }

    public void destroy() {
        //close any resources here
    }

}

知道如何确保过滤器首先拦截请求吗?

2 个答案:

答案 0 :(得分:2)

  

知道如何确保过滤器首先拦截请求吗?

您需要安装一种名为ServerAuthModule的特殊过滤器,也称为SAM

这个特殊的过滤器来自Java EE的JASPIC规范,并且在调用任何其他过滤器或servlet之前被调用,并且它是您应该在Java EE中执行与安全性相关的事务的专用位置。

答案 1 :(得分:0)

由于规范和安全限制,这是不可能的,容器将在过滤器之前处理安全性约束。

您可以删除安全约束,并使用Servlet 3.0中的HttpServletRequest#login()方法动态登录所需的所有页面。