【发布时间】: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