Skip to content

Commit

Permalink
🐛 使用失效 token 请求接口返回 401 数据重复问题修复
Browse files Browse the repository at this point in the history
🐛 使用失效 token 请求接口返回 401 数据重复问题修复
  • Loading branch information
ronger-x authored Jul 5, 2024
2 parents 3f69abd + bb19d5e commit 61e54fb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/rymcu/forest/auth/JwtFilter.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -126,7 +129,8 @@ private void onLoginFail(ServletResponse response) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;

/**
Expand All @@ -21,4 +22,9 @@ public RedisMessageListenerContainer container(RedisConnectionFactory connection
container.setConnectionFactory(connectionFactory);
return container;
}

@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return new StringRedisTemplate(redisConnectionFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public GlobalResult newChat(@RequestBody List<ChatMessageModel> messages) {
throw new IllegalArgumentException("参数异常!");
}
User user = UserUtils.getCurrentUserByToken();
Collections.reverse(messages);
List<ChatMessage> list = new ArrayList<>(messages.size());
if (messages.size() > 4) {
messages = messages.subList(messages.size() - 4, messages.size());
Expand All @@ -78,6 +77,7 @@ public GlobalResult newChat(@RequestBody List<ChatMessageModel> messages) {

@NotNull
private GlobalResult sendMessage(User user, List<ChatMessage> 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")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/rymcu/forest/util/UserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public static TokenUser getTokenUser(String token) {
}
throw new UnauthenticatedException();
}

public static boolean isAdmin(String email) {
return userMapper.hasAdminPermission(email);
}
}

0 comments on commit 61e54fb

Please sign in to comment.