日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

仅对此用户禁用 java_Spring Security实现禁止用户重复登陆的配置原理

發布時間:2023/12/10 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 仅对此用户禁用 java_Spring Security实现禁止用户重复登陆的配置原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要介紹了Spring Security實現禁止用戶重復登陸的配置原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

系統使用了Spring Security做權限管理,現在對于系統的用戶,需要改動配置,實現無法多地登陸。

一、SpringMVC項目,配置如下:

首先在修改Security相關的XML,我這里是spring-security.xml,修改UsernamePasswordAuthenticationFilter相關Bean的構造配置

加入

新增sas的Bean及其相關配置

class="org.springframework.security.core.session.SessionRegistryImpl" />

加入ConcurrentSessionFilter相關Bean配置

class="org.springframework.security.web.session.ConcurrentSessionFilter">

class="org.springframework.security.web.session.SimpleRedirectSessionInformationExpiredStrategy">

二、SpringBoot項目

三、Bean配置說明

SessionAuthenticationStrategy:該接口中存在onAuthentication方法用于對新登錄用戶進行session相關的校驗。

查看UsernamePasswordAuthenticationFilter及其父類代碼,可以發現在doFilter中存在sessionStrategy.onAuthentication(authResult, request, response);方法

但UsernamePasswordAuthenticationFilter中的sessionStrategy對象默認為NullAuthenticatedSessionStrategy,即不對session進行相關驗證。

如本文配置,建立id為sas的CompositeSessionAuthenticationStrategy的Bean對象。

CompositeSessionAuthenticationStrategy可以理解為一個托管類,托管所有實現SessionAuthenticationStrategy接口的對象,用來批量托管執行onAuthentication函數

這里CompositeSessionAuthenticationStrategy中注入了三個對象,關注ConcurrentSessionControlAuthenticationStrategy,它實現了對于session并發的控制

UsernamePasswordAuthenticationFilter的Bean中注入新配置的sas,用于替換原本的NullAuthenticatedSessionStrategy

ConcurrentSessionFilter的Bean用來驗證session是否失效,并通過SimpleRedirectSessionInformationExpiredStrategy將失敗訪問進行跳轉。

四、代碼流程說明(這里模擬用戶現在A處登錄,隨后用戶在B處登錄,之后A處再進行操作時會返回失敗,提示重新登錄)

1、用戶在A處登錄,UsernamePasswordAuthenticationFilter調用sessionStrategy.onAuthentication進行session驗證

2、ConcurrentSessionControlAuthenticationStrategy中的onAuthentication開始進行session驗證,服務器中保存了登錄后的session

/**

* In addition to the steps from the superclass, the sessionRegistry will be updated

* with the new session information.

*/

public void onAuthentication(Authentication authentication,

HttpServletRequest request, HttpServletResponse response) {

//根據所登錄的用戶信息,查詢相對應的現存session列表

final List sessions = sessionRegistry.getAllSessions(

authentication.getPrincipal(), false);

int sessionCount = sessions.size();

//獲取session并發數量,對于XML中的maximumSessions

int allowedSessions = getMaximumSessionsForThisUser(authentication);

//判斷現有session列表數量和并發控制數間的關系

//如果是首次登錄,根據xml配置,這里應該是0<1,程序將會繼續向下執行,

//最終執行到SessionRegistryImpl的registerNewSession進行新session的保存

if (sessionCount < allowedSessions) {

// They haven't got too many login sessions running at present

return;

}

if (allowedSessions == -1) {

// We permit unlimited logins

return;

}

if (sessionCount == allowedSessions) {

//獲取本次http請求的session

HttpSession session = request.getSession(false);

if (session != null) {

// Only permit it though if this request is associated with one of the

// already registered sessions

for (SessionInformation si : sessions) {

//循環已保存的session列表,判斷本次http請求session是否已經保存

if (si.getSessionId().equals(session.getId())) {

//本次http請求是有效請求,返回執行下一個filter

return;

}

}

}

// If the session is null, a new one will be created by the parent class,

// exceeding the allowed number

}

//本次http請求為新請求,進入具體判斷

allowableSessionsExceeded(sessions, allowedSessions, sessionRegistry);

}

/**

* Allows subclasses to customise behaviour when too many sessions are detected.

*

* @param sessions either null or all unexpired sessions associated with

* the principal

* @param allowableSessions the number of concurrent sessions the user is allowed to

* have

* @param registry an instance of the SessionRegistry for subclass use

*

*/

protected void allowableSessionsExceeded(List sessions,

int allowableSessions, SessionRegistry registry)

throws SessionAuthenticationException {

//根據exceptionIfMaximumExceeded判斷是否要將新http請求拒絕

//exceptionIfMaximumExceeded也可以在XML中配置

if (exceptionIfMaximumExceeded || (sessions == null)) {

throw new SessionAuthenticationException(messages.getMessage(

"ConcurrentSessionControlAuthenticationStrategy.exceededAllowed",

new Object[] { Integer.valueOf(allowableSessions) },

"Maximum sessions of {0} for this principal exceeded"));

}

// Determine least recently used session, and mark it for invalidation

SessionInformation leastRecentlyUsed = null;

//若不拒絕新請求,遍歷現存seesion列表

for (SessionInformation session : sessions) {

//獲取上一次/已存的session信息

if ((leastRecentlyUsed == null)

|| session.getLastRequest()

.before(leastRecentlyUsed.getLastRequest())) {

leastRecentlyUsed = session;

}

}

//將上次session信息寫為無效(欺騙)

leastRecentlyUsed.expireNow();

}

