展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

配置session会话超时时长,自定义session失效处理

  • 在demo05的基础上开发

  • 认证时勾选记住我,认证成功后,会在应用程序中生成remember-me和JSESSIONID

  • 数据库中会生成1条记录

  • 为了演示session的作用,所以在之后的测试中不勾选记住我

  • 在web模块的yml中设置session会话超时时长

server:
servlet:
session:
timeout: 1m # session会话超时时间,默认情况 下是30分钟(m),不能小于1分钟
  • 设置为1分钟后,访问success接口,该接口需要认证,认证成功后,会跳转到success.html

  • 无操作1分钟后,刷新页面,这时又跳转到认证页面,这是因为会话超时了

  • session会话超时后的处理

  • 编写会话超时的处理类,需实现InvalidSessionStrategy接口

import com.ychen.security.result.MengxueguResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.web.session.InvalidSessionStrategy;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomInvalidSessionStrategy implements InvalidSessionStrategy {
// session会话超时时,返回json字符串
@Override
public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 返回的结果集
MengxueguResult result = new MengxueguResult().build(HttpStatus.UNAUTHORIZED.value(), "登录已超时,请重新登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(result.toJsonString());
}
}
  • 配置到容器中
import com.ychen.security.authentication.CustomInvalidSessionStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.session.InvalidSessionStrategy;
/**
* 主要为容器中添加Bean实例
*/
@Configuration
public class SecurityConfigBean {
// 如果容器中还有InvalidSessionStrategy的实现类,则使用新的实现类
// 如果没有,则使用CustomInvalidSessionStrategy来处理session会话超时
@Bean
@ConditionalOnMissingBean(InvalidSessionStrategy.class)
public InvalidSessionStrategy invalidSessionStrategy() {
return new CustomInvalidSessionStrategy();
}
}
  • 配置到security配置类中
# 注入
@Autowired
private InvalidSessionStrategy invalidSessionStrategy;
# 配置
.and()
.sessionManagement() // session管理
.invalidSessionStrategy(invalidSessionStrategy)
  • 这是不管访问哪个接口,发现都提示访问超时

  • 因为浏览器中保存了JSESSIONID

  • 重新打开浏览器,或者删除JSESSIONID,才能再次认证

  • 修改会话超时的处理类

public class CustomInvalidSessionStrategy implements InvalidSessionStrategy {
// session会话超时时,返回json字符串
@Override
public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 要将浏览器中的cookie的jsessionid删除
cancelCookie(request, response);
// 返回的结果集
MengxueguResult result = new MengxueguResult().build(HttpStatus.UNAUTHORIZED.value(), "登录已超时,请重新登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(result.toJsonString());
}
// 删除cookie的方法
protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = new Cookie("JSESSIONID", null);
cookie.setMaxAge(0);
cookie.setPath(getCookiePath(request));
response.addCookie(cookie);
}
// 获取cookie路径的方法
private String getCookiePath(HttpServletRequest request) {
String contextPath = request.getContextPath();
return contextPath.length() > 0 ? contextPath : "/";
}
}
  • 再次认证

  • 超过1分钟后,提示超时,并自动删除cookie

posted @   DogLeftover  阅读(499)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示