简体   繁体   中英

JSF 2.2 & Authentication: redirect condition

NOTE: the user in these scenarios has already an account. I'm using Java EE 7: JSF 2.2, GLassfish 4.1 and From-based Authentication. Goal: when the user login (sign in) for the first time I want to redirect him to (for example) page : a.xhtml so he enter some information. after that every time he login will be redierct to the page: normal.xhtml and NOT a.xhtml. as you can see in the following code after the user login, he will always be redirected to a.xhtml and that is the problem:

<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>

2nd Question: when I visited the "default project page" : http://localhost:8080/Pro1.0/ the server wants redirect me to the protected a.xhtml and since the user is not authenticated, the server redirect me to the "form-login-page" page declared in web.xml, my question here is that how can I make the server redirect "default project page" to a public page (home page like any website) and redirect the user when authenticated to a private page like normal.xhtml as asked in the first question. Thanks and if you have any question I'm here.

UPDATE: Thanks Oskars Pakers for solution

here is my implementation: 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>

Redirect.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;
    }
}
}

1st question
I see two options here.

  1. You can check on page a.xhtml that user has been there (and entered information) and redirect it to normal.xhtml
  2. You can switch to programmatic login, make a custom jsf page and execute backing bean method similar to this

    String login(){ HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); request.login(username,password); //check if user has been in a.xhtml return "a.xhtml"; // or return "normal.xhtml" }

2nd question

Similar to question one, you must check on your public page if user is logged in and redirect to his page. As you are using JSF 2.2. have you seen

<f:viewAction

tag? It exectues method on entering the page. You can return outcome if you want to redirect user.
See f:viewAction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM