简体   繁体   中英

Spring security login form always redirect to authfailed

I am trying to apply spring security to a login form. Here is the login form:

<form:form action="j_spring_security_check" method="post" modelAttribute="userLogin">
<p>UserName: </p> <form:input type="text" path="name" />
<p>Password: </p> <form:input type="password" path="password" /><br>
<input type="submit" value="Sign In">
</form:form>

And here is the spring security xml

<http auto-config="true">
    <access-denied-handler />
    <intercept-url pattern="/user**" access="ROLE_USER" />
    <intercept-url pattern="/admin**" access="ROLE_ADMIN" />
    <form-login login-page='/login' username-parameter="username"
        password-parameter="password" default-target-url="/mainPage"
        authentication-failure-url="/login?authfailed" />
    <logout logout-success-url="/login?logout" />
</http>
<authentication-manager> <authentication-provider> <user-service> <user 
        name="user" password="user@123" authorities="ROLE_ADMIN" /> </user-service> 
        </authentication-provider> </authentication-manager>

When i submit the form, it is redirected to the url ' http://localhost:8080/SecurityTest/login?authfailed ' even if i entered the right user name and password.

Try

<spring:url value="/j_spring_security_check" var="action" />

<form:form action="${action}" method="post" modelAttribute="userLogin">

I found the answer, the problem was in the path variable in the form:input tag. It should be named username while i was naming it name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM