gpt4 book ai didi

jsf - 在 JSF 项目中使用授权过滤器进行自定义身份验证

转载 作者:行者123 更新时间:2023-12-01 17:17:42 24 4
gpt4 key购买 nike

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

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()
{
}
}

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

最佳答案

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

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. 过滤器中的
  3. request.getSession() 永远不会返回 null,因为您没有将 false 传递给它。因此 session == null 是多余的,或者您必须添加 false

关于jsf - 在 JSF 项目中使用授权过滤器进行自定义身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9158142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com