spring boot實(shí)戰(zhàn)教程之shiro session過(guò)期時(shí)間詳解
前言
眾所周知在spring boot內(nèi),設(shè)置session過(guò)期時(shí)間只需在application.properties內(nèi)添加server.session.timeout配置即可。在整合shiro時(shí)發(fā)現(xiàn),server.session.timeout設(shè)置為7200,但未到2小時(shí)就需要重新登錄,后來(lái)發(fā)現(xiàn)是shiro的session已經(jīng)過(guò)期了,shiro的session過(guò)期時(shí)間并不和server.session.timeout一致,目前是采用filter的方式來(lái)進(jìn)行設(shè)置。
ShiroSessionFilter
/**
* 通過(guò)攔截器設(shè)置shiroSession過(guò)期時(shí)間
* @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() {}
}
注冊(cè)filter
在被@Configuration注解標(biāo)注的類內(nèi)注冊(cè)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;
}
這樣當(dāng)每次請(qǐng)求時(shí),如果用戶已登錄,就重新設(shè)置shiro session有效期,從而和server session保持了一致。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Shiro中session超時(shí)頁(yè)面跳轉(zhuǎn)的處理方式
- spring boot整合redis實(shí)現(xiàn)shiro的分布式session共享的方法
- Spring Boot集成Shiro并利用MongoDB做Session存儲(chǔ)的方法詳解
- Shiro+Redis實(shí)現(xiàn)登錄次數(shù)凍結(jié)的示例
- springboot整合shiro登錄失敗次數(shù)限制功能的實(shí)現(xiàn)代碼
- SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
- Shiro實(shí)現(xiàn)session限制登錄數(shù)量踢人下線功能
相關(guān)文章
JAVA設(shè)置手動(dòng)提交事務(wù),回滾事務(wù),提交事務(wù)的操作
這篇文章主要介紹了JAVA設(shè)置手動(dòng)提交事務(wù),回滾事務(wù),提交事務(wù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Java和c語(yǔ)言隨機(jī)數(shù)Random代碼詳細(xì)
這篇文章主要介紹Java和c語(yǔ)言得隨機(jī)數(shù)Random,隨機(jī)數(shù)的用處在生活中比較少見(jiàn),但是用處并不少,比如一些小游戲的制作等等。下面我們就一起來(lái)學(xué)習(xí)這篇關(guān)于Java和c隨機(jī)數(shù)Random得文章吧2021-10-10
Java多線程實(shí)現(xiàn)Callable接口
本文給大家分享的是使用Java多線程來(lái)實(shí)現(xiàn)callable接口的方法,以及使用方法,另外還有一個(gè)網(wǎng)友的實(shí)例,希望能夠?qū)Υ蠹艺莆認(rèn)ava多線程有所幫助。2016-06-06
詳解Java創(chuàng)建多線程的四種方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了Java創(chuàng)建多線程的四種方式以及優(yōu)缺點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例
注冊(cè)頁(yè)面是很多網(wǎng)站都會(huì)是使用的到,本文主要介紹了JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
SpringBoot中使用HTTP客戶端工具Retrofit
這篇文章主要為大家介紹了SpringBoot中使用HTTP客戶端工具Retrofit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

