189 8069 5689

springboot实战教程之shirosession过期时间详解

前言

创新互联是专业的文县网站建设公司,文县接单;提供成都网站建设、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行文县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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

ShiroSessionFilter

/** 
 * 通过拦截器设置shiroSession过期时间
 * @author yangwk 
 */ 
public class ShiroSessionFilter implements Filter { 
 private static Logger logger = LoggerFactory.getLogger(ShiroSessionFilter.class);
 
 public List excludes = new ArrayList();
 
 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() {} 
 
}

注册filter

在被@Configuration注解标注的类内注册ShiroSessionFilter。

@Value("${server.session.timeout}")
private String serverSessionTimeout;

@Bean
public FilterRegistrationBean shiroSessionFilterRegistrationBean() {
 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
 filterRegistrationBean.setFilter(new ShiroSessionFilter());
 filterRegistrationBean.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
 filterRegistrationBean.setEnabled(true);
 filterRegistrationBean.addUrlPatterns("/*");
 Map initParameters = Maps.newHashMap();
 initParameters.put("serverSessionTimeout", serverSessionTimeout);
 initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
 filterRegistrationBean.setInitParameters(initParameters);
 return filterRegistrationBean;
}

这样当每次请求时,如果用户已登录,就重新设置shiro session有效期,从而和server session保持了一致。

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。


网站栏目:springboot实战教程之shirosession过期时间详解
文章网址:http://cdxtjz.com/article/gddiee.html

其他资讯