0

I have integrated Spring Security in my application , and would like to display an error message to the user in case of Bad credentials. jsp:

    <c:out value='${secerror}' /> //prints nothing on screen
    <c:if test="${empty secerror}">
    error is empty or null. //always prints this line on screen
    </c:if>
    <c:if test="${not empty secerror}">
    <div class="errorblock">
        Your login attempt was not successful, try again.<br /> Caused :
        ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </div>
    </c:if>
     <c:set var = "url" value = "/j_spring_security_check" />
    <form:form method="post" commandName="portalLogin" action="${pageContext.servletContext.contextPath}${url}" name="f">

[Update]: Sorry all, i realized that my Model object was getting overriden after i redirect to portalLogin.html as i had created a new model object created there previously. But i tried few easy options by which i can pass Model object from one controller method to another method in the same controller. But nothing worked.

  1. I tried using forward: prefix instead of redirect prefix. For this, i didn't get error message at all.
  2. I tried below code in loginerror method.

        return new ModelAndView("portalLogin","secerror","true");
    

I was getting following error for the above code:

        java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'portalLogin' available as request attribute

I did come across this link, but i found it to be a very lengthy solution and wasn't sure if iv'e to write that much code.

I wasn't sure if i can use Model object for @ModelAttribute annotations@ModelAttribute.

Please provide me with code snippets / examples which i can try out.

My controller method is like this:

    @RequestMapping("/portalLogin")
public ModelAndView goToLogin(Model model) {
    try {
        System.out.println("Enter goToLogin************************");
    } catch (Exception exception) {

    }
    return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
    //return new ModelAndView("portalLogin", model);
}

    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public ModelAndView loginerror(ModelMap model) {

    //model.addAttribute("secerror", "true");
    //return "redirect:portalLogin.html";
    return new ModelAndView("portalLogin","secerror","true");

}  

[Update]: As a work around i added goToLogin method logic inside loginerror method as my only intention is to get portalLogin page. Error was thrown as expected.

    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public ModelAndView loginerror(Model model) {
     model.addAttribute("secerror", "true");       
    return new ModelAndView("portalLogin", "portalLogin", new LoginModel());
    }

But still i would like to know if i can pass Model object from one controller method to another method in the same controller through some way.

Community
  • 1
  • 1
coder87
  • 145
  • 1
  • 8
  • 21

2 Answers2

0

Let`s make things easy, if you want to show a Bad credentials message, you can simply do something like this:

In your spring-security.xml:

<sec:form-login login-page="/login.jsp"
    default-target-url="/default-url-when-login-correct"
    authentication-failure-url="/login.jsp?error=true"
    login-processing-url="/login" always-use-default-target="true" />

In your login.jsp:

<c:if test="${not empty param.error}">
    <span>${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}</span>
</c:if>
Dani
  • 3,744
  • 4
  • 27
  • 35
0

You can also try something like this

<c:if test="${not empty secerror == 'true'}">
<tr>
<td colspan="2" align="center"><font style="color: red">Your login attempt was not successful, try again</font>
</td></tr></c:if>
Fahad
  • 393
  • 3
  • 12
  • 33