会话奇怪地返回null

时间:2017-08-17 10:54:47

标签: javascript java spring angular spring-mvc

我将数据保存到会话中,但后来我尝试将其读回来,并且它为空。 Spring MVC是我的后端,Angular 4是前端。

爪哇:

@RestController
@CrossOrigin(origins = "http://localhost:3009", allowCredentials = "true")
@RequestMapping(value = "api")
public class RestController {

@Autowired
MainLogic mainLogic;

@RequestMapping(value = "/data", method = RequestMethod.GET)
public List<Data> getData(HttpServletRequest httpServletRequest){

    // user is null here )-:
    User user = (User)httpServletRequest.getSession().getAttribute("user");

    if (user == null) {
        return null;
    }

    return mainLogic.getData();
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public LoginResult logIn(HttpServletRequest httpServletRequest, @RequestParam("username") String username, @RequestParam("password") String password){

    LoginResult result = mainLogic.logIn(username, password);

    if (result.getUser() != null) {
    // user is not null here
        httpServletRequest.getSession().setAttribute("user", result.getUser());
    }

    return result;
}
}

角:

logIn(username: string, password: string): Observable<LoginResult>{
    let result = this.http
      .post(`${this.baseUrl}/login?username=${username}&password=${password}` , {headers: configuration.getHeaders(), withCredentials: true})
      .map(response => response.json());

    return result;
  }

getData(): Observable<Affiliate[]>{
    let results = this.http
      .get(`${this.baseUrl}/data`, {headers: configuration.getHeaders(), withCredentials: true})
      .map(response => response.json());

    return results;
  }

知道我在这里失踪的是什么吗?也许是CORS的东西?

1 个答案:

答案 0 :(得分:0)

你能检查一下是否启用了cookie?