ソースを参照

优化代码逻辑

周浩 9 年 前
コミット
e254cea936

+ 3 - 1
hsweb-web-core/src/main/java/org/hsweb/web/core/authorize/AopAuthorizeValidator.java

@@ -65,9 +65,11 @@ public class AopAuthorizeValidator extends SimpleAuthorizeValidator {
         Map<String, Object> param = new LinkedHashMap<>();
         MethodSignature signature = (MethodSignature) pjp.getSignature();
         String[] names = signature.getParameterNames();
+        Object[] args=pjp.getArgs();
         for (int i = 0; i < names.length; i++) {
-            param.put(names[i], pjp.getArgs()[i]);
+            param.put(names[i], args[i]);
         }
+        param.put("paramsMap",param);
         return validate(user, param, config);
     }
 

+ 1 - 1
hsweb-web-core/src/main/java/org/hsweb/web/core/authorize/annotation/AopJsonpSupport.java

@@ -1,4 +1,4 @@
-package org.hsweb.web.core.authorize.annotation;
+package org.hsweb.web.core.jsonp;
 
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;

+ 43 - 0
hsweb-web-core/src/main/java/org/hsweb/web/core/session/AbstractHttpSessionManager.java

@@ -0,0 +1,43 @@
+package org.hsweb.web.core.session;
+
+import org.hsweb.web.bean.po.user.User;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.servlet.http.HttpSession;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by zhouhao on 16-6-2.
+ */
+public abstract class AbstractHttpSessionManager implements HttpSessionManager {
+
+    private List<HttpSessionManagerListener> listeners = new ArrayList<>();
+
+    protected void onUserLogin(User user,HttpSession session) {
+        if (listeners != null) {
+            listeners.forEach(listener -> listener.onUserLogin(user,session));
+        }
+    }
+
+    protected void onUserLoginOut(String  userId,HttpSession session) {
+        if (listeners != null) {
+            listeners.forEach(listener -> listener.onUserLoginOut(userId,session));
+        }
+    }
+
+    @Autowired(required = false)
+    public void setListeners(List<HttpSessionManagerListener> listeners) {
+        this.listeners = listeners;
+    }
+
+    public List<HttpSessionManagerListener> getListeners() {
+        return listeners;
+    }
+
+    @Override
+    public void addListener(HttpSessionManagerListener listener) {
+        listeners.add(listener);
+    }
+
+}

+ 2 - 0
hsweb-web-core/src/main/java/org/hsweb/web/core/session/HttpSessionManager.java

@@ -73,4 +73,6 @@ public interface HttpSessionManager {
      * @param userId 用户ID
      */
     boolean isLogin(String userId);
+
+    void addListener(HttpSessionManagerListener listener);
 }

+ 14 - 0
hsweb-web-core/src/main/java/org/hsweb/web/core/session/HttpSessionManagerListener.java

@@ -0,0 +1,14 @@
+package org.hsweb.web.core.session;
+
+import org.hsweb.web.bean.po.user.User;
+
+import javax.servlet.http.HttpSession;
+
+/**
+ * Created by zhouhao on 16-6-2.
+ */
+public interface HttpSessionManagerListener {
+    void onUserLogin(User user,HttpSession session);
+
+    void onUserLoginOut(String userId,HttpSession session);
+}

+ 108 - 2
hsweb-web-core/src/main/java/org/hsweb/web/core/session/redis/RedisHttpSessionManager.java

@@ -1,20 +1,24 @@
 package org.hsweb.web.core.session.redis;
 
 import org.hsweb.web.bean.po.user.User;
-import org.hsweb.web.core.session.HttpSessionManager;
+import org.hsweb.web.core.session.AbstractHttpSessionManager;
 import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.session.ExpiringSession;
 import org.springframework.session.data.redis.RedisOperationsSessionRepository;
 
+import javax.servlet.ServletContext;
 import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
  * Created by zhouhao on 16-5-27.
  */
