JSESSIONID未在浏览器中创建

时间:2019-10-30 12:20:44

标签: java spring-boot session

下午好,我有一些奇怪的问题。 我的程序在springboot / angular上运行。 当我在不安全的浏览器上运行程序时(在我的情况下为chrome) 一切正常。 当我在全局浏览器中运行程序时,登录后再也不会创建会话。

package zoolpon.project.rest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import zoolpon.project.entities.CustomLogin;
import zoolpon.project.entities.Login;
import zoolpon.project.exceptions.InvalidLoginException;
import zoolpon.project.services.LoginService;

@CrossOrigin("http://localhost:4200")
@RestController
@RequestMapping("Login")
@Validated
public class LoginWebService {

    @Autowired
    private LoginService loginService;

    @Autowired
    Logger logger;

    @PostMapping
    public boolean Login(@RequestBody Login login, HttpServletRequest request, HttpServletResponse response) {

        try {
            CustomLogin customLogin = loginService.login(login);
            HttpSession session = request.getSession(true);
            session.setAttribute("FACADE", customLogin);
            logger.info(login.getUserName() + "Just logged in.");
            return true;
        } catch (InvalidLoginException e) {
            logger.info("Something went wrong , might be wrong userName or password.");
            return false;
        }

    }

    @GetMapping
    public boolean logOut(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession(false);
            session.removeAttribute("FACADE");
            session.invalidate();
            logger.info("logout succesfully.");
            return true;

        } catch (Exception e) {
            return false;
        }

    }
}

**不要忘记** 程序运行并且可以100%使用不安全的浏览器,为什么全局浏览器无法保存/创建我的会话? 谢谢你的帮手:)

0 个答案:

没有答案