尝试websocket支持登陆

pull/1682/head
648540858 2024-11-02 12:11:52 +08:00
parent 84be003a8f
commit 9789fcd3fa
3 changed files with 18 additions and 5 deletions

View File

@ -50,11 +50,20 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
chain.doFilter(request, response);
return;
}
String jwt = request.getHeader(JwtUtils.getHeader());
// 这里如果没有jwt继续往后走因为后面还有鉴权管理器等去判断是否拥有身份凭证所以是可以放行的
// 没有jwt相当于匿名访问若有一些接口是需要权限的则不能访问这些接口
System.out.println("sec-websocket-protocol==" + request.getHeader("sec-websocket-protocol"));
if (StringUtils.isBlank(jwt)) {
jwt = request.getParameter(JwtUtils.getHeader());
String secWebsocketProtocolHeader = request.getHeader("sec-websocket-protocol");
if (secWebsocketProtocolHeader != null) {
jwt = secWebsocketProtocolHeader;
}else {
jwt = request.getParameter(JwtUtils.getHeader());
}
if (StringUtils.isBlank(jwt)) {
jwt = request.getHeader(JwtUtils.getApiKeyHeader());
if (StringUtils.isBlank(jwt)) {

View File

@ -19,8 +19,6 @@ public class LogChannel {
@OnMessage(maxMessageSize = 1) // MaxMessage 1 byte
public void onMessage(String message) {
log.debug("Recv Message: {}", message);
try {
this.session.close(new CloseReason(CloseReason.CloseCodes.TOO_BIG, "此节点不接收任何客户端信息"));
} catch (IOException e) {

View File

@ -13,6 +13,8 @@
<script>
import userService from "./service/UserService";
export default {
name: 'log',
components: {},
@ -39,9 +41,13 @@ export default {
console.log(window.location.host)
let url = "ws://localhost:18080/channel/log";
if (process.env.NODE_ENV !== 'development') {
url = `ws://${window.location.host}/channel/log`
if (location.protocol === "https:") {
url = `wss://${window.location.host}/channel/log`
}else {
url = `ws://${window.location.host}/channel/log`
}
}
window.websocket = new WebSocket(url);
window.websocket = new WebSocket(url, userService.getToken());
window.websocket.onclose = e => {
console.log(`conn closed: code=${e.code}, reason=${e.reason}, wasClean=${e.wasClean}`)
}