有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是否可以通过编程方式使用Spring安全性进行身份验证并使其持久化?

我在Spring控制器中使用以下方法来允许通过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);
    }
}

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


共 (1) 个答案

  1. # 1 楼答案

    确定

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