【问题标题】:How to fix blank page after @WebFilter redirect in JSF?如何在 JSF 中 @WebFilter 重定向后修复空白页?
【发布时间】:2020-02-22 12:15:39
【问题描述】:

我正在使用 JSF 制作应用程序,如果用户未登录,我想使用 @WebFilter 将用户从每个页面(不包括 login/register.xhtml)重定向到 login.xhtml。问题是重定向后我看到的只是一个空白页。

我认为它过滤了包括 bootstrap.css 在内的所有资源,所以我使用了这样的东西:

if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response);
    return;
}

但它什么也没做。

这是我的 LoginFilter.java

@WebFilter("*")
public class LoginFilter extends HttpFilter {
    @Inject
    CurrentSession currentSession;

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String currentPath = req.getContextPath() + req.getServletPath();

        if (!userIsLogged()) {
            if(!currentPath.equals("/app/register.xhtml") && !currentPath.equals("/app/login.xhtml"))
                res.sendRedirect(req.getContextPath() + "/login.xhtml");
        }
        else
            chain.doFilter(req, res);
}

有人可以指导我添加什么吗?

【问题讨论】:

  • 解决方案属于答案,而不是问题的编辑。请还原此内容

标签: jsf servlet-filters


【解决方案1】:

我认为您不应该编写自定义过滤器,因为 JavaEE 具有用于身份验证和授权的内置 Security Mechanism。 您只需要在WEB-INF/web.xml 文件中进行配置。 这是一个最小的项目:

示例网络应用程序 ├── pom.xml └── 源 └── 主要 ├── java ├── 资源 │   └── messages.properties └── 网页应用 ├── 页 │   ├── admin(仅限管理员) │   │   └── admin.xhtml │   ├──dashboard.xhtml(所有登录用户均可使用) │   ├── protected.xhtml │   └── 公共(不受限制的页面) │   ├── login.xhtml │   └── register.xhtml ├── 资源(不受限制的公共资源) │   └── css │   └── style.css └── WEB-INF ├── faces-config.xml ├── jboss-web.xml └── web.xml

所有静态资源(图像、样式表等)都位于webapp/resources 下。 JSF 文件位于webapp/pages/... 下,但这只是一个示例。期望的行为

  • 每个静态资源都是公开的
  • login.xhtml 和 register.xhtml (webapp/pages/public 下的所有内容)都是公开的
  • webapp/pages/ 下的所有其他页面都受到保护,可供所有登录用户使用
  • webapp/pages/admin 下的页面仅供管理员使用。

在 web.xml 中只需配置以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">


    <welcome-file-list>
        <welcome-file>/pages/dashboard.xhtml</welcome-file>
    </welcome-file-list>

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>stackoverflow-auth-realm</realm-name>
        <form-login-config>
            <form-login-page>/pages/public/login.xhtml</form-login-page>
            <form-error-page>/pages/public/login.xhtml?showerror=true</form-error-page>
        </form-login-config>
    </login-config>

    <security-constraint>
        <display-name>Allowed for admin only</display-name>
        <web-resource-collection>
            <web-resource-name>Admin</web-resource-name>
            <url-pattern>/pages/admin/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>ADMINISTRATOR</role-name>
        </auth-constraint>
    </security-constraint>

    <security-constraint>
        <display-name>Unrestricted access</display-name>
        <web-resource-collection>
            <web-resource-name>Unrestricted</web-resource-name>
            <url-pattern>/pages/public/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <!-- auth-constraint section is missing which means theese resources are available for everyone -->
    </security-constraint>
    <security-constraint>
        <display-name>Allowed for all logged in users</display-name>
        <web-resource-collection>
            <web-resource-name>LoggedIn</web-resource-name>
            <url-pattern>/pages/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name> <!-- Wildcard role-name means authentication is required but theese resources are available for every roles -->
        </auth-constraint>
    </security-constraint>

    <security-role>
        <role-name>ADMINISTRATOR</role-name>
    </security-role>
    <security-role>
        <role-name>*</role-name>
    </security-role>

    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

</web-app>

在 login.xhtml 中有一些要求。

  • 表单必须提交至j_security_check action
  • 用户和密码字段必须是j_usernamej_password,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets">

<h:body>
    <div class="login-box">
    <ui:fragment rendered="#{param.showerror}">
        <div class="text-danger">
            <h4>#{msg['login.error']}</h4>
        </div>
    </ui:fragment>
    <form method="post" action="j_security_check">
        <p:inputText id="j_username" placeholder="#{msg['login.username']}"
                     required="true" autocomplete="off"
                     requiredMessage="#{msg['login.error.username']}"/>
        <p:password id="j_password" placeholder="#{msg['login.password']}" required="true" autocomplete="off"
                    requiredMessage="#{msg['login.error.password']}"/>
        <button><h:outputText value="#{msg['login.submit']}"/></button>
    </form>
</h:body>
</html>

假设您有三种不同的最终用户类型。

  • 未经身份验证(谁没有登录)
  • 用户(已验证)
  • ADMINISTRATOR(经过身份验证并具有特殊角色)


  • 未经身份验证的用户将访问login.xhtmlregister.xhtml 否则该用户将被重定向到登录表单。
  • 经过身份验证的用户将访问webapp/pages 下的所有页面,webapp/pages/admin 下的页面除外。如果 USER 尝试访问管理页面,它将 得到一个禁止响应。
  • ADMINISTRATOR 用户将访问每个页面。

【讨论】:

  • 如果 OP 出于其他原因想要使用过滤器怎么办......这样就行不通了,问题可能仍然存在。知道是什么导致了最初的问题吗?
【解决方案2】:

我问这个问题已经有一段时间了,但我认为有人可能会遇到这个帖子,所以我将在这里发布我的解决方案。

@WebFilter("*")
public class LoginFilter extends HttpFilter {
    @Inject
    CurrentSession currentSession;

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

        //Resources
        boolean isResource = req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/");

        if(isResource)
            chain.doFilter(req, res);

}

我使用上下文路径和资源标识符来检查它当前加载的文件是否是资源。

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2019-12-27
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多