mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-10 01:43:45 +08:00
优化一系列逻辑
This commit is contained in:
@@ -6,7 +6,9 @@ import org.hsweb.web.core.authorize.annotation.Authorize;
|
||||
import org.hsweb.web.core.authorize.validator.SimpleAuthorizeValidator;
|
||||
import org.hsweb.web.bean.po.user.User;
|
||||
import org.hsweb.web.core.exception.AuthorizeException;
|
||||
import org.hsweb.web.core.session.HttpSessionManager;
|
||||
import org.hsweb.web.core.utils.WebUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.webbuilder.utils.common.ClassUtils;
|
||||
import org.webbuilder.utils.common.StringUtils;
|
||||
|
||||
@@ -47,12 +49,19 @@ public class AopAuthorizeValidator extends SimpleAuthorizeValidator {
|
||||
return config;
|
||||
}
|
||||
|
||||
private HttpSessionManager httpSessionManager;
|
||||
|
||||
@Autowired
|
||||
public void setHttpSessionManager(HttpSessionManager httpSessionManager) {
|
||||
this.httpSessionManager = httpSessionManager;
|
||||
}
|
||||
|
||||
public boolean validate(ProceedingJoinPoint pjp) {
|
||||
AuthorizeValidatorConfig config = getConfig(pjp);
|
||||
if (config == null) return true;
|
||||
User user = WebUtil.getLoginUser();
|
||||
User user = httpSessionManager.getUserBySessionId(WebUtil.getHttpServletRequest().getSession().getId());
|
||||
if (user == null) throw new AuthorizeException("未登录", 401);
|
||||
if(config.isEmpty())return true;
|
||||
if (config.isEmpty()) return true;
|
||||
Map<String, Object> param = new LinkedHashMap<>();
|
||||
MethodSignature signature = (MethodSignature) pjp.getSignature();
|
||||
String[] names = signature.getParameterNames();
|
||||
|
||||
@@ -15,6 +15,13 @@ public interface HttpSessionManager {
|
||||
*/
|
||||
String getSessionIdByUserId(String userId) ;
|
||||
|
||||
/**
|
||||
* 根据sessionId 获取用户信息
|
||||
* @param sessionId 根据sessionId
|
||||
* @return 用户信息
|
||||
*/
|
||||
User getUserBySessionId(String sessionId) ;
|
||||
|
||||
/**
|
||||
* 根据用户ID从session中删除一个用户(下线)
|
||||
*
|
||||
|
||||
@@ -32,6 +32,16 @@ public class RedisHttpSessionManager implements HttpSessionManager {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUserBySessionId(String sessionId) {
|
||||
if (sessionId == null) return null;
|
||||
ExpiringSession redisSession = redisOperationsSessionRepository.getSession(sessionId);
|
||||
if (redisSession != null) {
|
||||
return (User) redisSession.getAttribute("user");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSessionIdByUserId(String userId) {
|
||||
return (String) sessionRedisTemplate.execute((RedisCallback<String>) connection -> {
|
||||
|
||||
@@ -32,6 +32,13 @@ public class SimpleHttpSessionManager implements HttpSessionManager {
|
||||
.filter(user -> user != null).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUserBySessionId(String sessionId) {
|
||||
if (sessionId == null) return null;
|
||||
HttpSession session = sessionStorage.get(sessionId);
|
||||
return session == null ? null : ((User) session.getAttribute("user"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSessionIdByUserId(String userId) {
|
||||
HttpSession session = userSessionStorage.get(userId);
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
<dependency>
|
||||
<groupId>org.hsweb</groupId>
|
||||
<artifactId>hsweb-web-bean</artifactId>
|
||||
<version>${parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -25,5 +24,16 @@
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hsweb</groupId>
|
||||
<artifactId>hsweb-web-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.fusesource</groupId>
|
||||
<artifactId>sigar</artifactId>
|
||||
<version>1.6.4</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.hsweb.web.socket.CMDWebSocketAutoConfiguration
|
||||
@@ -1,4 +0,0 @@
|
||||
#LOGGING
|
||||
logging:
|
||||
config: classpath:logback.xml
|
||||
path: ~/hsweb/logger/
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||
<!-- 控制台输出日志 -->
|
||||
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} > %-5level %logger{35} - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="ERROR">
|
||||
<appender-ref ref="Console"/>
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -2,6 +2,9 @@ package org.hsweb.web.socket;
|
||||
|
||||
import org.hsweb.web.socket.cmd.CMD;
|
||||
import org.hsweb.web.socket.cmd.CmdProcessor;
|
||||
import org.hsweb.web.socket.message.SimpleWebSocketMessageManager;
|
||||
import org.hsweb.web.socket.message.WebSocketMessage;
|
||||
import org.hsweb.web.socket.message.WebSocketMessageManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -9,6 +12,7 @@ import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 测试命令处理器
|
||||
@@ -18,6 +22,8 @@ import javax.annotation.PostConstruct;
|
||||
public class TestProcessor implements CmdProcessor {
|
||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private WebSocketMessageManager webSocketMessageManager = new SimpleWebSocketMessageManager();
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "test";
|
||||
@@ -27,7 +33,22 @@ public class TestProcessor implements CmdProcessor {
|
||||
public void exec(CMD cmd) throws Exception {
|
||||
logger.info("execute cmd :" + cmd);
|
||||
//收到命令后,向客户端推送一条消息
|
||||
cmd.getSession().sendMessage(new TextMessage("你好!"));
|
||||
if ("subscribe".equals(cmd.getParams().get("type"))) {
|
||||
webSocketMessageManager.subscribe("test", "admin",cmd.getSession());
|
||||
}
|
||||
WebSocketMessage message = new WebSocketMessage();
|
||||
message.setType("test");
|
||||
message.setTo("admin");
|
||||
message.setContent("test");
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
webSocketMessageManager.publish(message);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -39,11 +60,12 @@ public class TestProcessor implements CmdProcessor {
|
||||
@Override
|
||||
public void onSessionConnect(WebSocketSession session) throws Exception {
|
||||
logger.info("小伙伴进来了");
|
||||
session.sendMessage(new TextMessage("命令:" + getName() + " , 作用:测试"));
|
||||
// webSocketMessageManager.onSessionConnect(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSessionClose(WebSocketSession session) throws Exception {
|
||||
logger.info("小伙伴离开了");
|
||||
// webSocketMessageManager.onSessionClose(session);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@ public class WebSocketClientTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
WebSocketClient client = new StandardWebSocketClient();
|
||||
String url = "ws://localhost:8088/socket";
|
||||
String url = "ws://localhost:8080/socket";
|
||||
client.doHandshake(new AbstractWebSocketHandler() {
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
//链接成功后发送消息
|
||||
session.sendMessage(new TextMessage("{\"cmd\":\"test\"}"));
|
||||
session.sendMessage(new TextMessage("{\"cmd\":\"system-monitor\",\"params\":{\"type\":\"cpu\"}}"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,34 +1,23 @@
|
||||
package org.hsweb.web.socket;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.hsweb.web.socket.cmd.support.SystemMonitorProcessor;
|
||||
import org.hsweb.web.socket.message.SimpleWebSocketMessageManager;
|
||||
import org.hsweb.web.socket.message.WebSocketMessageManager;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
|
||||
/**
|
||||
* spring-boot websokcet 测试
|
||||
* Created by 浩 on 2016-01-19 0019.
|
||||
*/
|
||||
@ComponentScan(basePackages = "org.hsweb.web.socket")
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
public class WebSocketTest extends WebSocketConfigurationSupport {
|
||||
|
||||
/**
|
||||
* 基于命令的websocket服务
|
||||
*/
|
||||
@Autowired
|
||||
private CmdWebSocketHandler cmdWebSocketHandler;
|
||||
|
||||
/**
|
||||
* 注册WebSocket处理器
|
||||
*/
|
||||
@Override
|
||||
protected void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
//绑定到 /socket
|
||||
registry.addHandler(cmdWebSocketHandler, "/socket");
|
||||
}
|
||||
public class WebSocketTest {
|
||||
|
||||
/**
|
||||
* 测试步骤
|
||||
@@ -38,7 +27,7 @@ public class WebSocketTest extends WebSocketConfigurationSupport {
|
||||
* 3.执行:ws.onmessage=function(message){console.log(message.data)}
|
||||
* 4.执行:ws.send('{"cmd":"test"}'); 按回车
|
||||
* 5.如果看到后台日志显示:handleMessage,id:0 msg={"cmd":"test"},前台有接收到推送消息,则代表成功了
|
||||
* <p/>
|
||||
* <p>
|
||||
* 也可以运行:{@link WebSocketClientTest#main} 测试
|
||||
*
|
||||
* @throws Exception
|
||||
|
||||
6
pom.xml
6
pom.xml
@@ -176,7 +176,11 @@
|
||||
<artifactId>hsweb-web-concurrent-lock</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hsweb</groupId>
|
||||
<artifactId>hsweb-web-websocket</artifactId>
|
||||
<version>${project.version}T</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc14</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user