Compare commits

..

3 Commits

Author SHA1 Message Date
ivmiku
17a52ed471 fix: 依赖改名 2024-09-14 18:06:46 +08:00
ivmiku
b987c11cd1 fix: 依赖改名 2024-09-14 14:57:39 +08:00
ivmiku
62a521388d feat: 修改网关配置 2024-09-14 13:57:21 +08:00
7 changed files with 59 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ public class SaTokenConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
// 注册注解拦截器,并排除不需要注解鉴权的接口地址 (与登录拦截器无关)
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
.addPathPatterns("/**");
.addPathPatterns("/**")
.excludePathPatterns("/swagger-ui/**");
}
}

View File

@@ -0,0 +1,34 @@
package com.ivmiku.tutorial.config;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.util.WebAppRootListener;
@Configuration
public class WebSocketConfig implements ServletContextInitializer {
@Bean
public ServerEndpointExporter serverEndpointExporter (){
return new ServerEndpointExporter();
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(WebAppRootListener.class);
servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","102400000");
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
// 在此处设置bufferSize
container.setMaxTextMessageBufferSize(50*1024*1024);
container.setMaxBinaryMessageBufferSize(50*1024*1024);
container.setMaxSessionIdleTimeout(15 * 60000L);
return container;
}
}

View File

@@ -4,7 +4,6 @@ import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Comment;
import com.ivmiku.tutorial.entity.Pages;
import com.ivmiku.tutorial.response.AtNotifier;
@@ -13,7 +12,6 @@ import com.ivmiku.tutorial.service.CommentService;
import com.ivmiku.tutorial.service.FileService;
import com.ivmiku.tutorial.util.MyRedisUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.apiguardian.api.API;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@@ -21,7 +19,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.List;
/**
* 评论控制器用于处理评论相关的HTTP请求。
@@ -44,7 +41,7 @@ public class CommentController {
private RabbitTemplate rabbitTemplate;
@Resource
private MyRedisUtil redisUtil;
private MyRedisUtil myRedisUtil;
/**
@@ -83,7 +80,7 @@ public class CommentController {
notifier.setToId(mentionedUserId);
notifier.setPostId(String.valueOf(comment.getCommentId()));
rabbitTemplate.convertAndSend("exchange2", "", JSON.toJSONString(notifier));
redisUtil.listAdd("at:" + notifier.getToId(), JSON.toJSONString(notifier));
myRedisUtil.listAdd("at:" + notifier.getToId(), JSON.toJSONString(notifier));
}
return Result.ok(comment);
}
@@ -131,7 +128,7 @@ public class CommentController {
notifier.setToId(mentionedUserId);
notifier.setPostId(String.valueOf(reply.getCommentId()));
rabbitTemplate.convertAndSend("exchange2", "", JSON.toJSONString(notifier));
redisUtil.listAdd("at:" + notifier.getToId(), JSON.toJSONString(notifier));
myRedisUtil.listAdd("at:" + notifier.getToId(), JSON.toJSONString(notifier));
}
return Result.ok(reply);
}

View File

@@ -8,7 +8,9 @@ import com.ivmiku.tutorial.service.impl.SearchService;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import java.io.IOException;
@@ -18,7 +20,7 @@ import java.util.concurrent.ConcurrentHashMap;
@Controller
@ServerEndpoint(value = "/search/{satoken}")
public class WebSocketServer {
public class WebSocketServer implements ApplicationContextAware {
public static Map<String, Session> sessionMap = new ConcurrentHashMap<>();
private static ApplicationContext applicationContext;
private SearchService searchService;
@@ -61,7 +63,7 @@ public class WebSocketServer {
}
case '#' -> {
String sub = message.substring(1);
List<String> list = searchService.getTag(message);
List<String> list = searchService.getTag(sub);
session.getBasicRemote().sendText(JSON.toJSONString(Result.ok(list)));
}
//预留给社区搜索
@@ -70,4 +72,9 @@ public class WebSocketServer {
}
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
WebSocketServer.applicationContext = applicationContext;
}
}

View File

@@ -47,7 +47,8 @@ public class SaTokenConfigure {
"/user/register",
"/swagger-resources/**",
"/v3/**",
"/swagger-ui/**")
"/swagger-ui/**",
"/ws/**")
// 鉴权方法:每次访问进入
.setAuth(obj -> {
// 登录校验 -- 拦截所有路由,并排除指定路由

View File

@@ -26,3 +26,7 @@ spring.cloud.gateway.routes[2].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[3].id=websocket
spring.cloud.gateway.routes[3].uri=lb:ws://user
spring.cloud.gateway.routes[3].predicates[0]=Path=/chat/**
spring.cloud.gateway.routes[4].id=search
spring.cloud.gateway.routes[4].uri=lb:ws://community
spring.cloud.gateway.routes[4].predicates[0]=Path=/search/**

View File

@@ -25,3 +25,7 @@ spring.cloud.gateway.routes[2].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[3].id=websocket
spring.cloud.gateway.routes[3].uri=lb:ws://user
spring.cloud.gateway.routes[3].predicates[0]=Path=/chat/**
spring.cloud.gateway.routes[4].id=search
spring.cloud.gateway.routes[4].uri=lb:ws://community
spring.cloud.gateway.routes[4].predicates[0]=Path=/search/**