1

我是 Java 企业版的新手。我开始从一些 YouTube 视频中学习,最近开始阅读http://docs.oracle.com/javaee/6/tutorial/doc/ 我读完了第 15 章。

我尝试制作自己的过滤器。

我没有使用 Java Servlet 类。因为我想使用 JSF 页面,据我所知,只能将托管 Bean 与 JSF 页面一起使用,而 Servlet 类与 JSP 一起使用。没关系。

据我所知登录过滤器的用处: https ://stackoverflow.com/tags/servlet-filters/info

[...] 当您有多个页面要检查登录用户时,这特别有用。您可以使用过滤器将其放在一个位置,而不是在所有页面上复制粘贴相同的逻辑。

当用户直接在浏览器中为需要登录用户的页面键入 URL 时,它很有用(据我所知),因此过滤器会将他重定向到登录页面或在他登录时继续。

我搜索了任何简单的例子来学习,但没有找到。我举个简单的例子:

我有两个 JSF 页面
,一个名为 home.xhtml(需要登录用户)
,另一个名为 login.xhtml(如果非登录用户寻找主页,过滤器必须重定向到它)

登录.xhtml:

  <h:form>
     <h:panelGrid columns="2">
     <h:outputLabel value="name:"/> <h:inputText value="#{user.name}"/>
     <h:outputLabel value="password:"/> <h:inputSecret value="#{user.password}"/>
     </h:panelGrid>
     <h:commandButton id="btn"  value="login" action="#{user.login()}"/>
  </h:form>

主页.xhtml:

<h:body>
   Hello #{user.name}. You are welcome
</h:body>

用户:

@ManagedBean
@SessionScoped
public class User implements Serializable
{
   String name;
   String password;
   Authentication authentication;

   public User()
   {
      authentication = new Authentication();
   }

    //Getters and Setters for name and password.

   public String login()
   {
      if (this.getName().equals("user") &&(this.getPassword().equals("1234")))
      {
         authentication.setLoggedIn(true);
         FacesContext context = FacesContext.getCurrentInstance();
         context.getExternalContext().getSessionMap().put("auth", authentication);
         return "home";
      }
      else
      {
         authentication.setLoggedIn(false);
         FacesContext context = FacesContext.getCurrentInstance();
         context.getExternalContext().getSessionMap().put("auth", authentication);
         return "login";
      }
   }
}

验证:

@ManagedBean
@SessionScoped
public class Authentication implements Serializable
{

   private boolean authenticated;

   public Authentication()
   {
      authenticated = false;
   }

   public boolean isLoggedIn()
   {
      return authenticated;
   }

   public void setLoggedIn(boolean authenticated)
   {
      this.authenticated = authenticated;
   }

}

登录过滤器:

@WebFilter(value = "/home")
public class LoginFilter implements Filter
{

   @Override
   public void init(FilterConfig filterConfig) throws ServletException
   {
      //throw new UnsupportedOperationException("Not supported yet.");
   }

   @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
   {
      HttpServletRequest req = (HttpServletRequest) request;
      Authentication auth = (Authentication) req.getSession().getAttribute("auth");

      if (auth != null && auth.isLoggedIn())
      {
         System.out.println("Filter is working");
         chain.doFilter(request, response);
      } else
      {
         System.out.println("Filter is working");
         HttpServletResponse res = (HttpServletResponse) response;
         res.sendRedirect(req.getContextPath() + "/login.xhtml");
      }
   }

   @Override
   public void destroy()
   {
      //throw new UnsupportedOperationException("Not supported yet.");
   }
}

面孔配置:

   <navigation-rule>
      <from-view-id>/login.xhtml</from-view-id>
      <navigation-case>
         <from-outcome>home</from-outcome>
         <to-view-id>/home.xhtml</to-view-id>
         <redirect/>
      </navigation-case>
      <navigation-case>
         <from-outcome>login</from-outcome>
         <to-view-id>/login.xhtml</to-view-id>
         <redirect/>
      </navigation-case>
   </navigation-rule>

网页.xml:

<context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/login.xhtml</welcome-file>
    </welcome-file-list>

现在,当我在浏览器中键入 home.xhtml 页面的 URL(在清除历史记录和 cookie 之后)时,假定将我重定向到登录页面。但相反,它以空值的 name 回到家中:
Hello #{user.name}. You are welcome呈现为Hello . You are welcome

甚至System.out.println("Filter is working");不打印任何东西。

4

1 回答 1

1

你确定过滤器被调用了吗?如果没有打印任何内容,System.out我想不会。问题可能是 servlet 映射。

你指定了这个:

@WebFilter(value = "/home")
public class LoginFilter implements Filter {...}

我认为这仅与 url 匹配/home。尝试使用/*or /home*(这是非常有限的,我不推荐它)来代替。

另一件事:如果你得到Hello #{user.name}. You are welcome作为输出,那么FacesServlet可能不会调用。这可能有两个原因:

  1. 您使用了错误的映射。尝试使用/faces/home.xhtmlor来调用页面/home.jsf。url 取决于您在web.xml.
  2. FacesServlet正确配置/根本没有在web.xml.
于 2013-04-18T20:07:48.317 回答