Spring Security實現禁止使用者重複登陸的配置原理
這篇文章主要介紹了Spring Security實現禁止使用者重複登陸的配置原理,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
系統使用了Spring Security做許可權管理,現在對於系統的使用者,需要改動配置,實現無法多地登陸。
一、SpringMVC專案,配置如下:
首先在修改Security相關的XML,我這裡是spring-security.xml,修改UsernamePasswordAuthenticationFilter相關Bean的構造配置
加入
<property name="sessionAuthenticationStrategy" ref="sas" />
新增sas的Bean及其相關配置
<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數量,此處為1,表示同一個使用者同時只會有一個session線上 --> <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相關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專案
略
三、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<SessionInformation> 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 <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 { //根據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,response)); return; } else { // Non-expired - update last request date/time sessionRegistry.refreshLastRequest(info.getSessionId()); } } } chain.doFilter(request,response); }
5、A處使用者只能再次登入,這時B處使用者session將會失效重登,如此迴圈
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。