Validation not happening for custom login page in Spring security -
i trying execute sample example spring security using custom login page.the application not throwing error.from home.jsp, navigating custom login page after entering correct user id , password going home.jsp instead of hello.jsp without showing error.requesting in identifying error.
hello.jsp
<%@ taglib prefix="c" uri="/web-inf/tld/c.tld" %> <html> <body> <h3>message : ${message}</h3> <h3>user name : ${username}</h3> <a href = "<c:url value="/j_spring_security_logout" />">logout</a> </body> </html> home.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> <h3>welcome spring security</h3> <a href="welcome"><b>click here logon</b></a> </body> </html> login.jsp
<%@ taglib prefix="c" uri="/web-inf/tld/c.tld"%> <html> <head> <title>login page</title> <style> .errorblock { color: #ff0000; background-color: #ffeeee; border: 3px solid #ff0000; padding: 8px; margin: 16px; } </style> </head> <body onload='document.f.j_username.focus();'> <h3>login username , password (custom page)</h3> <c:if test="${spring_security_last_exception !=null}"><!-- ${not empty error} --> <div class="errorblock"> login attempt not successful, try again.<br /> caused : ${sessionscope["spring_security_last_exception"].message} </div> </c:if> <form name='f' action="<c:url value='j_spring_security_check' />" method='get'> <table> <tr> <td>user:</td> <td><input type='text' name='j_username' value=''> </td> </tr> <tr> <td>password:</td> <td><input type='password' name='j_password' /> </td> </tr> <tr> <td colspan='2'><input name="submit" type="submit" value="submit" /> </td> </tr> <tr> <td colspan='2'><input name="reset" type="reset" /> </td> </tr> </table> </form> </body> </html> web.xml
<?xml version="1.0"?> <web-app xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" id="webapp_id"> <display-name>spring mvc</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/mvc-dispatcher-servlet.xml,/web-inf/spring-security.xml </param-value> </context-param> <filter> <filter-name>springsecurityfilterchain</filter-name> <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter> <filter-mapping> <filter-name>springsecurityfilterchain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> mvc-dispatcher-servlet.xml
<?xml version="1.0"?> <beans xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans"> <context:component-scan base-package="test"/> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/web-inf/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="messagesource" class="org.springframework.context.support.resourcebundlemessagesource"> <property name="basenames"> <list> <value>mymessages</value> </list> </property> </bean> </beans> logincontroller.java
package test; import java.security.principal; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class logincontroller { @requestmapping(value = "/welcome", method = requestmethod.get) public string printwelcome(modelmap model,principal principal) { system.out.println("*****hello controller called***"); string name = principal.getname(); model.addattribute("username", name); model.addattribute("message","spring security custom form example"); return "hello"; } @requestmapping(value = "/login", method = requestmethod.get) public string login(modelmap model) { return "login"; } @requestmapping(value = "/loginfailed", method = requestmethod.get) public string loginerror(modelmap model) { model.addattribute("error","true"); return "login"; } @requestmapping(value = "/logout", method = requestmethod.get) public string logout(modelmap model) { return "login"; } @requestmapping(value = "/*", method = requestmethod.get) public string home(modelmap model) { return "home"; } } spring-security.xml
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd"> <!-- <http auto-config="true"> <intercept-url pattern="/welcome*" access="hasrole('role_user')" /> <http-basic /> </http> --> <!-- <http> <intercept-url pattern="/welcome*" access="hasrole('role_user')"/> <form-login/> <logout logout-success-url="/home"/> </http> --> <http> <intercept-url pattern="/welcome*" access="hasrole('role_user')"/> <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url ="/loginfailed" /> <logout logout-success-url="/logout"/> <csrf disabled="true"/> </http> <authentication-manager> <authentication-provider> <user-service> <user name="rahul" password="123" authorities="role_user" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
try add parameters spring security form login make sure named requests parameters correct.
<form-login login-page="/login" username-parameter="j_username" password-parameter="j_password" default-target-url="/welcome" authentication-failure-url ="/loginfailed" /> also make sure change form:
<form name='loginform' action="<c:url value='j_spring_security_check' />" method='post'> also logout should post, not supported logout. don't need it. like:
<c:url value="/j_spring_security_logout" var="logouturl" /> <form action="${logouturl}" method="post" id="logoutform"> </form>
Comments
Post a Comment