从零开始java安全权限框架篇(七):spring security整合Redis实现session共享

时间:2025-04-02 13:31:52
  • package .repository;
  • import ;
  • import ;
  • import .Date;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .Set;
  • import ;
  • import ;
  • import ;
  • import .factory.;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import .constant.redisConstant;
  • import ;
  • import ;
  • import ;
  • @Component
  • public class MySessionRegistryImpl implements SessionRegistry,
  • ApplicationListener<SessionDestroyedEvent>{
  • protected final Log logger = (MySessionRegistryImpl.class);
  • //====================================================
  • @Autowired
  • private redisService redisService;
  • private String hash_key=redisConstant.security_session_principals_key;
  • private long timeout=redisConstant.security_session_timeout;
  • //=====================================================
  • public void sessionIdsPut(String sessionId,SessionInformation t) {
  • String str=(t);
  • redisService.set(sessionId, str,timeout);
  • }
  • public void sessionIdsRemove(String sessionId) {
  • (sessionId);
  • }
  • public Set<String> principalsGet(Object principal){
  • String str=(principal);
  • return (Set<String>) (hash_key, str);
  • }
  • public Set<String> principalsPutIfAbsent(Object principal,Set<String> strs){
  • Set<String> res=new HashSet<String>();
  • if(principalsGet(principal) != null ) {
  • res= principalsGet(principal);
  • }
  • String str=(principal);
  • (hash_key, str, strs,timeout);
  • return res;
  • }
  • public void principalsRemove(Object principal){
  • String str=(principal);
  • (str);
  • }
  • //===================================================
  • @Override
  • public SessionInformation getSessionInformation(String sessionId) {
  • String str = redisService.get(sessionId);
  • try {
  • if(str!= null && ().length() != 0) {
  • SessionInformation sf = jsonToEntity(str);
  • return sf;
  • }
  • } catch (Exception e) {
  • // TODO Auto-generated catch block
  • ();
  • }
  • return null;
  • }
  • public List<Object> principalsKeySet(){
  • Map<Object, Object> map=(hash_key);
  • if(map== null) {
  • map=new HashMap<Object, Object>();
  • }
  • return new ArrayList<Object>(());
  • }
  • public SessionInformation jsonToEntity(String jsonStr){
  • // SessionInformation sf=new SessionInformation(principal, sessionId, lastRequest)
  • JSONObject ssEntity = (jsonStr);
  • Date lastRequest=(("lastRequest"));
  • Object principal=ssEntity.get("principal");
  • String sessionId=("sessionId");
  • SessionInformation sf=new SessionInformation(principal, sessionId, lastRequest);
  • return sf;
  • }
  • //==================================================
  • @Override
  • public List<Object> getAllPrincipals() {
  • ("==============>获取到当前所有的用户");
  • return new ArrayList<>(principalsKeySet());
  • }
  • @Override
  • public List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions) {
  • ("====================>更具当前用户获取到所有的session");
  • final Set<String> sessionsUsedByPrincipal = principalsGet(principal);
  • if (sessionsUsedByPrincipal == null) {
  • return ();
  • }
  • List<SessionInformation> list = new ArrayList<>(
  • sessionsUsedByPrincipal.size());
  • for (String sessionId : sessionsUsedByPrincipal) {
  • SessionInformation sessionInformation = getSessionInformation(sessionId);
  • if (sessionInformation == null) {
  • continue;
  • }
  • if (includeExpiredSessions || !()) {
  • list.add(sessionInformation);
  • }
  • }
  • return list;
  • }
  • public void onApplicationEvent(SessionDestroyedEvent event) {
  • String sessionId = ();
  • removeSessionInformation(sessionId);
  • }
  • @Override
  • public void refreshLastRequest(String sessionId) {
  • ("====================>刷新请求");
  • (sessionId, "SessionId required as per interface contract");
  • SessionInformation info = getSessionInformation(sessionId);
  • if (info != null) {
  • ();
  • }
  • }
  • @Override
  • public void registerNewSession(String sessionId, Object principal) {
  • ("====================>session注册");
  • (sessionId, "SessionId required as per interface contract");
  • (principal, "Principal required as per interface contract");
  • if (()) {
  • ("Registering session " + sessionId + ", for principal "
  • + principal);
  • }
  • if (getSessionInformation(sessionId) != null) {
  • removeSessionInformation(sessionId);
  • }
  • sessionIdsPut(sessionId,
  • new SessionInformation(principal, sessionId, new Date()));
  • Set<String> sessionsUsedByPrincipal = principalsGet(principal);
  • if (sessionsUsedByPrincipal == null) {
  • sessionsUsedByPrincipal = new CopyOnWriteArraySet<>();
  • Set<String> prevSessionsUsedByPrincipal = principalsPutIfAbsent(principal,
  • sessionsUsedByPrincipal);
  • if (prevSessionsUsedByPrincipal != null) {
  • sessionsUsedByPrincipal = prevSessionsUsedByPrincipal;
  • }
  • }
  • sessionsUsedByPrincipal.add(sessionId);
  • if (()) {
  • ("Sessions used by '" + principal + "' : "
  • + sessionsUsedByPrincipal);
  • }
  • }
  • @Override
  • public void removeSessionInformation(String sessionId) {
  • ("====================>提出童虎"+sessionId);
  • (sessionId, "SessionId required as per interface contract");
  • SessionInformation info = getSessionInformation(sessionId);
  • if (info == null) {
  • return;
  • }
  • if (()) {
  • ("Removing session " + sessionId
  • + " from set of registered sessions");
  • }
  • sessionIdsRemove(sessionId);
  • Set<String> sessionsUsedByPrincipal = principalsGet(());
  • if (sessionsUsedByPrincipal == null) {
  • return;
  • }
  • if (()) {
  • ("Removing session " + sessionId
  • + " from principal's set of registered sessions");
  • }
  • (sessionId);
  • if (()) {
  • // No need to keep object in principals Map anymore
  • if (()) {
  • ("Removing principal " + ()
  • + " from registry");
  • }
  • principalsRemove(());
  • }
  • if (()) {
  • ("Sessions used by '" + () + "' : "
  • + sessionsUsedByPrincipal);
  • }
  • }
  • }