1

I m using primefaces and spring security the method below inside my backing bean works well.It invokes user details service and authenticates or rejects login attempts. My problem is with redirection. What is the proper way of redirecting a user to the desired page after auth? Currently I can see that the user is authenticated but still the login form is displayed.

public String login(){

    try{
        Authentication request = new UsernamePasswordAuthenticationToken(this.getUsername(), this.getPassword());
        Authentication result = authenticationManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(result);
    }
    catch(Exception e){

        e.printStackTrace();
        return "incorrect";
    }
 return "correct"; 

}



<http auto-config="true">

     <intercept-url pattern="/web/*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
     <intercept-url pattern="/**" access="ROLE_USER" />

     <form-login login-page="/web/login.xhtml" 
      default-target-url="/main.xhtml"
     always-use-default-target="true"     />

</http>

<authentication-manager alias="authenticationManager">

        <authentication-provider  user-service-ref="kullaniciDetayServisi" />

</authentication-manager>




</beans:beans>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
</h:head>
<h:body>
    <div align="center" style="">
        <h:form  id="loginFormId" prependId="false">
                <div id="loginFieldsPnlId">
                    <div id="loginFieldUsrContId">
                        <h:outputText id="outTxtUserNameId" value="Username: " name="outTxtUserNameNm"></h:outputText>
                        <h:inputText id="userName" required="true" value="#{loginBean.username}" requiredMessage="Please enter username"></h:inputText>
                        <h:outputLabel id="outLblUserNameId" for="userName" name="outLblUserNameNm"></h:outputLabel>
                    </div>
                    <div id="loginFieldPassContId">
                        <h:outputText id="outTxtPasswordId" value="Password: " name="outTxtPasswordNm"></h:outputText>
                        <h:inputSecret id="password"  required="true" value="#{loginBean.password}" requiredMessage="Please enter password" name="inTxtPasswordNm"></h:inputSecret>
                        <h:outputLabel id="outLblPasswordId" for="password" name="outLblPasswordNm"></h:outputLabel>
                    </div>
                </div>
                <div id="loginBtnPanelId">
                    <h:commandButton id="btnLoginId" value="Login" action="#{loginBean.login}"  ajax="false"></h:commandButton>
                    <h:commandButton id="btnCancelId" value="Cancel" action="#{loginBean.cancel}"   immediate="true" update="loginFormId" ajax="false"></h:commandButton>
                </div>
        </h:form>
    </div>
    <div>
        <h:messages></h:messages>
    </div>
</h:body>
</html>
Kukeltje
  • 12,085
  • 4
  • 21
  • 46
desperado06
  • 352
  • 2
  • 13

1 Answers1

1

Looks like you forgot to map the correct and incorrect path inside controller methods.

If I want to redirect than I might use

return "redirect:/correct/" + user.getUserName();

And handle this new request in a new controller method that map this new request. or

return "redirect:/incorrect"

Return something like that from the controller method that handle login request.

or

Using proper navigation rules in /web/WEB-INF/faces-config.xml for JSF like this:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>incorrect</from-outcome>
            <to-view-id>/failure.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>correct</from-outcome>
            <to-view-id>/sucess.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>
abcdef12
  • 1,003
  • 1
  • 10
  • 22
  • 1
    return "/main.xhtml?faces-redirect=true"; made the trick for me. Thank you Rohit.You made my day. – desperado06 Jan 26 '16 at 14:24
  • ` /greeting.xhtml success /response.xhtml ` For JSF specific details look at this official [documentation](https://docs.oracle.com/javaee/7/tutorial/jsf-intro005.htm). Should not be using this trick. Should be handling event properly. – abcdef12 Jan 26 '16 at 16:29
  • `redirect:` is a Spring MVC related artifact which cannot be used with JSF. – Tiny Jan 27 '16 at 18:32
  • Spring MVC is a completely different thing. It is not equal to Spring. – Tiny Jan 28 '16 at 21:53