如何将已通过身份验证的用户从登录页面重定向到主页

2024-03-16

我正在使用 Apache Shiro 开发 JSF 应用程序。我使用 Shiro 验证用户并将她重定向到主页,这没有问题。身份验证后,当我尝试访问登录页面时,它不会将我重定向到主页。即使已经有登录的用户,我也可以再次登录。我正在做程序化登录 http://balusc.blogspot.com/2013/01/apache-shiro-is-it-ready-for-java-ee-6.html#ProgrammaticLogin正如 BalusC 在他的博客文章中提到的。

[main]
credentialsMatcher = org.apache.shiro.authc.credential.PasswordMatcher
myRealm = com.example.security.myRealm
myRealm.credentialsMatcher = $credentialsMatcher
securityManager.realms = $myRealm
user = com.example.web.filter.FacesAjaxAwareUserFilter
user.loginUrl = /login.xhtml

[urls]
/login.xhtml = user

该过滤器是根据博客文章编写的。

public class FacesAjaxAwareUserFilter extends UserFilter {

private static final String FACES_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        + "<partial-response><redirect url=\"%s\"></redirect></partial-response>";

@Override
protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
    HttpServletRequest req = (HttpServletRequest) request;
    if ("partial/ajax".equals(req.getHeader("Faces-Request"))) {
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().printf(FACES_REDIRECT_XML, req.getContextPath() + getLoginUrl());
    }
    else {
        super.redirectToLogin(request, response);
    }
}

}

问题是什么?如果用户已经通过身份验证,我该如何重定向用户?

EDIT:现在,如果用户已经通过身份验证,我将使用 PostConstruct 注释进行重定向。我愿意接受任何好的解决方案。


身份验证后,当我尝试访问登录页面时,它不会将我重定向到主页。即使已经有登录用户,我也可以再次登录

Shiro 和自定义 Shiro 用户过滤器都无意阻止这种情况。 Shiro 没有这方面的内置设施。自定义 Shiro 用户过滤器仅在找到未经身份验证的用户时运行,而不是在找到已通过身份验证的用户时运行。

阻止经过身份验证的用户直接访问登录页面是您自己的责任。根据业务需求,您可以执行以下操作:

  • 就允许吧。也许用户只是想切换登录名。如果需要,您可以有条件地显示一条消息,例如:

      <ui:fragment rendered="#{not empty request.remoteUser}">
          You are already logged-in as #{request.remoteUser}.
          By logging in as another user, you will be logged out.
      </ui:fragment>
      <h:form id="login">
          ...
      </h:form>
    
  • 不允许,但留在同一页面。有条件地隐藏登录表单并显示如下消息:

      <ui:fragment rendered="#{not empty request.remoteUser}">
          Hey, how did you end up here?
          You are already logged-in as #{request.remoteUser}!
      </ui:fragment>
      <h:form id="login" rendered="#{empty request.remoteUser}">
          ...
      </h:form>
    

    当然,当用户已经登录时,请确保您的 Web 应用程序在任何地方都没有登录链接。

  • 不允许它并重定向到所需的目标页面。这又可以通过多种方式来完成。最干净的方法是使用 servlet 过滤器。

      @WebFilter(urlPatterns = "/login.xhtml")
      public class LoginPageFilter implements Filter {
    
          @Override
          public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
              HttpServletRequest request = (HttpServletRequest) req;
              HttpServletResponse response = (HttpServletResponse) res;
    
              if (request.getRemoteUser() != null) {
                  response.sendRedirect(request.getContextPath() + "/home.xhtml"); // Redirect to home page.
              } else {
                  chain.doFilter(req, res); // User is not logged-in, so just continue request.
              }
          }
    
          // Add/generate init() and destroy() with NOOP.
      }
    

    您也可以在preRenderView事件监听器 https://stackoverflow.com/questions/8399045/conditional-redirection-in-jsf/8399591#8399591. A @PostConstruct可能为时已晚,因为此时可能已经提交了响应。请注意,没有任何形式的反馈的重定向可能会让最终用户感到困惑。在过滤器中,考虑传递一个应触发条件消息的附加参数。或者在preRenderView事件侦听器,设置 Flash 范围消息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将已通过身份验证的用户从登录页面重定向到主页 的相关文章

随机推荐