我是 Spring 安全的新手,所以我尝试使用 Spring MVC 来处理无效登录,但最终出现 Page Not Found 404
在我的 security-context.xml 中,我有这个AuthenticationProvider
处理所有的身份验证登录,所以基本上只是检查用户的帐户和密码,但由于某些原因,authentication-failure-url
只要有无效的登录尝试,它就会一直说找不到 404。
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="AuthenticationProvider"/>
</security:authentication-manager>
<bean id="preAuthenticationFilter"
class="authentication.PreAuthenticationFilter"
p:authenticationManager-ref="authenticationManager" />
<security:http auto-config="true">
<security:intercept-url pattern="/member/**" access="MEMBER" requires-channel="https"/>
<security:form-login login-page="/login"
username-parameter="email"
password-parameter="password"
default-target-url="/member/"
authentication-failure-url="/loginfailed" />
<security:custom-filter position="PRE_AUTH_FILTER" ref="preAuthenticationFilter" />
</security:http>
但我确实有一个相应的控制器监听该 url 模式以处理无效登录。
@Controller
public class LoginController
{
@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
public String loginError(ModelMap model)
{
model.addAttribute("error", "true");
return "login";
}
}
** * *更新* ** * *
在某些AuthenticationProvider
时候,只要用户的凭据不正确,我就会验证用户并抛出异常(我不知道这是否重要)
@Component
public class AuthenticationProvider{
private User validateUser(String userName, String password)
{
try{
//authenticate user's info
.......
}
catch (UnauthorizedAccessException e)
{
throw new BadCredentialsException(e);
}
}
}