【问题标题】:Using Authorization filter in JSF project for custom authentication在 JSF 项目中使用授权过滤器进行自定义身份验证
【发布时间】:2012-02-27 19:28:52
【问题描述】:

我在我的应用程序中嵌入了登录/注销功能,但过滤器可能无法正常工作,因为当我将它们指向浏览器地址栏中时,我仍然可以在注销后看到这些页面。这是我的登录操作:-

this.currentUser = new User();  // initiate currentUser
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getApplication().createValueBinding("#{" + Constants.VISIT_KEY_SCOPE +
Constants.VISIT_KEY + "}").setValue(facesContext, currentUser);
FacesUtils.putIntoSession(Constants.VISIT_KEY, currentUser);

注销操作:-

FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)facesContext.getExternalContext().getSession(false);
session.removeAttribute(Constants.VISIT_KEY_SCOPE + Constants.VISIT_KEY);

 if (session != null)
    {
     session.invalidate();
    }

常量类:-

public class Constants
{
  // Backing bean keys
 public final static String VISIT_KEY_SCOPE = "sessionScope.";
 public final static String VISIT_KEY = "currentUser";

 // Model object keys
 public final static String PROJECT_COORDINATOR_SCOPE = "applicationScope.";


  public final static String ORIGINAL_VIEW_SCOPE = "sessionScope";
  public final static String ORIGINAL_VIEW_KEY = "originalTreeId";
  }

web.xml:-

 <filter>
 <filter-name>AuthorizationFilter</filter-name>
 <filter-class>org.AuthorizationFilter.AuthorizationFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>AuthorizationFilter</filter-name>
 <url-pattern>/faces/pages/*</url-pattern>
 </filter-mapping>

最后授权过滤器如下:-

public class AuthorizationFilter implements Filter
{
 FilterConfig config = null;
 ServletContext servletContext = null;

 public AuthorizationFilter()
 {
 }

 public void init(FilterConfig filterConfig) throws ServletException
 {
  config = filterConfig;
  servletContext = config.getServletContext();
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                   FilterChain chain) throws IOException, ServletException
     {
       HttpServletRequest httpRequest = (HttpServletRequest)request;
       HttpServletResponse httpResponse = (HttpServletResponse)response;
       HttpSession session = httpRequest.getSession();

       User currentUser = (User)session.getAttribute("currentUser");

    if (session  == null || currentUser == null || currentUser.getUserName() == null)
     {
      session.setAttribute(Constants.ORIGINAL_VIEW_KEY, httpRequest.getPathInfo());
      httpResponse.sendRedirect(httpRequest.getContextPath() +  "/faces/pages  
       /login.jsp");
      }
     else
        {
         session.removeAttribute(Constants.ORIGINAL_VIEW_KEY);
         chain.doFilter(request, response);
         } 

         }

          public void destroy()
          {
           }
           }

非常感谢您的耐心和帮助。

【问题讨论】:

    标签: jsf servlet-filters


    【解决方案1】:

    您需要告诉浏览器缓存您需要检查用户是否登录的受限页面。否则浏览器将只显示缓存中的页面,因此永远不会调用你的过滤器。您可以在调用FilterChain#doFilter() 之前将以下几行添加到过滤器中的else 块中之前

    httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    httpResponse.setDateHeader("Expires", 0); // Proxies.
    

    与具体问题无关,您的代码存在一些缺陷:

    1. session.removeAttribute() 在您的注销操作中可能会抛出NullPointerException,因为您在getSession() 中传递了false。无论如何,当您要致电session.invalidate() 时,这条线是多余的。只需将其删除。

    2. 您的过滤器中的request.getSession() 永远不会返回null,因为您没有将false 传递给它。所以session == null是多余的或者你必须添加false

    【讨论】:

    • 嗨 BalusC。谢谢你的答复。我已经添加了“缓存删除”的行。并解决了你提到的第 1 点和第 2 点。应用程序没有抛出任何错误,但是当它们的地址写入浏览器时,页面仍然显示。知道去哪里找吗?
    猜你喜欢
    • 2018-12-11
    • 2021-09-19
    • 2021-06-05
    • 2013-12-29
    • 2015-09-05
    • 2021-09-16
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多