Python 官方文档:入门教程 => 点击学习
目录session超时退到登录页面1、首先在SpringBoot中追加配置session的超时时间2、登录成功接口中把用户信息追加session中3、在WEBmvcConfig中配置
最近发现使用的工程居然没有session超时机制,功能太欠缺了,现在把追加方法分享出来,里面有一些坑,大家自由使用。
注意springboot2的写法发生了改变
springboot2写法
server:
servlet:
session:
timeout: 1800s
springboot1写法
server:
session:
timeout: 1800s
public ResponseEntity loginGo(httpservletRequest request,String userName, String passWord) {
// 此处省略若干
HttpSession session = request.getSession(true);
session.setAttribute("username", user.getUserRemark());
}
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addViewControllers(ViewControllerReGIStry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/loginOverTime").setViewName("loginOverTime");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**") // 表示拦截所有的请求
.excludePathPatterns("/login", "/loginOverTime", "/register", "/plugins/**", "/javascript/**", "/api/system/user/login","/img/**","/CSS/common/**");
// 表示拦截所有的请求
}
}
这里采用先跳转中转页面loginOverTime,然后再跳转到登录页面,如果直接跳转到登录页面只能在页面的内部iframe中跳转,无法这个页面跳转
@Component
public class LoginInterceptor implements HandlerInterceptor {
Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 获取session
HttpSession session = request.getSession(true);
// 判断用户是否存在,不存在就跳转到登录界面
if(session.getAttribute("user") == null){
response.sendRedirect(request.getContextPath()+"/loginOverTime");
return false;
}else{
session.setAttribute("user", session.getAttribute("user"));
return true;
}
}
}
然后自动跳转到login页面,提升一下用户体验
{% extends 'common/layout' %}
{% block head %}
<link href="{{ request.contextPath }}/css/common/loginOverTime.css" rel="external nofollow" rel="stylesheet" />
{% endblock %}
{% block content %}
<body class="body_bg" >
<div class="show">
<div id="page">
<h1>抱歉,登录超时~</h1>
<h2> </h2>
<font color="#666666">由于您长期未操作为了保证您的信息安全请重新登录!</font><br /><br />
<div align="center" style="color: #666666">
将于<span>3</span>秒后跳转至<a href="javascript:void(0)" rel="external nofollow" >登录页</a>
</div>
</div>
</div>
</body>
{% endblock %}
{% block footer %}
<script type="text/javascript">
$(document).ready(function(){
// 关闭二级菜单
if(parent.window.closeSecondMenu != undefined){
parent.window.closeSecondMenu();
}
// 读秒显示
var second = 3;
// 设置定时任务
window.setInterval("changeTime()", 1000);
// 修改时间
changeTime = function(){
// 时间自动减1
second--;
// 修改页面上显示
$("span").html(second);
// 判断是否跳转登陆页
if(second == 0){
$("a").click();
}
}
// 跳转至登录页
$("a").click(function(){
//window.location.href="{{ request.contextPath }}/login" rel="external nofollow" ;
window.top.location="{{ request.contextPath }}/login";
});
});
</script>
{% endblock %}
这样就实现了sesseion超时退出的问题,大功告成
最近在做SpringBoot的项目,用到了session,发现放置好session后,过一会就失效了,用下面发语句获取session失效时间,发现是60s
request.getSession().getMaxInactiveInterval();
去网上查找,发现大多解决问题的办法是 在启动类中main方法的下面加入以下方法来手动设置session失效时间
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer(){
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer Container) {
container.setSessionTimeout(1800);//单位为S
}
};
}
但是社会在发展,时代在进步,SpringBoot2.0以后已经不支持这种方式了
ps:可以在pom文件中查看你的SpringBooot版本。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
SpringBoot2.0以后的版本只需要在application.properties中加入以下配置就好
server.servlet.session.timeout = PT5H
这里重点解释一下 PT5H 意思是设置session失效的时间是5小时
通过查看setTimeouot的方法,这里要求传入Duration的实例
public void setTimeout(Duration timeout) {
this.timeout = timeout;
}
最后总结一下Duration最实用的一个功能其实是 between 方法,因为有很多时候我们需要计算两个日期之间的天数或者小时数,用这个就可以很方便的进行操作。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springboot2中session超时,退到登录页面方式
本文链接: https://www.lsjlt.com/news/161836.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