Pretty Faces with Spring Security and primefaces

Splash Forums PrettyFaces Users Pretty Faces with Spring Security and primefaces

This topic contains 3 replies, has 2 voices, and was last updated by  Christian Kaltepoth 5 years, 4 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #18414

    soroor ash
    Participant

    Hi :)

    my configuration is:

    maven- version:2.3.1

    hibernate – version: 3.2.7ga

    spring – version: 2.5.6

    spring-security-version: 2.0.4

    mysql – version: 5.1.9

    jsf – version:2.1.0-b03

    primefaces – version: 3.2

    prettyfaces – version :3.3.3

    my problem:

    After Login , get session correctly but , dialog not closed and not show welcome message and not directly to admin page.

    my login page is :

    <script type="text/javascript">
    function handleLoginRequest(xhr, status, args) {
    if (args.validationFailed || !args.loggedIn) {
    jQuery('#dialog').effect("shake", { times:3 }, 100);
    } else {
    dlg.hide();
    jQuery('#loginLink').fadeOut();
    }
    }
    </script>

    <p:dialog id="dialog" header="Login" widgetVar="dlg" style="width:300px;">
    <h:form id="loginForm" prependId="false">
    <h:panelGrid columns="2" cellpadding="5">
    <label for="j_username">
    <h:outputText value="Username:"/>
    </label>
    <h:inputText validator="#{loginBean.validateUsername}"
    id="j_username"
    required="true">
    </h:inputText>
    <label for="j_password">
    <h:outputText value="Password:"/>
    </label>
    <h:inputSecret validator="#{loginBean.validatePassword}" id="j_password"
    required="true">
    </h:inputSecret>
    <label for="_spring_security_remember_me">
    <h:outputText value="Remember me"/>
    </label>
    <h:selectBooleanCheckbox id="_spring_security_remember_me"/>

    <f:facet name="footer">
    <p:commandButton type="submit" update=":growl" id="login"
    action="#{loginBean.doLogin}" value="Login"
    oncomplete="handleLoginRequest(xhr, status, args);" />
    </f:facet>
    </h:panelGrid>
    </h:form>
    </p:dialog>

    my applicationContext-security.xml is:

    <global-method-security
    secured-annotations="enabled">
    </global-method-security>

    <http auto-config="true" access-denied-page="/faces/accessDenied.xhtml">
    <intercept-url pattern="/faces/admin**" access="ROLE_SUPERVISOR"/>
    <intercept-url pattern="/faces/xhtml/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>

    <form-login
    login-processing-url="/j_spring_security_check"
    login-page="/faces/home.xhtml"
    default-target-url="/"
    authentication-failure-url="/faces/accessDenied.xhtml"/>
    <logout logout-url="/logout*" logout-success-url="/"/>

    </http>
    <authentication-provider>
    <password-encoder hash="md5"/>
    <user-service>
    <user name="rod" password="a564de63c2d0da68cf47586ee05984d7"
    authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER"/>
    <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER"/>
    <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER"/>
    <user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER"/>
    </user-service>
    </authentication-provider>

    my web.xml is:

    <!-- Pretty faces -->
    <context-param>
    <param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
    <param-value>true</param-value>
    </context-param>

    <!-- Pretty Filter -->
    <filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
    </filter-mapping>

    <!-- Filter Config -->
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
    org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
    </filter>

    <!-- Filter Mappings -->
    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    my pretty-config.xml is:

    <url-mapping id="manage">
    <pattern value="/adminPage" />
    <view-id value="/faces/adminIndex.xhtml" />
    </url-mapping>

    my method in LoginBean is:

    public String doLogin() throws IOException, ServletException {

    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

    RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
    .getRequestDispatcher("/j_spring_security_check");

    dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());

    FacesContext.getCurrentInstance().responseComplete();

    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
    AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, null);
    FacesContext.getCurrentInstance().addMessage(null,
    new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome !!!", null));
    loggedIn = Boolean.TRUE;
    setWelcome(Boolean.TRUE);

    // It's OK to return null here because Faces is just going to exit.
    return "pretty:manag";

    }

    Please Help me i’m very helpless.

    #22588

    First of all, you have a typo in the return statement of your login method. You return pretty:manag but it should be pretty:manage.

    Are you sure your handleLoginRequest gets invoked? If you return a PrettyFaces navigation from an action method invoked by an AJAX request, JSF typically automatically redirects the user to the target page. I’m not sure the oncomplete handler is invoked in this case.

    I would also recommend to use Firebug or some similar development tool to look into the AJAX response you are getting.

    #22589

    soroor ash
    Participant

    Thank you christian :)

    i change pretty:manag to pretty:manage but not redirect.

    if used login method Instead doLogin(), in LoginBean:

    public void login(ActionEvent actionEvent) {
    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;
    boolean loggedIn = false;

    if(username != null && username.equals("rod") && password != null && password.equals("a564de63c2d0da68cf47586ee05984d7")) {
    loggedIn = true;
    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
    } else {
    loggedIn = false;
    msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
    }

    FacesContext.getCurrentInstance().addMessage(null, msg);
    context.addCallbackParam("loggedIn", loggedIn);
    }

    then say message welcome and close dialog and no get seesion . but if add this line in method:

    try {
    doLogin();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ServletException e) {
    e.printStackTrace();
    }

    then not say message and not close dialog and not go to adminPage.xhtml :((((((

    I think should be the other way getSession for login but I do not know.

    please help me .

    Thank you :)

    #22590

    I don’t think your problems are related to PrettyFaces at all. It seems more like a problem with the overall interaction between the different components involved in the login process.

    One of the key things you should check is the following:

    If you invoke an action method and return pretty:someId there, you should definitely be redirected to the target page. If this doesn’t work, you should use Firebug to have a look at the AJAX response you get from the server.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Comments are closed.