增加webSocket鉴权样例工程:websocket-demo

This commit is contained in:
zhult13
2022-06-05 21:25:33 +08:00
parent 82f6c952ec
commit 4d56f487b1
8 changed files with 193 additions and 1 deletions
@@ -74,7 +74,7 @@ public class AuthUtils {
* 校验accessToken
*/
public static void checkAccessToken(HttpServletRequest request) {
String accessToken = extractHeaderToken(request);
String accessToken = extractToken(request);
checkAccessToken(accessToken);
}
+39
View File
@@ -0,0 +1,39 @@
## 代码说明
- [SpringBoot的WebSocket接口如何实现鉴权?](https://www.kancloud.cn/zlt2000/microservices-platform/2278851)
 
## 启动以下服务
1. zlt-uaa:统一认证中心
2. user-center:用户服务
3. sc-gateway:api网关
4. websocket-demo
> 环境配置与启动参考文档:https://www.kancloud.cn/zlt2000/microservices-platform/919418
 
## 获取access_token
可使用任意授权模式获取;
例如:密码模式授权
- 请求方式:POST
- 请求头:Authorization:Basic d2ViQXBwOndlYkFwcA==
- 请求地址:http://localhost:9900/api-uaa/oauth/token?grant_type=password&username=admin&password=admin
> 授权接口清单参考文档:https://www.kancloud.cn/zlt2000/microservices-platform/1158135
 
## 测试步骤
使用 `Postman` 进行测试(最新版本支持 webSocket 接口)
点击 `New` 按钮,选择 `WebSocket Request` ;
在 `URL` 中输入 `ws://localhost:8092/websocket/test`
在 `Headers` 中添加:`Authorization:Bearer xxx`
或者
在 `参数` 中添加:`ws://localhost:8092/websocket/test?access_token=xxx`
> xxx 需替换为正确的 access_token
+51
View File
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-demo</artifactId>
<version>5.3.0</version>
</parent>
<artifactId>websocket-demo</artifactId>
<dependencies>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-config</artifactId>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-redis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,18 @@
package org.zlt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zlt
* @date 2022/5/8
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@SpringBootApplication
public class WebSocketApp {
public static void main(String[] args) {
SpringApplication.run(WebSocketApp.class, args);
}
}
@@ -0,0 +1,20 @@
package org.zlt.config;
import com.central.oauth2.common.config.DefaultResourceServerConf;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
/**
* security资源服务器配置
*
* @author zlt
* @version 1.0
* @date 2022/5/9
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Configuration
@EnableResourceServer
public class MyResourceConfig extends DefaultResourceServerConf {
}
@@ -0,0 +1,23 @@
package org.zlt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* WebSocket配置
*
* @author zlt
* @version 1.0
* @date 2022/5/8
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
@@ -0,0 +1,27 @@
package org.zlt.controller;
import com.central.oauth2.common.config.WcAuthConfigurator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
/**
* @author zlt
* @date 2022/5/8
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
@Component
@ServerEndpoint(value = "/websocket/test", configurator = WcAuthConfigurator.class)
public class TestWebSocketController {
@OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText("TestWebSocketController-ok");
}
}
@@ -0,0 +1,14 @@
server:
port: 8092
spring:
application:
name: zlt-websocket
main:
allow-bean-definition-overriding: true
zlt:
security:
ignore:
httpUrls: >
/websocket/**