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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 登录踢出_spring security 4 如何踢出用户?

發布時間:2025/3/21 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 登录踢出_spring security 4 如何踢出用户? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目用的是spring boot 1.3 全javaconfig 配置

目的 :

我要踢出一個登錄在線的用戶,踢出后,剔出的用戶再次訪問服務器的時候會去到登錄界面,

security 配置 :

http

.authorizeRequests()

.antMatchers("/app/views/assets/**", "/app/apk/**", "/assets/**", "/view/**")

.permitAll().anyRequest().authenticated()

.and()

.formLogin().loginPage("/login").loginProcessingUrl("/loginLoc")

.successHandler(customSuccessHandler).failureHandler(customErrorHandler)

.permitAll()

.and().sessionManagement().invalidSessionUrl("/login").maximumSessions(-1).maxSessionsPreventsLogin(true).sessionRegistry(sessionRegistry())

.and()

.and()

.logout().logoutSuccessUrl("/login").permitAll()

.and().csrf().disable();

踢出的代碼:

List allPrincipals = sessionRegistry.getAllPrincipals();

for (int j = 0; j < allPrincipals.size(); j++) {

CustomUser customUser = (CustomUser) allPrincipals.get(j);

if (customUser.getUsername().equals(username)) {

List allSessions = sessionRegistry.getAllSessions(customUser, false);

if (allSessions != null) {

for (int i = 0; i < allSessions.size(); i++) {

SessionInformation sessionInformation = allSessions.get(i);

sessionInformation.expireNow();

sessionRegistry.removeSessionInformation(sessionInformation.getSessionId());

}

}

}

}

執行倒是成功了,踢出后sessionRegistry.getAllPrincipals()返回確實是少了一個(已經沒有踢出的那個用戶了)。

但是剔出的用戶還是可以正常的訪問服務器,按理來說,踢出后再次訪問應該會找不到session然后跳轉到登錄去嗎? 莫非還要我自己在寫一個過濾器,每次都判斷這個用戶在不在sessionRegistry.getAllPrincipals()里面嗎?

怎么才能正常工作 ?

總結

以上是生活随笔為你收集整理的java 登录踢出_spring security 4 如何踢出用户?的全部內容,希望文章能夠幫你解決所遇到的問題。

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