Spring Security實(shí)現(xiàn)禁止用戶(hù)重復(fù)登陸的配置原理
這篇文章主要介紹了Spring Security實(shí)現(xiàn)禁止用戶(hù)重復(fù)登陸的配置原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
系統(tǒng)使用了Spring Security做權(quán)限管理,現(xiàn)在對(duì)于系統(tǒng)的用戶(hù),需要改動(dòng)配置,實(shí)現(xiàn)無(wú)法多地登陸。
一、SpringMVC項(xiàng)目,配置如下:
首先在修改Security相關(guān)的XML,我這里是spring-security.xml,修改UsernamePasswordAuthenticationFilter相關(guān)Bean的構(gòu)造配置
加入
<property name="sessionAuthenticationStrategy" ref="sas" />
新增sas的Bean及其相關(guān)配置
<bean id="sas" class="org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy">
<constructor-arg>
<list>
<bean class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
<constructor-arg ref="sessionRegistry"/>
<!-- 這里是配置session數(shù)量,此處為1,表示同一個(gè)用戶(hù)同時(shí)只會(huì)有一個(gè)session在線(xiàn) -->
<property name="maximumSessions" value="1" />
<property name="exceptionIfMaximumExceeded" value="false" />
</bean>
<bean class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy">
</bean>
<bean class="org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy">
<constructor-arg ref="sessionRegistry"/>
</bean>
</list>
</constructor-arg>
</bean>
<bean id="sessionRegistry"
class="org.springframework.security.core.session.SessionRegistryImpl" />
加入ConcurrentSessionFilter相關(guān)Bean配置
<bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<constructor-arg name="sessionInformationExpiredStrategy" ref="redirectSessionInformationExpiredStrategy" />
</bean>
<bean id="redirectSessionInformationExpiredStrategy"
class="org.springframework.security.web.session.SimpleRedirectSessionInformationExpiredStrategy">
<constructor-arg name="invalidSessionUrl" value="/login.html" />
</bean>
二、SpringBoot項(xiàng)目
略
三、Bean配置說(shuō)明
- SessionAuthenticationStrategy:該接口中存在onAuthentication方法用于對(duì)新登錄用戶(hù)進(jìn)行session相關(guān)的校驗(yàn)。
- 查看UsernamePasswordAuthenticationFilter及其父類(lèi)代碼,可以發(fā)現(xiàn)在doFilter中存在sessionStrategy.onAuthentication(authResult, request, response);方法
- 但UsernamePasswordAuthenticationFilter中的sessionStrategy對(duì)象默認(rèn)為NullAuthenticatedSessionStrategy,即不對(duì)session進(jìn)行相關(guān)驗(yàn)證。
- 如本文配置,建立id為sas的CompositeSessionAuthenticationStrategy的Bean對(duì)象。
- CompositeSessionAuthenticationStrategy可以理解為一個(gè)托管類(lèi),托管所有實(shí)現(xiàn)SessionAuthenticationStrategy接口的對(duì)象,用來(lái)批量托管執(zhí)行onAuthentication函數(shù)
- 這里CompositeSessionAuthenticationStrategy中注入了三個(gè)對(duì)象,關(guān)注ConcurrentSessionControlAuthenticationStrategy,它實(shí)現(xiàn)了對(duì)于session并發(fā)的控制
- UsernamePasswordAuthenticationFilter的Bean中注入新配置的sas,用于替換原本的NullAuthenticatedSessionStrategy
- ConcurrentSessionFilter的Bean用來(lái)驗(yàn)證session是否失效,并通過(guò)SimpleRedirectSessionInformationExpiredStrategy將失敗訪(fǎng)問(wèn)進(jìn)行跳轉(zhuǎn)。
四、代碼流程說(shuō)明(這里模擬用戶(hù)現(xiàn)在A處登錄,隨后用戶(hù)在B處登錄,之后A處再進(jìn)行操作時(shí)會(huì)返回失敗,提示重新登錄)
1、用戶(hù)在A處登錄,UsernamePasswordAuthenticationFilter調(diào)用sessionStrategy.onAuthentication進(jìn)行session驗(yàn)證
2、ConcurrentSessionControlAuthenticationStrategy中的onAuthentication開(kāi)始進(jìn)行session驗(yàn)證,服務(wù)器中保存了登錄后的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) {
//根據(jù)所登錄的用戶(hù)信息,查詢(xún)相對(duì)應(yīng)的現(xiàn)存session列表
final List<SessionInformation> sessions = sessionRegistry.getAllSessions(
authentication.getPrincipal(), false);
int sessionCount = sessions.size();
//獲取session并發(fā)數(shù)量,對(duì)于XML中的maximumSessions
int allowedSessions = getMaximumSessionsForThisUser(authentication);
//判斷現(xiàn)有session列表數(shù)量和并發(fā)控制數(shù)間的關(guān)系
//如果是首次登錄,根據(jù)xml配置,這里應(yīng)該是0<1,程序?qū)?huì)繼續(xù)向下執(zhí)行,
//最終執(zhí)行到SessionRegistryImpl的registerNewSession進(jìn)行新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請(qǐng)求的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) {
//循環(huán)已保存的session列表,判斷本次http請(qǐng)求session是否已經(jīng)保存
if (si.getSessionId().equals(session.getId())) {
//本次http請(qǐng)求是有效請(qǐng)求,返回執(zhí)行下一個(gè)filter
return;
}
}
}
// If the session is null, a new one will be created by the parent class,
// exceeding the allowed number
}
//本次http請(qǐng)求為新請(qǐng)求,進(jìn)入具體判斷
allowableSessionsExceeded(sessions, allowedSessions, sessionRegistry);
}
/**
* Allows subclasses to customise behaviour when too many sessions are detected.
*
* @param sessions either <code>null</code> 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 <code>SessionRegistry</code> for subclass use
*
*/
protected void allowableSessionsExceeded(List<SessionInformation> sessions,
int allowableSessions, SessionRegistry registry)
throws SessionAuthenticationException {
//根據(jù)exceptionIfMaximumExceeded判斷是否要將新http請(qǐng)求拒絕
//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;
//若不拒絕新請(qǐng)求,遍歷現(xiàn)存seesion列表
for (SessionInformation session : sessions) {
//獲取上一次/已存的session信息
if ((leastRecentlyUsed == null)
|| session.getLastRequest()
.before(leastRecentlyUsed.getLastRequest())) {
leastRecentlyUsed = session;
}
}
//將上次session信息寫(xiě)為無(wú)效(欺騙)
leastRecentlyUsed.expireNow();
}
3、用戶(hù)在B處登錄,再次通過(guò)ConcurrentSessionControlAuthenticationStrategy的檢查,將A處登錄的session置于無(wú)效狀態(tài),并在session列表中添加本次session
4、用戶(hù)在A處嘗試進(jìn)行其他操作,ConcurrentSessionFilter進(jìn)行Session相關(guān)的驗(yàn)證,發(fā)現(xiàn)A處用戶(hù)已經(jīng)失效,提示重新登錄
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//獲取本次http請(qǐng)求的session
HttpSession session = request.getSession(false);
if (session != null) {
//從本地session關(guān)系表中取出本次http訪(fǎng)問(wèn)的具體session信息
SessionInformation info = sessionRegistry.getSessionInformation(session
.getId());
//如果存在信息,則繼續(xù)執(zhí)行
if (info != null) {
//判斷session是否已經(jīng)失效(這一步在本文4.2中被執(zhí)行)
if (info.isExpired()) {
// Expired - abort processing
if (logger.isDebugEnabled()) {
logger.debug("Requested session ID "
+ request.getRequestedSessionId() + " has expired.");
}
//執(zhí)行登出操作
doLogout(request, response);
//從XML配置中的redirectSessionInformationExpiredStrategy獲取URL重定向信息,頁(yè)面跳轉(zhuǎn)到登錄頁(yè)面
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處用戶(hù)只能再次登錄,這時(shí)B處用戶(hù)session將會(huì)失效重登,如此循環(huán)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 注冊(cè)中心配置了spring?security后客戶(hù)端啟動(dòng)報(bào)錯(cuò)
- SpringBoot?整合Security權(quán)限控制的初步配置
- Spring?Security自定義登錄頁(yè)面認(rèn)證過(guò)程常用配置
- SpringBoot2.7?WebSecurityConfigurerAdapter類(lèi)過(guò)期配置
- spring security動(dòng)態(tài)配置url權(quán)限的2種實(shí)現(xiàn)方法
- SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解
- Spring?Security?OAuth?Client配置加載源碼解析
相關(guān)文章
SpringSecurity實(shí)現(xiàn)前后端分離的示例詳解
Spring Security默認(rèn)提供賬號(hào)密碼認(rèn)證方式,具體實(shí)現(xiàn)是在UsernamePasswordAuthenticationFilter 中,這篇文章主要介紹了SpringSecurity實(shí)現(xiàn)前后端分離的示例詳解,需要的朋友可以參考下2023-03-03
Java實(shí)現(xiàn)圖書(shū)借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖書(shū)借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Spring Boot jar中沒(méi)有主清單屬性的解決方法
這篇文章主要介紹了Spring Boot jar中沒(méi)有主清單屬性的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java中的形式參數(shù)和實(shí)際參數(shù)案例詳解
這篇文章主要介紹了Java中的形式參數(shù)和實(shí)際參數(shù),形參和實(shí)參間的關(guān)系,兩者是在調(diào)用的時(shí)候進(jìn)行結(jié)合的,通常實(shí)參會(huì)將取值傳遞給形參,形參去之后進(jìn)行函數(shù)過(guò)程運(yùn)算,然后可能將某些值經(jīng)過(guò)參數(shù)或函數(shù)符號(hào)返回給調(diào)用者,需要的朋友可以參考下2023-10-10
Springboot使用pdfbox提取PDF圖片的代碼示例
PDFBox是一個(gè)用于創(chuàng)建和處理PDF文檔的Java庫(kù),它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容,本文就給大家介紹Springboot如何使用pdfbox提取PDF圖片,感興趣的同學(xué)可以借鑒參考2023-06-06
使用Java動(dòng)態(tài)創(chuàng)建Flowable會(huì)簽?zāi)P偷氖纠a
動(dòng)態(tài)創(chuàng)建流程模型,尤其是會(huì)簽(Parallel Gateway)模型,是提升系統(tǒng)靈活性和響應(yīng)速度的關(guān)鍵技術(shù)之一,本文將通過(guò)Java編程語(yǔ)言,深入探討如何在運(yùn)行時(shí)動(dòng)態(tài)地創(chuàng)建包含會(huì)簽環(huán)節(jié)的Flowable流程模型,需要的朋友可以參考下2024-05-05
SpringBoot啟動(dòng)異常Exception in thread “main“ 
本文主要介紹了SpringBoot啟動(dòng)異常Exception in thread “main“ java.lang.UnsupportedClassVersionError,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)企業(yè)員工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
springboot中手動(dòng)提交事務(wù)的實(shí)現(xiàn)方法
手動(dòng)提交事務(wù)可以提供更靈活的控制,以便在分布式環(huán)境中處理事務(wù)的提交和回滾,本文就來(lái)介紹一下springboot中手動(dòng)提交事務(wù)的實(shí)現(xiàn)方法,感興趣的可以了解一下2024-01-01

