spring boot获取session的值为null问题及解决方法
问题描述:
我在登陆的时候,登陆成功后将name存进了session,然后在获取个人信息时取出session里的name的值为null.
在login里存session:
在getUserInfo里获取session的name:
控制台结果:
直接原因:
两个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跨域问题
到此这篇关于spring boot获取session的值为null问题的文章就介绍到这了,更多相关spring boot获取session的值为null内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
关于Shiro过滤器配置方式(ShiroFilterFactoryBean)
这篇文章主要介绍了关于Shiro过滤器配置方式(ShiroFilterFactoryBean),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
最新评论