3、用戶在B處登錄,再次通過ConcurrentSessionControlAuthenticationStrategy的檢查,將A處登錄的session置于無效狀態,并在session列表中添加本次session

4、用戶在A處嘗試進行其他操作,ConcurrentSessionFilter進行Session相關的驗證,發現A處用戶已經失效,提示重新登錄

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

//獲取本次http請求的session

HttpSession session = request.getSession(false);

if (session != null) {

//從本地session關系表中取出本次http訪問的具體session信息

SessionInformation info = sessionRegistry.getSessionInformation(session

.getId());

//如果存在信息,則繼續執行

if (info != null) {

//判斷session是否已經失效(這一步在本文4.2中被執行)

if (info.isExpired()) {

// Expired - abort processing

if (logger.isDebugEnabled()) {

logger.debug("Requested session ID "

+ request.getRequestedSessionId() + " has expired.");

}

//執行登出操作

doLogout(request, response);

//從XML配置中的redirectSessionInformationExpiredStrategy獲取URL重定向信息,頁面跳轉到登錄頁面

this.sessionInformationExpiredStrategy.onExpiredSessionDetected(new SessionInformationExpiredEvent(info, request, response));

return;

}

else {

// Non-expired - update last request date/time

sessionRegistry.refreshLastRequest(info.getSessionId());

}

}

}

chain.doFilter(request, response);

}

5、A處用戶只能再次登錄,這時B處用戶session將會失效重登,如此循環

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的仅对此用户禁用 java_Spring Security实现禁止用户重复登陆的配置原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 啪啪网视频| 少妇aaaa| 久久精品视频无码 | 男生和女生一起搞鸡 | 日日噜噜噜夜夜爽爽狠狠 | 午夜老湿机 | 亚洲精选中文字幕 | 国产在线色站 | 久久深夜福利 | 人妻毛片| 欧美伊人 | 国产xx在线观看 | 西欧free性满足hd老熟妇 | 尤物videos另类xxxx | 亚洲一区欧美 | 国产一区二区视频播放 | 91n视频 | 污污网站免费在线观看 | 国产精品无码永久免费不卡 | 中国性老太hd大全69 | 青青青草视频在线观看 | jizz日本女人| 涩涩视频免费观看 | 香蕉久久精品日日躁夜夜躁 | 国产女主播一区 | 中文日韩在线观看 | 欧美在线观看不卡 | 日本电影成人 | 亚洲一区二区电影 | 中文字幕日韩精品亚洲一区小树林 | 亚洲成人精 | 好吊色视频988gao在线观看 | 国产黄色高清视频 | 午夜视频在线免费看 | 欧美 日韩 国产 在线 | 林雅儿欧洲留学恋爱日记在线 | 久久久久无码国产精品一区李宗瑞 | 少妇精品无码一区二区 | www.夜夜| 网址在线观看你懂的 | ts人妖在线观看 | 色婷婷狠狠18禁久久 | 精品国产乱码久久久久久图片 | 岛国色图| 天天射干 | 日本不卡视频在线观看 | 久久精品在线观看 | 久久9999久久免费精品国产 | 国模丫头1000人体 | 国产系列在线观看 | 在哪里可以看黄色片 | 欧美不卡一二三 | 俺也去婷婷 | 高清一区二区三区视频 | 九九九九精品 | 国产视频一区在线 | 另类男人与善交video | 国产精品一区二区性色av | 美国一区二区三区 | 欧美日本在线看 | 婷婷一级片 | 91久久极品少妇xxxxⅹ软件 | 久久午夜无码鲁丝片 | 国产精品永久免费观看 | 婷婷激情五月 | 性国产精品 | 能看的av| 九月色婷婷| 亚洲一区二区三区四区电影 | 成人av网站在线 | 无码少妇一区二区三区芒果 | 久久午夜片 | 四虎影视成人永久免费观看亚洲欧美 | 国产视频资源 | 女同av网站 | 狠狠五月 | 男女视频在线观看免费 | 国产女人被狂躁到高潮小说 | 日本精品国产 | 亚洲欧洲免费 | 蜜桃视频在线观看污 | 亚州av在线| 免费看成人 | 欧美三级韩国三级日本三斤 | 国产乱子伦农村叉叉叉 | 天天插天天射天天干 | 天天操综合网 | 久热精品在线 | 人人妻人人爽一区二区三区 | 999视频在线播放 | av无码精品一区二区三区宅噜噜 | 久久精品大片 | 成人人伦一区二区三区 | 亚洲福利一区 | 一本到av| 狂野欧美性猛交blacked | 澳门一级黄色片 | 日韩美女爱爱 | 国产肉体ⅹxxx137大胆 |