shiro springboot 會話管理

一 概要

Shiro提供了完整的企業(yè)級會話管理功能,不依賴于底層容器(如Tomcat)赋续,不管是J2SE還是J2EE環(huán)境都可以使用干奢,提供了會話管理,會話事件監(jiān)聽糯彬,會話存儲/持久化凭语,容器無關(guān)的集群,失效/過期支持撩扒,對Web的透明支持似扔,SSO單點登錄的支持等特性吨些。即直接使用 Shiro 的會話管理可以直接替換如 Web 容器的會話管理。

shiro-redis開源項目已經(jīng)很好的將shiro與redis整合到一起炒辉,實現(xiàn)了將session存入redis豪墅,可以方便的用于session共享實現(xiàn)集群部署。

github

主要涉及到緩存跟session的配置

二 使用步驟

導(dǎo)入依賴

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n9" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>3.2.3</version>
</dependency></pre>

配置會話管理器

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n11" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//session管理
@Bean
public SessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
Collection<SessionListener> listeners = new ArrayList<SessionListener>();
listeners.add(new ShiroSessionListener());
// 配置自定義監(jiān)聽,比如統(tǒng)計網(wǎng)站在線人數(shù)等
sessionManager.setSessionListeners(listeners);
//設(shè)置redisSessionDao
sessionManager.setSessionDAO(redisSessionDAO());
//設(shè)置session超時時間為1小時(單位毫秒)
//sessionManager.setGlobalSessionTimeout(3600000);
sessionManager.setGlobalSessionTimeout(-1);//永不超時
return sessionManager;
}</pre>

配置Reids緩存管理器

將緩存信息保存到Redis中

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n15" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Bean
public RedisCacheManager cacheManager(){
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
//redis中針對不同用戶緩存
redisCacheManager.setPrincipalIdFieldName("username");
//用戶權(quán)限信息緩存時間
redisCacheManager.setExpire(200000);
return redisCacheManager;
}</pre>

配置RedisSessionDAO

SessionDAO的作用是為Session提供CRUD并進行持久化的一個shiro組件

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Bean
public RedisSessionDAO redisSessionDAO() {
RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
redisSessionDAO.setRedisManager(redisManager());
return redisSessionDAO;
}</pre>

配置RedisCacheManager

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//配置cacheManager
public RedisCacheManager cacheManager(RedisManager redisManager) {
RedisCacheManager redisCacheManager = new RedisCacheManager();
//redis中針對不同用戶緩存
redisCacheManager.setPrincipalIdFieldName("username");
//用戶權(quán)限信息緩存時間
redisCacheManager.setExpire(200000);
redisCacheManager.setRedisManager(redisManager);
return redisCacheManager;
}</pre>

配置redisManager

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public RedisManager redisManager() {
RedisManager redisManager = new RedisManager();
redisManager.setHost(host);
redisManager.setPort(port);
redisManager.setTimeout(timeout);
redisManager.setPassword(password);
redisManager.setExpire(3600);//配置緩存過期時間(秒)
return redisManager;
}</pre>

配置緩存認(rèn)證信息

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Bean
public UserRealm realm(HashedCredentialsMatcher hashedCredentialsMatcher) {
UserRealm userRealm = new UserRealm();
userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
userRealm.setCachingEnabled(true);
//啟用身份驗證緩存黔寇,即緩存AuthenticationInfo信息偶器,默認(rèn)false
userRealm.setAuthenticationCachingEnabled(true);
//緩存AuthenticationInfo信息的緩存名稱
userRealm.setAuthenticationCacheName("authenticationCache");
//啟用授權(quán)緩存,即緩存AuthorizationInfo信息缝裤,默認(rèn)false
userRealm.setAuthorizationCachingEnabled(true);
//緩存AuthorizationInfo信息的緩存名稱
userRealm.setAuthorizationCacheName("authorizationCache");
return userRealm;
}</pre>

配置會話ID生成器

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Bean
public SessionIdGenerator sessionIdGenerator() {
return new JavaUuidSessionIdGenerator();
} </pre>

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="java" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class ShiroSessionListener implements SessionListener{

/**
 * 統(tǒng)計在線人數(shù)
 */
private final AtomicInteger sessionCount = new AtomicInteger(0);

/**
 * 會話創(chuàng)建時觸發(fā)
 * @param session
 */
@Override
public void onStart(Session session) {
    //會話創(chuàng)建屏轰,在線人數(shù)加一
    sessionCount.incrementAndGet();
}

/**
 * 退出會話時觸發(fā)
 * @param session
 */
@Override
public void onStop(Session session) {
    //會話退出,在線人數(shù)減一
    sessionCount.decrementAndGet();
}

/**
 * 會話過期時觸發(fā)
 * @param session
 */
@Override
public void onExpiration(Session session) {
    //會話過期,在線人數(shù)減一
    sessionCount.decrementAndGet();
}
/**
 * 獲取在線人數(shù)使用
 * @return
 */
public AtomicInteger getSessionCount() {
    return sessionCount;
}

}</pre>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市憋飞,隨后出現(xiàn)的幾起案子霎苗,更是在濱河造成了極大的恐慌,老刑警劉巖榛做,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唁盏,死亡現(xiàn)場離奇詭異,居然都是意外死亡检眯,警方通過查閱死者的電腦和手機厘擂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來轰传,“玉大人驴党,你說我怎么就攤上這事』癫纾” “怎么了港庄?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長恕曲。 經(jīng)常有香客問我鹏氧,道長,這世上最難降的妖魔是什么佩谣? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任把还,我火速辦了婚禮,結(jié)果婚禮上茸俭,老公的妹妹穿的比我還像新娘吊履。我一直安慰自己,他們只是感情好调鬓,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布艇炎。 她就那樣靜靜地躺著,像睡著了一般腾窝。 火紅的嫁衣襯著肌膚如雪缀踪。 梳的紋絲不亂的頭發(fā)上居砖,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機與錄音驴娃,去河邊找鬼奏候。 笑死,一個胖子當(dāng)著我的面吹牛唇敞,可吹牛的內(nèi)容都是我干的蔗草。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼疆柔,長吁一口氣:“原來是場噩夢啊……” “哼蕉世!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起婆硬,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奸例,沒想到半個月后彬犯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡查吊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年谐区,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逻卖。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡宋列,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出评也,到底是詐尸還是另有隱情炼杖,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布盗迟,位于F島的核電站坤邪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏罚缕。R本人自食惡果不足惜艇纺,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望邮弹。 院中可真熱鬧黔衡,春花似錦、人聲如沸腌乡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽导饲。三九已至捞高,卻和暖如春氯材,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背硝岗。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工氢哮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人型檀。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓冗尤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胀溺。 傳聞我的和親對象是個殘疾皇子裂七,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355