欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2020-11-06 10:12:07|栏目:JAVA代码|点击:

前言

众所周知在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<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() {} 
 
}

注册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<String, String> initParameters = Maps.newHashMap();
 initParameters.put("serverSessionTimeout", serverSessionTimeout);
 initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
 filterRegistrationBean.setInitParameters(initParameters);
 return filterRegistrationBean;
}

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

总结

上一篇:Spring条件注解用法案例分析

栏    目:JAVA代码

下一篇:java获取ip地址示例

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

本文地址:http://www.codeinn.net/misctech/19659.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有