-public class RedisHttpSessionManager implements HttpSessionManager {
+public class RedisHttpSessionManager extends AbstractHttpSessionManager {
 
     private RedisTemplate sessionRedisTemplate;
 
@@ -57,6 +61,9 @@ public class RedisHttpSessionManager implements HttpSessionManager {
         sessionRedisTemplate.execute((RedisCallback) connection -> {
             String key = "http.session.user:" + userId;
             String sessionId = getSessionIdByUserId(userId);
+            ExpiringSession redisSession = redisOperationsSessionRepository.getSession(sessionId);
+            HttpSession session = new HttpSessionWrapper(redisSession);
+            onUserLoginOut(userId, session);
             removeSession(sessionId);
             return connection.del(key.getBytes());
         });
@@ -76,6 +83,7 @@ public class RedisHttpSessionManager implements HttpSessionManager {
             String key = "http.session.user:" + user.getId();
             String value = session.getId();
             connection.set(key.getBytes(), value.getBytes());
+            onUserLogin(user, session);
             return null;
         });
     }
@@ -127,4 +135,102 @@ public class RedisHttpSessionManager implements HttpSessionManager {
     public void setSessionRedisTemplate(RedisTemplate sessionRedisTemplate) {
         this.sessionRedisTemplate = sessionRedisTemplate;
     }
+
+    private final class HttpSessionWrapper implements HttpSession {
+        private ExpiringSession session;
+        private boolean invalidated;
+        private boolean old;
+
+        public HttpSessionWrapper(ExpiringSession session) {
+            this.session = session;
+        }
+
+        public long getCreationTime() {
+            return session.getCreationTime();
+        }
+
+        public String getId() {
+            return session.getId();
+        }
+
+        public long getLastAccessedTime() {
+            checkState();
+            return session.getLastAccessedTime();
+        }
+
+        public ServletContext getServletContext() {
+            return null;
+        }
+
+        public void setMaxInactiveInterval(int interval) {
+            session.setMaxInactiveIntervalInSeconds(interval);
+        }
+
+        public int getMaxInactiveInterval() {
+            return session.getMaxInactiveIntervalInSeconds();
+        }
+
+        @SuppressWarnings("deprecation")
+        public HttpSessionContext getSessionContext() {
+            return null;
+        }
+
+        public Object getAttribute(String name) {
+            checkState();
+            return session.getAttribute(name);
+        }
+
+        public Object getValue(String name) {
+            return getAttribute(name);
+        }
+
+        public Enumeration<String> getAttributeNames() {
+            checkState();
+            return Collections.enumeration(session.getAttributeNames());
+        }
+
+        public String[] getValueNames() {
+            checkState();
+            Set<String> attrs = session.getAttributeNames();
+            return attrs.toArray(new String[0]);
+        }
+
+        public void setAttribute(String name, Object value) {
+            checkState();
+            session.setAttribute(name, value);
+        }
+
+        public void putValue(String name, Object value) {
+            setAttribute(name, value);
+        }
+
+        public void removeAttribute(String name) {
+            checkState();
+            session.removeAttribute(name);
+        }
+
+        public void removeValue(String name) {
+            removeAttribute(name);
+        }
+
+        public void invalidate() {
+            checkState();
+            invalidated = true;
+        }
+
+        public void setNew(boolean isNew) {
+            this.old = !isNew;
+        }
+
+        public boolean isNew() {
+            checkState();
+            return !old;
+        }
+
+        private void checkState() {
+            if (invalidated) {
+                throw new IllegalStateException("The HttpSession has already be invalidated.");
+            }
+        }
+    }
 }

+ 9 - 1
hsweb-web-core/src/main/java/org/hsweb/web/core/session/redis/RedisHttpSessionManagerConfiguration.java

@@ -1,6 +1,8 @@
 package org.hsweb.web.core.session.redis;
 
 import org.hsweb.web.core.session.HttpSessionManager;
+import org.hsweb.web.core.session.HttpSessionManagerListener;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -10,6 +12,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.session.data.redis.RedisOperationsSessionRepository;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 /**
  * Created by zhouhao on 16-5-27.
@@ -18,13 +21,18 @@ import javax.annotation.Resource;
 @ConditionalOnBean(value = RedisOperationsSessionRepository.class, name = "sessionRedisTemplate")
 @Order(Ordered.HIGHEST_PRECEDENCE)
 public class RedisHttpSessionManagerConfiguration {
+    @Autowired(required = false)
+    private List<HttpSessionManagerListener> httpSessionManagerListeners;
 
     @Resource(name = "sessionRedisTemplate")
-    RedisTemplate sessionRedisTemplate;
+    private RedisTemplate sessionRedisTemplate;
 
     @Bean
     public HttpSessionManager sessionListener(RedisOperationsSessionRepository repository) {
         RedisHttpSessionManager redisHttpSessionManager = new RedisHttpSessionManager();
+        if (httpSessionManagerListeners != null) {
+            redisHttpSessionManager.setListeners(httpSessionManagerListeners);
+        }
         redisHttpSessionManager.setSessionRedisTemplate(sessionRedisTemplate);
         redisHttpSessionManager.setRedisOperationsSessionRepository(repository);
         return redisHttpSessionManager;

+ 5 - 1
hsweb-web-core/src/main/java/org/hsweb/web/core/session/siample/SimpleHttpSessionManager.java

@@ -1,6 +1,7 @@
 package org.hsweb.web.core.session.siample;
 
 import org.hsweb.web.bean.po.user.User;
+import org.hsweb.web.core.session.AbstractHttpSessionManager;
 import org.hsweb.web.core.session.HttpSessionManager;
 import org.hsweb.web.core.utils.WebUtil;
 
@@ -14,7 +15,7 @@ import java.util.stream.Collectors;
 /**
  * Created by zhouhao on 16-5-27.
  */
-public class SimpleHttpSessionManager implements HttpSessionManager {
+public class SimpleHttpSessionManager extends AbstractHttpSessionManager {
 
     /**
      * httpSession存储器,sessionId:HttpSession
@@ -60,6 +61,7 @@ public class SimpleHttpSessionManager implements HttpSessionManager {
             session.removeAttribute("user");
             sessionStorage.remove(session.getId());
             userSessionStorage.remove(userId);
+            onUserLoginOut(userId,session);
         }
     }
 
@@ -69,6 +71,7 @@ public class SimpleHttpSessionManager implements HttpSessionManager {
         if (session != null) {
             User user = WebUtil.getLoginUser(session);
             if (user != null) {
+                onUserLoginOut(user.getId(),session);
                 userSessionStorage.remove(user.getId());
             }
             sessionStorage.remove(sessionId);
@@ -81,6 +84,7 @@ public class SimpleHttpSessionManager implements HttpSessionManager {
         sessionStorage.put(session.getId(), session);
         userSessionStorage.put(user.getId(), session);
         session.setAttribute("user", user);
+        onUserLogin(user,session);
     }
 
     @Override

+ 1 - 0
hsweb-web-core/src/main/java/org/hsweb/web/core/session/siample/SimpleHttpSessionManagerAutoConfiguration.java

@@ -10,6 +10,7 @@ import org.springframework.context.annotation.Configuration;
 @ConditionalOnMissingBean(HttpSessionManager.class)
 @ConditionalOnWebApplication
 public class SimpleHttpSessionManagerAutoConfiguration {
+
     @Bean
     public HttpSessionManager simpleHttpSessionManager() {
         SimpleHttpSessionManager httpSessionManager = new SimpleHttpSessionManager();

+ 0 - 2
hsweb-web-core/src/main/java/org/hsweb/web/core/utils/ThreadLocalUtils.java

@@ -1,7 +1,5 @@
 package org.hsweb.web.core.utils;
 
-import org.springframework.util.ResourceUtils;
-
 import java.util.HashMap;
 import java.util.Map;