使用するシーン:
管理者がユーザー権限情報を変更した後、最新の権限を取得するには、ユーザーに強制的にログオフして再度ログインする必要があります。
他のユーザー情報を記録する必要がないため、静的オブジェクトを使用してユーザー関連のセッションを管理しました。
処理ロジック
1.ユーザーがログインしたときに、ユーザーセッション情報を静的オブジェクトに記録します。
@RequestMapping (value = "/ success" )
@ ResponseBody
public Result < List < String >> success (HttpServletRequest request、HttpServletResponse response){
SessionUtil 。putSession(リクエスト); //管理用户セッションコレクション< SimpleGrantedAuthority >権限=(コレクション< SimpleGrantedAuthority >) SecurityContextHolder 。getContext()。getAuthentication()。getAuthorities(); LinkedList
<文字列>ロール=新しいLinkedList <>();
for (SimpleGrantedAuthority auth :authorities )roles .add(auth .getAuthority());
最終的なCookie [] Cookies = request.getCookies();
if (null != Cookies ){
for (Cookie c :cookies ){
if ("JSESSIONID" .equalsIgnoreCase(c .getName())){
c .setValue(c.getValue()+ "; SameSite = None; Secure" );
}
}
}
新しい返す検索結果を<リスト<文字列>>(結果。SUCCESS、"登录成功" 、役割)。
}
ログインユーザーセッション管理クラスの実現[1つのアカウントに複数回のログインを許可する]
インポートjavax.servlet.http.HttpServletRequest ;
インポートjavax.servlet.http.HttpSession ; java.utilを
インポートします。*;
public class SessionUtil {
private static Map < String 、List < HttpSession >> sessionMap = new HashMap <>();
/ **
* Lisession
* @param request * / public static void putSession (HttpServletRequest request){ String username = request.getParameter("username"
);
//获取セッションHttpSessionセッション= request.getSession(); リスト< HttpSession > sessionList = sessionMap .get(username ); if (sessionList == null ){ sessionList = new ArrayList <>(); sessionList.add(セッション); } else { if (!sessionList.contains(session )){ sessionList.add(session ); } } sessionMap .put(ユーザー名
、SessionList);
CacheUtil 。putStatus(ユーザ名、falseに); //はユーザバッファの状態を初期化} / ** *セッションの破壊が地図から削除されます * @paramのユーザー名* /パブリック静的な無効moveSession (文字列ユーザ名){ sessionMap。削除(ユーザー名); } / ** *クリアセッション * @paramユーザー名* /パブリック静的な無効destroyedSession (文字列のユーザー名){ リスト<のHttpSession > sessionList =
sessionMap .get(username);
if(sessionList!=null){
for(HttpSession session:sessionList){
session.invalidate();
}
moveSession(ユーザー名);
}
}
/ **
*清除所有登录セッション
* /パブリック静的ボイドdestroyedAllSession(){ ため(文字列名: sessionMap .keySet()){ リスト<のHttpSession>sessionList= sessionMap
.get(ユーザー名);
for (HttpSession session :sessionList ){
session .invalidate();
}
moveSession(ユーザー名);
}
}
}
2.ユーザ情報を変更した後、ユーザのオフライン操作実行SessionUtil 。destroyedSessionを(ユーザ.getUsername())。
@PostMapping (value = "/ update" )
@ ResponseBody
public Result update (@RequestBody @Validated UserDTO userDTO){
User user = userService .findUser(userDTO.getId());
リスト<ロール> userRoleList = new ArrayList <>();
if (userDTO.getRole()。isArray()){
for (JsonNode roleId :userDTO.getRole()){
Role userRole = roleService .getRole(roleId .asInt());
userRoleList .add(userRole );
}
}
user .setRoles(userRoleList );
user .setUsername(userDTO.getUsername());
user .setPassword(userDTO.getPassword());
user .setEmailAddress(userDTO.getEmailaddress());
user .setTrueName(userDTO.getTruename());
user .setProject(userDTO.getProject());
user .setGroupName(userDTO.getGroupname());
ユーザー.setUpdateTime(new Date());
userService .editUser(user );
SessionUtil 。destroyedSession(user .getUsername());
結果を返します。わかりました();
}