聊天室信息发送

This commit is contained in:
chanbook 2022-08-08 10:28:33 +08:00
parent 666847f482
commit 10773f4f5a
3 changed files with 40 additions and 31 deletions

View File

@ -1,10 +1,13 @@
package com.zhangshu.chat.demo.config; package com.zhangshu.chat.demo.config;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.zhangshu.chat.demo.dto.CommonResult; import com.zhangshu.chat.demo.dto.CommonResult;
import com.zhangshu.chat.demo.util.ResponseWriteUtil; import com.zhangshu.chat.demo.util.ResponseWriteUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean; import org.springframework.web.filter.GenericFilterBean;
@ -36,10 +39,16 @@ public class JwtTokenAuthenticationFilter extends GenericFilterBean {
SecurityContextHolder.getContext().setAuthentication(auth); SecurityContextHolder.getContext().setAuthentication(auth);
} }
} }
// 无授权 且是访问白名单
// if (Objects.isNull(auth) && whiteListHandler.match(request)) {
// //设置游客访问
// auth = new AnonymousAuthenticationToken(IdUtil.fastSimpleUUID(), "anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
// SecurityContextHolder.getContext().setAuthentication(auth);
// }
} catch (Exception e) { } catch (Exception e) {
ResponseWriteUtil.write(response, CommonResult.unauthorized()); ResponseWriteUtil.write(response, CommonResult.unauthorized());
return; return;
} }
filterChain.doFilter(req, res); filterChain.doFilter(request, response);
} }
} }

View File

@ -4,6 +4,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import javax.servlet.http.HttpServletRequest;
@Configuration @Configuration
public class WhiteListHandler { public class WhiteListHandler {
private final static String[] DEFAULT_WHITE_LIST = new String[]{ private final static String[] DEFAULT_WHITE_LIST = new String[]{
@ -35,4 +37,8 @@ public class WhiteListHandler {
.authenticated() .authenticated()
.and(); .and();
} }
// public boolean match(HttpServletRequest request) {
// return matcherList.stream().anyMatch(matcher -> matcher.matches(request));
// }
} }

View File

@ -7,7 +7,6 @@ import cn.hutool.json.JSONUtil;
import com.zhangshu.chat.demo.config.UserDetailsImpl; import com.zhangshu.chat.demo.config.UserDetailsImpl;
import com.zhangshu.chat.demo.entity.Room; import com.zhangshu.chat.demo.entity.Room;
import com.zhangshu.chat.demo.service.RoomCache; import com.zhangshu.chat.demo.service.RoomCache;
import com.zhangshu.chat.demo.vo.RoomUserVo;
import com.zhangshu.chat.demo.vo.UserMessageVo; import com.zhangshu.chat.demo.vo.UserMessageVo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -17,7 +16,6 @@ import javax.websocket.*;
import javax.websocket.server.PathParam; import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import java.io.IOException; import java.io.IOException;
import java.security.Principal;
import java.util.*; import java.util.*;
@Slf4j @Slf4j
@ -39,15 +37,15 @@ public class ChatRoom {
*/ */
@OnOpen @OnOpen
public void onOpen(Session session, @PathParam("roomId") String roomId) { public void onOpen(Session session, @PathParam("roomId") String roomId) {
// if (getRoomCache().createSuccess(roomId)) { if (getRoomCache().createSuccess(roomId)) {
// sendRefuseConnectionMessage(session); this.sendMessage("无此房间,拒绝连接", session);
// return; return;
// } }
// UserDetailsImpl userInfo = (UserDetailsImpl) session.getUserPrincipal(); UserDetailsImpl userInfo = (UserDetailsImpl) session.getUserPrincipal();
// if (Objects.isNull(userInfo) || !getRoomCache().isExistUser(userInfo.getId())) { if (Objects.isNull(userInfo) || !getRoomCache().isExistUser(userInfo.getId())) {
// sendRefuseConnectionMessage(session); this.sendMessage("用户验证失败或者房间内无此用户,拒绝连接", session);
// return; return;
// } }
String sessionId = session.getId().toLowerCase(); String sessionId = session.getId().toLowerCase();
SESSION_CACHE.put(sessionId, session); SESSION_CACHE.put(sessionId, session);
if (StringUtils.isBlank(this.roomId)) { if (StringUtils.isBlank(this.roomId)) {
@ -67,24 +65,24 @@ public class ChatRoom {
public void onMessage(String message, Session session) { public void onMessage(String message, Session session) {
log.info("服务端接收消息成功,消息内容:{}", message); log.info("服务端接收消息成功,消息内容:{}", message);
// 处理消息并响应给客户端 // 处理消息并响应给客户端
// Room room = getRoomCache().get(this.roomId); Room room = getRoomCache().get(this.roomId);
// if (Objects.isNull(room)) { if (Objects.isNull(room)) {
// return; return;
// } }
// UserDetailsImpl userInfo = (UserDetailsImpl) session.getUserPrincipal(); UserDetailsImpl userInfo = (UserDetailsImpl) session.getUserPrincipal();
// if (Objects.isNull(userInfo)) { if (Objects.isNull(userInfo)) {
// return; return;
// } }
// UserMessageVo messageVo = UserMessageVo.builder()
// .id(userInfo.getId())
// .nickname(userInfo.getNickname())
// .username(userInfo.getUsername())
// .message(message)
// .build();
UserMessageVo messageVo = UserMessageVo.builder() UserMessageVo messageVo = UserMessageVo.builder()
.id(userInfo.getId())
.nickname(userInfo.getNickName())
.username(userInfo.getUsername())
.message(message) .message(message)
.build(); .build();
// UserMessageVo messageVo = UserMessageVo.builder()
// .message(message)
// .build();
String text = JSONUtil.toJsonStr(messageVo); String text = JSONUtil.toJsonStr(messageVo);
ROOM_SESSION_MAP.get(roomId).stream().map(SESSION_CACHE::get).filter(Objects::nonNull) ROOM_SESSION_MAP.get(roomId).stream().map(SESSION_CACHE::get).filter(Objects::nonNull)
.forEach(v -> this.sendMessage(text, v)); .forEach(v -> this.sendMessage(text, v));
@ -110,10 +108,6 @@ public class ChatRoom {
} }
private void sendRefuseConnectionMessage(Session session) {
this.sendMessage("拒绝连接", session);
}
/** /**
* 关闭连接 * 关闭连接
* *