是否有可能以编程方式使用Spring Security进行身份验证并使其持久化?

Mat*_*ble 5 java security spring spring-security

我在Spring Controller中使用以下方法来允许通过Ajax进行身份验证.它可以工作,但似乎没有创建cookie或任何使身份验证持久的东西.

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能使身份验证持久化?

Bor*_*ner 3

确保

  • 你已经SecurityContextPersistenceFilter配置了
  • 您没有将标签create-session的属性设置为。<security:http>none