返回顶部
首页 > 资讯 > 精选 >spring boot实战教程之shiro session过期时间详解
  • 556
分享到

spring boot实战教程之shiro session过期时间详解

springbootsession过期shiro 2023-05-31 01:05:41 556人浏览 独家记忆
摘要

前言众所周知在Spring Boot内,设置session过期时间只需在application.properties内添加server.session.timeout配置即可。在整合shiro时发现,server.session.timeo

前言

众所周知在Spring Boot内,设置session过期时间只需在application.properties内添加server.session.timeout配置即可。在整合shiro时发现,server.session.timeout设置为7200,但未到2小时就需要重新登录,后来发现是shiro的session已经过期了,shiro的session过期时间并不和server.session.timeout一致,目前是采用filter的方式来进行设置。

ShiroSessionFilter

 public class ShiroSessionFilter implements Filter {  private static Logger logger = LoggerFactory.getLogger(ShiroSessionFilter.class);  public List<String> excludes = new ArrayList<String>();  private long serverSessionTimeout = 180000L;//ms  public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,ServletException {   if(logger.isDebugEnabled()){   logger.debug("shiro session filter is open");  }    httpservletRequest req = (HttpServletRequest) request;  HttpServletResponse resp = (HttpServletResponse) response;  if(handleExcludeURL(req, resp)){   filterChain.doFilter(request, response);   return;  }    Subject currentUser = SecurityUtils.getSubject();  if(currentUser.isAuthenticated()){   currentUser.getSession().setTimeout(serverSessionTimeout);  }  filterChain.doFilter(request, response); }  private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) {  if (excludes == null || excludes.isEmpty()) {   return false;  }  String url = request.getServletPath();  for (String pattern : excludes) {   Pattern p = Pattern.compile("^" + pattern);   Matcher m = p.matcher(url);   if (m.find()) {    return true;   }  }  return false; } @Override public void init(FilterConfig filterConfig) throws ServletException {  if(logger.isDebugEnabled()){   logger.debug("shiro session filter init~~~~~~~~~~~~");  }  String temp = filterConfig.getInitParameter("excludes");  if (temp != null) {   String[] url = temp.split(",");   for (int i = 0; url != null && i < url.length; i++) {    excludes.add(url[i]);   }  }  String timeout = filterConfig.getInitParameter("serverSessionTimeout");  if(StringUtils.isNotBlank(timeout)){   this.serverSessionTimeout = NumberUtils.toLong(timeout,1800L)*1000L;  } } @Override public void destroy() {}  }

--结束END--

本文标题: spring boot实战教程之shiro session过期时间详解

本文链接: https://lsjlt.com/news/222706.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作