diff --git a/src/main/java/com/rymcu/forest/auth/JwtFilter.java b/src/main/java/com/rymcu/forest/auth/JwtFilter.java index b2fb341..e7c2835 100644 --- a/src/main/java/com/rymcu/forest/auth/JwtFilter.java +++ b/src/main/java/com/rymcu/forest/auth/JwtFilter.java @@ -1,7 +1,10 @@ package com.rymcu.forest.auth; import com.alibaba.fastjson2.JSONObject; +import com.rymcu.forest.core.result.GlobalResult; import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.core.result.ResultCode; +import com.rymcu.forest.util.ErrorCode; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureException; @@ -126,7 +129,8 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setContentType("application/json"); httpServletResponse.setCharacterEncoding("UTF-8"); - httpServletResponse.getOutputStream().write(JSONObject.toJSONString(GlobalResultGenerator.genErrorResult("未登录或已登录超时,请重新登录")).getBytes()); + httpServletResponse.getOutputStream().write(JSONObject.toJSONString(new GlobalResult<>(ResultCode.UNAUTHENTICATED)).getBytes()); + httpServletResponse.getOutputStream().flush(); } catch (IOException e) { // 错误日志 log.error(e.getMessage()); diff --git a/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java b/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java index 676ee27..ec7ee36 100644 --- a/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java +++ b/src/main/java/com/rymcu/forest/config/RedisListenerConfig.java @@ -3,6 +3,7 @@ package com.rymcu.forest.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** @@ -21,4 +22,9 @@ public class RedisListenerConfig { container.setConnectionFactory(connectionFactory); return container; } + + @Bean + public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { + return new StringRedisTemplate(redisConnectionFactory); + } } diff --git a/src/main/java/com/rymcu/forest/openai/OpenAiController.java b/src/main/java/com/rymcu/forest/openai/OpenAiController.java index 4375c81..d12be57 100644 --- a/src/main/java/com/rymcu/forest/openai/OpenAiController.java +++ b/src/main/java/com/rymcu/forest/openai/OpenAiController.java @@ -60,7 +60,6 @@ public class OpenAiController { throw new IllegalArgumentException("参数异常!"); } User user = UserUtils.getCurrentUserByToken(); - Collections.reverse(messages); List list = new ArrayList<>(messages.size()); if (messages.size() > 4) { messages = messages.subList(messages.size() - 4, messages.size()); @@ -78,6 +77,7 @@ public class OpenAiController { @NotNull private GlobalResult sendMessage(User user, List list) { + boolean isAdmin = UserUtils.isAdmin(user.getEmail()); OpenAiService service = new OpenAiService(token, Duration.ofSeconds(180)); ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() .model("gpt-3.5-turbo-16k-0613") diff --git a/src/main/java/com/rymcu/forest/util/UserUtils.java b/src/main/java/com/rymcu/forest/util/UserUtils.java index 78fe61d..277dccf 100644 --- a/src/main/java/com/rymcu/forest/util/UserUtils.java +++ b/src/main/java/com/rymcu/forest/util/UserUtils.java @@ -78,4 +78,8 @@ public class UserUtils { } throw new UnauthenticatedException(); } + + public static boolean isAdmin(String email) { + return userMapper.hasAdminPermission(email); + } }