spring boot获取session的值为null问题

问题描述:
我在登陆的时候,登陆成功后将name存进了session,然后在获取个人信息时取出session里的name的值为null.

在login里存session:
spring boot获取session的值为null问题_第1张图片
在getUserInfo里获取session的name:
spring boot获取session的值为null问题_第2张图片
控制台结果:
在这里插入图片描述

直接原因:
两个session对象不同,即session.getId()的值不同
根本原因:
涉及前端传参没有携带cookie,后端不能证明是同一会话,故建的session不是同一个对象
解决方案:

本人前端是vue项目,故在main.js里添加:

axios.defaults.withCredentials = true

后端spring boot项目中添加:

CorsFilter.class
package com.postcode.item;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/*", filterName = "CorsFilter")
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest reqs = (HttpServletRequest) req;
        String curOrigin = reqs.getHeader("Origin");
        response.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }


    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {}

}
CorsFilter.class
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/*", filterName = "CorsFilter")
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest reqs = (HttpServletRequest) req;
        String curOrigin = reqs.getHeader("Origin");
        response.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }


    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {}

}
最后结果:

在之前的基础上多打印了session.getId(),可以看到两个session的id相同,并且session里的name值也取到了
在这里插入图片描述

参考:

cookie相关
Access-Control-Allow-Origin与跨域
Spring Boot如何解决前端的Access-Control-Allow-Origin跨域问题

你可能感兴趣的:(java,开发坑)