Spring security - resource j_spring_security_check not avaiable -
i trying build spring based web application , start configuring simple authentication system based on username & password tuples stored in database table.
it understanding can achieved using spring security, cannot work.
the following web.xml file.
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/servlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
follows servlet-context.xml file. bob , sam users there testing purposes. after right switch jdbc based user service.
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemalocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <sec:http use-expressions="true"> <sec:intercept-url pattern="/**" access="permitall" /> <sec:form-login login-page="/home.html" login-processing-url="/j_spring_security_check" authentication-failure-url="/login-error.html" default-target-url="/welcome.html" /> <sec:logout logout-success-url="/home.html" /> </sec:http> <sec:authentication-manager> <sec:authentication-provider> <sec:password-encoder hash="md5"/> <sec:user-service> <sec:user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="role_supervisor, role_user" /> <sec:user name="sam" password="d1a5e26d0558c455d386085fad77d427" authorities="role_user" /> </sec:user-service> </sec:authentication-provider> </sec:authentication-manager> <context:component-scan base-package="cz.dusanrychnovsky.whattoreadnext" /> <mvc:annotation-driven /> </beans>
this home controller.
@controller public class homecontroller { @requestmapping(value = "/home.html") public string home() { return "home"; } @requestmapping(value = "/login-error.html") public string loginerror(model model) { model.addattribute("loginerror", true); return "home"; } }
and thymeleaf based view.
<!doctype html system "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>contacts</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <div id="content"> <h1>welcome site!</h1> <p th:if="${loginerror}">wrong user or password</p> <form th:action="@{/j_spring_security_check}" method="post"> <label for="j_username">email address</label>: <input type="text" id="j_username" name="j_username" /> <br /> <label for="j_password">password</label>: <input type="password" id="j_password" name="j_password" /> <br /> <input type="submit" value="log in" /> </form> </div> </body> </html>
when deploy war file local tomcat installation , visit http://localhost:8080/test/home.html
url, home page opens fine. when fill in form, though, gets submitted http://localhost:8080/test/j_spring_security_check
, 404 - requested resource () not available.
error.
what doing wrong? please bear me i'm newcomer both spring mvc/security , thymeleaf.
you cannot configure spring security in
servlet-context.xml
, becauseservlet-context.xml
belongs specificdispatcherservlet
, spring security filter works before request reaches servlet.you need create root application context using
contextloaderlistener
, put spring security configuration there.actually, long don't need separate
servlet-context.xml
,applicationcontext.xml
, i'd suggest moveservlet-context.xml
applicationcontext.xml
, leaveservlet-context.xml
empty (that is, leave<beans>
element empty).