【问题标题】:JSF 2.2 & Authentication: redirect conditionJSF 2.2 & Authentication:重定向条件
【发布时间】:2014-11-27 15:04:36
【问题描述】:

注意:这些场景中的用户已经有一个帐户。 我正在使用 Java EE 7:JSF 2.2、GLassfish 4.1 和基于 From 的身份验证。目标:当用户第一次登录(登录)时,我想将他重定向到(例如)页面:a.xhtml 以便他输入一些信息。之后他每次登录都会重定向到页面:normal.xhtml 而不是 a.xhtml。正如您在用户登录后在以下代码中看到的那样,他将始终被重定向到 a.xhtml,这就是问题所在:

<security-constraint>
    <display-name>securityConstraint1</display-name>
    <web-resource-collection>
      <web-resource-name>resources</web-resource-name>
      <description></description>
      <url-pattern>/private/* </url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>Admin</role-name>
    </auth-constraint>

    </security-constraint>
      <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>ProjRealm</realm-name>
        <form-login-config>
          <form-login-page>/login.xhtml</form-login-page>
          <form-error-page>/public/pages/forbidden.xhtml</form-error-page>
        </form-login-config>
      </login-config>
      <security-role>
        <role-name>Admin</role-name>
      </security-role>
      <welcome-file-list>
        <welcome-file>private/inscription/a.xhtml</welcome-file>
      </welcome-file-list>

第二个问题: 当我访问“默认项目页面”时:http://localhost:8080/Pro1.0/ 服务器想要将我重定向到受保护的 a.xhtml 并且由于用户未经过身份验证,服务器将我重定向到在 web.xml 中声明的“表单登录页面”页面。 xml,我的问题是如何使服务器将“默认项目页面”重定向到公共页面(像任何网站一样的主页)并在通过身份验证时将用户重定向到像 normal.xhtml 这样的私有页面,如第一个问题中所问. 谢谢,如果您有任何问题,我在这里。

更新:感谢 Oskars Pakers 的解决方案

这是我的实现: web.xml:

<welcome-file-list>
    <welcome-file>redirect.xhtml</welcome-file>
  </welcome-file-list>

redirect.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:metadata>
    <f:viewAction action="#{redirect.testUser}"></f:viewAction>
</f:metadata>
<h:head>
</h:head>
<h:body>
    <h1>TEST Redirect</h1>
</h:body>
</html>

重定向.java:

@Named
@RequestScoped
public class Redirect extends BaseBacking {

private static final String AAA = "/private/inscription/aaa.xhtml?faces-redirect=true";
private static final String NORMAL = "/private/sections/normal.xhtml?faces-redirect=true";
private static final String HOME = "/home.xhtml?faces-redirect=true";

@EJB
private EJBClass EJBClass;

public String testUser() {
    Principal user = getRequest().getUserPrincipal();
    if (user == null) {
        return HOME;
    }
    BigInteger Id = /*here I call a EJBClass method so I can decide is it the first time the user login OR NOT*/(user.getName());
    if (Id == null) {
        return AAA;
    } else {
        return NORMAL;
    }
}
}

【问题讨论】:

    标签: java jsf redirect jsf-2.2 java-ee-7


    【解决方案1】:

    第一个问题
    我在这里看到两个选项。

    1. 您可以在页面 a.xhtml 上检查用户是否去过那里(并输入了信息)并将其重定向到 normal.xhtml
    2. 可以切换到程序化登录,制作自定义jsf页面,执行类似这样的backing bean方法

      字符串登录(){ HttpServletRequest 请求 = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); request.login(用户名,密码); //检查用户是否在a.xhtml中 返回“a.xhtml”; // 或返回“normal.xhtml” }

    第二个问题

    与问题一类似,如果用户登录并重定向到他的页面,您必须检查您的公共页面。 当您使用 JSF 2.2 时。你见过吗

    <f:viewAction
    

    标签?它在进入页面时执行方法。如果要重定向用户,可以返回结果。
    f:viewAction

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 2014-12-30
      • 2014-07-14
      • 2015-11-14
      • 2014-07-18
      • 2010-11-01
      • 2013-06-26
      相关资源
      最近更新 更多