Python 官方文档:入门教程 => 点击学习
目录spring中普通类获取session项目中日志功能解决方案SpringBoot中使用session基础知识在 Spring Boot中使用总结Spring中普通类获取sessi
记录所有通过后台操作的记录,记录项有
时间、操作员、功能模块、操作内容
写了一个基于注解方式的aop,实现日志功能
使用shiro的话可以直接获取到用户的token,然后获取到用户登录信息
可是项目中没有使用token,使用的是session绑定登录信息,需要在@Aspect中获取到用户信息
public class ServletUtil {
public static ServletRequestAttributes getRequestAttributes() {
return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
}
public static httpservletRequest getRequest() {
return getRequestAttributes().getRequest();
}
}
Cookie
Session
设置useHttpOnly
jar 项目
在配置文件中设置:
server.servlet.session.cookie.http-only=false
war项目
在context.xml文件中的Context添加属性useHttpOnly
<Context useHttpOnly="false"><Context>
设置Cookie
场景:首次请求时跳转到指定页面,页面再通过ajax向后台请求数据,进行页面渲染。跳转页面时,需要指定cookie,否则会出现项目部署到外部tomcat时,cookie无法写入的情况,造成ajax请求时,两次的sessionId不一致。
@RequestMapping(value="/query", method=RequestMethod.GET)
public ModeAndView queryInfo(@RequestParam(value="sq")String sq,
HttpServletRequest request,
HttpServletResponse response
) {
ModeAndView view = new ModeAndView();
//... view 操作
Cookie[] cookies = request.getCookies();
String sessionId = request.getSession().getId();
if (cookies == null) {
Cookie cookie = new Cookie("jsESSIONID", sessionId);
cookie.setMaxAge(5 * 60);
cookie.setDomian("localhost");
cookie.setPath("/hand");
response.addCookie(cookie);
} else {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("JSESSIONID")) {
cookie.setMaxAge(5 * 60);
cookie.setValue(sessionId);
cookie.setDomain("localhost");
cookie.setPath("/hand");
response.addCookie(cookie);
break;
}
}
}
return view;
}
ajax 请求时带上cookie
在请求列表中加上xhrFields参数。
var s = $.ajax({
type: "get",
timeout: 8000,
xhrFields: { withCredentials: true},
url: "/hand/val",
data: {"params": ""},
success: function(rel) {
console.log(rel)
},
error: function() {
console.log("failed")
},
complete: function(XMLHttpRequest, status) {
if (status == "timeout") {
s.abort();
console.log("timeout");
}
}
})
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springboot普通类中如何获取session问题
本文链接: https://www.lsjlt.com/news/177522.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0