使用ThreadLocal获取用户信息

This commit is contained in:
xbx 2024-05-16 19:45:57 +08:00
parent 58840f9a0c
commit aec28947fe
11 changed files with 23 additions and 24 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -4,6 +4,7 @@ package com.xubx.springboot_01demo.controller;
import com.xubx.springboot_01demo.pojo.User;
import com.xubx.springboot_01demo.service.UserService;
import com.xubx.springboot_01demo.utils.token.RedisUtil;
import com.xubx.springboot_01demo.utils.token.RequestHolder;
import com.xubx.springboot_01demo.utils.token.TokenGenerate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -20,8 +21,6 @@ public class UserController {
UserService userService;
@Resource
RedisUtil redisUtil;
public static String userName;
public static String userNameToken;
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody User user) {
@ -36,10 +35,8 @@ public class UserController {
public String login(@RequestBody User user) {
//登陆
if (userService.findUserByUsername(user)) {
userName = user.getUsername();
userNameToken = userName;
String token = new TokenGenerate().generateToken(user.getUsername());
redisUtil.addTokens(user.getUsername(), token);
redisUtil.addTokens(token);
return token;
}
return "false";
@ -47,15 +44,11 @@ public class UserController {
@GetMapping("/getUsername")
public String getUserName() {
System.out.println("当前线程id:" + Thread.currentThread().getId());
return userName;
return RequestHolder.getuserId();
}
@RequestMapping("/uploadAvatar")
public void uploadAvatar(MultipartFile file) throws IOException {
System.out.println("当前线程id:" + Thread.currentThread().getId());
String pType = file.getContentType();
pType = pType.substring(pType.indexOf("/") + 1);
if ("jpeg".equals(pType)) {
@ -67,21 +60,20 @@ public class UserController {
String absolutePath = currentWorkingDirectory+"/static" + relativePath;
file.transferTo(new File(absolutePath));
System.out.println("导入数据库的路径:" + relativePath);
userService.addAvatar(relativePath, userName);
userService.addAvatar(relativePath, RequestHolder.getuserId());
}
@GetMapping("/getAvatar")
public ResponseEntity<String> getAvatar() {
System.out.println("当前线程id:" + Thread.currentThread().getId());
String path = userService.getAvatar(userName);
String path = userService.getAvatar(RequestHolder.getuserId());
System.out.println("发给前端的路径:" + path);
return ResponseEntity.ok(path);
}
@GetMapping("usernameChange")
public ResponseEntity<String> usernameChange(@RequestParam("username") String username){
if(userService.usernameChange(userName,username)){
userName = username;
if(userService.usernameChange(RequestHolder.getuserId(),username)){
RequestHolder.add(username);
return ResponseEntity.ok("修改成功!");
}
return ResponseEntity.ok("该用户已存在!");
@ -89,7 +81,7 @@ public class UserController {
@GetMapping("passwordChange")
public ResponseEntity<String> passwordChange(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){
if(userService.passwordChange(userName,oldPassword,newPassword)){
if(userService.passwordChange(RequestHolder.getuserId(),oldPassword,newPassword)){
return ResponseEntity.ok("修改成功!");
}
return ResponseEntity.ok("原密码输入错误!");

View File

@ -4,6 +4,7 @@ import com.xubx.springboot_01demo.controller.UserController;
import com.xubx.springboot_01demo.mapper.CommentMapper;
import com.xubx.springboot_01demo.pojo.Comment;
import com.xubx.springboot_01demo.service.CommentService;
import com.xubx.springboot_01demo.utils.token.RequestHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -22,7 +23,7 @@ public class CommentServiceImpl implements CommentService {
public void addComment(Comment comment) {
Comment comment1 = new Comment();
comment1.setArticle_id(comment.getArticle_id());
comment1.setUsername(UserController.userName);
comment1.setUsername(RequestHolder.getuserId());
comment1.setContent(comment.getContent());
comment1.setParent_id(comment.getParent_id());
comment1.setParent_name(comment.getParent_name());

View File

@ -12,13 +12,13 @@ public class RedisUtil {
@Resource
private RedisTemplate<String, String> stringRedisTemplate;//这是一个使用redis的API可以直接用StringRedisTemplate
public void addTokens(String username, String token) {//存入token
System.out.println(username+token+"打印addTokens");
public void addTokens(String token) {//存入token
System.out.println(token+"打印addTokens");
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set(username, token);
valueOperations.set("token", token);
}
public String getTokens(String username) {//获取token
return stringRedisTemplate.opsForValue().get(username);
public String getTokens() {//获取token
return stringRedisTemplate.opsForValue().get("token");
}
public void delTokens(String username) {//删除token在前端已经进行

View File

@ -31,7 +31,7 @@ public class TokenGenerate {
try {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
DecodedJWT jwt = verifier.verify(token);
System.out.println("当前线程id:" + Thread.currentThread().getId());
RequestHolder.add(jwt.getClaim("username").asString());
System.out.println("认证通过:");
System.out.println("issuer: " + jwt.getIssuer());
System.out.println("username: " + jwt.getClaim("username").asString());

View File

@ -26,7 +26,7 @@ public class TokenInterceptor implements HandlerInterceptor {
}
response.setCharacterEncoding("utf-8");
String token = redisUtil.getTokens(UserController.userNameToken);
String token = redisUtil.getTokens();
if(token != null){
boolean result = TokenGenerate.verify(token);
if(result){