我在Spring控制器中使用以下方法来允许通过Ajax进行身份验证。它的工作原理,但似乎并没有创建一个cookie或任何认证持久化的东西。是否可以用Spring Security进行编程验证并使其持久化?
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public LoginStatus login(@RequestParam("j_username") String username,
@RequestParam("j_password") String password) {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
try {
Authentication auth = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(auth);
return new LoginStatus(auth.isAuthenticated(), auth.getName());
} catch (BadCredentialsException e) {
return new LoginStatus(false, null);
}
}
我需要做什么才能使认证持久化?
马特 - 你有没有想过这一个?我正在尝试做同样的事情。想要从Spring Security论坛尝试下面的代码:http://forum.springsource.org/showthread.php?99183-Automatic-login-after-account-creation – les2 2011-07-19 14:35:18
是的,请参阅以下评论和发布解决方案:http ://raibledesigns.com/rd/entry/implementing_ajax_authentication_using_jquery#comment-1300943308000 – 2011-10-06 15:56:42