1.修改发送AI信息token没有正确的问题
This commit is contained in:
@@ -9,6 +9,8 @@ import com.srs.common.core.controller.BaseController;
|
|||||||
// OkHttp 显式导入
|
// OkHttp 显式导入
|
||||||
import com.srs.common.exception.ServiceException;
|
import com.srs.common.exception.ServiceException;
|
||||||
import com.srs.common.utils.SecurityUtils;
|
import com.srs.common.utils.SecurityUtils;
|
||||||
|
import com.srs.common.utils.ServletUtils;
|
||||||
|
import com.srs.framework.web.service.TokenService;
|
||||||
import com.srs.teacher.domain.dto.ConversationDTO;
|
import com.srs.teacher.domain.dto.ConversationDTO;
|
||||||
import com.srs.web.core.config.DifyConfig;
|
import com.srs.web.core.config.DifyConfig;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
@@ -42,7 +44,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/aitutor/aichat")
|
@RequestMapping("/aitutor/aichat")
|
||||||
public class AiChatController extends BaseController {
|
public class AiChatController extends BaseController {
|
||||||
|
@Autowired
|
||||||
|
private TokenService tokenService;
|
||||||
/**
|
/**
|
||||||
* HTTP客户端实例
|
* HTTP客户端实例
|
||||||
* 配置了5分钟的读取超时时间,用于与Dify API进行通信
|
* 配置了5分钟的读取超时时间,用于与Dify API进行通信
|
||||||
@@ -117,7 +120,9 @@ public class AiChatController extends BaseController {
|
|||||||
|
|
||||||
// 在主线程中获取当前用户名,避免在异步线程中获取安全上下文
|
// 在主线程中获取当前用户名,避免在异步线程中获取安全上下文
|
||||||
String currentUsername = SecurityUtils.getLoginUser().getUsername();
|
String currentUsername = SecurityUtils.getLoginUser().getUsername();
|
||||||
String currentUserToken = SecurityUtils.getLoginUser().getToken();
|
// 获取JWT token
|
||||||
|
String currentUserToken = tokenService.getToken(ServletUtils.getRequest());
|
||||||
|
// String currentUserToken = SecurityUtils.getLoginUser().getToken();
|
||||||
// 异步执行请求处理,避免阻塞主线程
|
// 异步执行请求处理,避免阻塞主线程
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
try {
|
try {
|
||||||
@@ -147,7 +152,8 @@ public class AiChatController extends BaseController {
|
|||||||
* @param currentUsername
|
* @param currentUsername
|
||||||
* @throws IOException 当网络请求或IO操作失败时抛出
|
* @throws IOException 当网络请求或IO操作失败时抛出
|
||||||
*/
|
*/
|
||||||
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername,String currentUserToken)
|
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername,
|
||||||
|
String currentUserToken)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
// 构建请求体参数
|
// 构建请求体参数
|
||||||
@@ -184,7 +190,7 @@ public class AiChatController extends BaseController {
|
|||||||
inputs.put("user_name", userName);
|
inputs.put("user_name", userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Object userToken = requestData.get("user_token");
|
// Object userToken = requestData.get("user_token");
|
||||||
if (currentUserToken != null) {
|
if (currentUserToken != null) {
|
||||||
inputs.put("user_token", currentUserToken);
|
inputs.put("user_token", currentUserToken);
|
||||||
}
|
}
|
||||||
@@ -459,9 +465,9 @@ public class AiChatController extends BaseController {
|
|||||||
@PreAuthorize("@ss.hasPermi('cph:teacher:list')")
|
@PreAuthorize("@ss.hasPermi('cph:teacher:list')")
|
||||||
@GetMapping("/getMessagesToAdmin")
|
@GetMapping("/getMessagesToAdmin")
|
||||||
public AjaxResult getMessagesToAdmin(@RequestParam String user,
|
public AjaxResult getMessagesToAdmin(@RequestParam String user,
|
||||||
@RequestParam(required = false) String firstId,
|
@RequestParam(required = false) String firstId,
|
||||||
@RequestParam(defaultValue = "20") int limit,
|
@RequestParam(defaultValue = "20") int limit,
|
||||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 首先尝试从Redis缓存中获取会话ID
|
// 首先尝试从Redis缓存中获取会话ID
|
||||||
@@ -497,8 +503,8 @@ public class AiChatController extends BaseController {
|
|||||||
// 权限标识为学生
|
// 权限标识为学生
|
||||||
@GetMapping("/getMessagesToUser")
|
@GetMapping("/getMessagesToUser")
|
||||||
public AjaxResult getMessagesToUser(@RequestParam(required = false) String firstId,
|
public AjaxResult getMessagesToUser(@RequestParam(required = false) String firstId,
|
||||||
@RequestParam(defaultValue = "20") int limit,
|
@RequestParam(defaultValue = "20") int limit,
|
||||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String user = SecurityUtils.getLoginUser().getUsername();
|
String user = SecurityUtils.getLoginUser().getUsername();
|
||||||
@@ -561,7 +567,7 @@ public class AiChatController extends BaseController {
|
|||||||
/**
|
/**
|
||||||
* 将会话ID与用户绑定并存储到Redis中
|
* 将会话ID与用户绑定并存储到Redis中
|
||||||
*
|
*
|
||||||
* @param user 用户名
|
* @param user 用户名
|
||||||
* @param conversationId 会话ID
|
* @param conversationId 会话ID
|
||||||
*/
|
*/
|
||||||
private void cacheConversationId(String user, String conversationId) {
|
private void cacheConversationId(String user, String conversationId) {
|
||||||
|
@@ -209,7 +209,7 @@ public class TokenService
|
|||||||
* @param request
|
* @param request
|
||||||
* @return token
|
* @return token
|
||||||
*/
|
*/
|
||||||
private String getToken(HttpServletRequest request)
|
public String getToken(HttpServletRequest request)
|
||||||
{
|
{
|
||||||
String token = request.getHeader(header);
|
String token = request.getHeader(header);
|
||||||
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
||||||
|
Reference in New Issue
Block a user