使用ThreadLocal获取用户信息
This commit is contained in:
parent
58840f9a0c
commit
aec28947fe
|
@ -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>
|
|
@ -4,6 +4,7 @@ package com.xubx.springboot_01demo.controller;
|
||||||
import com.xubx.springboot_01demo.pojo.User;
|
import com.xubx.springboot_01demo.pojo.User;
|
||||||
import com.xubx.springboot_01demo.service.UserService;
|
import com.xubx.springboot_01demo.service.UserService;
|
||||||
import com.xubx.springboot_01demo.utils.token.RedisUtil;
|
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 com.xubx.springboot_01demo.utils.token.TokenGenerate;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -20,8 +21,6 @@ public class UserController {
|
||||||
UserService userService;
|
UserService userService;
|
||||||
@Resource
|
@Resource
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
public static String userName;
|
|
||||||
public static String userNameToken;
|
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public ResponseEntity<String> register(@RequestBody User user) {
|
public ResponseEntity<String> register(@RequestBody User user) {
|
||||||
|
@ -36,10 +35,8 @@ public class UserController {
|
||||||
public String login(@RequestBody User user) {
|
public String login(@RequestBody User user) {
|
||||||
//登陆
|
//登陆
|
||||||
if (userService.findUserByUsername(user)) {
|
if (userService.findUserByUsername(user)) {
|
||||||
userName = user.getUsername();
|
|
||||||
userNameToken = userName;
|
|
||||||
String token = new TokenGenerate().generateToken(user.getUsername());
|
String token = new TokenGenerate().generateToken(user.getUsername());
|
||||||
redisUtil.addTokens(user.getUsername(), token);
|
redisUtil.addTokens(token);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
return "false";
|
return "false";
|
||||||
|
@ -47,15 +44,11 @@ public class UserController {
|
||||||
|
|
||||||
@GetMapping("/getUsername")
|
@GetMapping("/getUsername")
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
System.out.println("当前线程id:" + Thread.currentThread().getId());
|
return RequestHolder.getuserId();
|
||||||
|
|
||||||
return userName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/uploadAvatar")
|
@RequestMapping("/uploadAvatar")
|
||||||
public void uploadAvatar(MultipartFile file) throws IOException {
|
public void uploadAvatar(MultipartFile file) throws IOException {
|
||||||
System.out.println("当前线程id:" + Thread.currentThread().getId());
|
|
||||||
|
|
||||||
String pType = file.getContentType();
|
String pType = file.getContentType();
|
||||||
pType = pType.substring(pType.indexOf("/") + 1);
|
pType = pType.substring(pType.indexOf("/") + 1);
|
||||||
if ("jpeg".equals(pType)) {
|
if ("jpeg".equals(pType)) {
|
||||||
|
@ -67,21 +60,20 @@ public class UserController {
|
||||||
String absolutePath = currentWorkingDirectory+"/static" + relativePath;
|
String absolutePath = currentWorkingDirectory+"/static" + relativePath;
|
||||||
file.transferTo(new File(absolutePath));
|
file.transferTo(new File(absolutePath));
|
||||||
System.out.println("导入数据库的路径:" + relativePath);
|
System.out.println("导入数据库的路径:" + relativePath);
|
||||||
userService.addAvatar(relativePath, userName);
|
userService.addAvatar(relativePath, RequestHolder.getuserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getAvatar")
|
@GetMapping("/getAvatar")
|
||||||
public ResponseEntity<String> getAvatar() {
|
public ResponseEntity<String> getAvatar() {
|
||||||
System.out.println("当前线程id:" + Thread.currentThread().getId());
|
String path = userService.getAvatar(RequestHolder.getuserId());
|
||||||
String path = userService.getAvatar(userName);
|
|
||||||
System.out.println("发给前端的路径:" + path);
|
System.out.println("发给前端的路径:" + path);
|
||||||
return ResponseEntity.ok(path);
|
return ResponseEntity.ok(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("usernameChange")
|
@GetMapping("usernameChange")
|
||||||
public ResponseEntity<String> usernameChange(@RequestParam("username") String username){
|
public ResponseEntity<String> usernameChange(@RequestParam("username") String username){
|
||||||
if(userService.usernameChange(userName,username)){
|
if(userService.usernameChange(RequestHolder.getuserId(),username)){
|
||||||
userName = username;
|
RequestHolder.add(username);
|
||||||
return ResponseEntity.ok("修改成功!");
|
return ResponseEntity.ok("修改成功!");
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok("该用户已存在!");
|
return ResponseEntity.ok("该用户已存在!");
|
||||||
|
@ -89,7 +81,7 @@ public class UserController {
|
||||||
|
|
||||||
@GetMapping("passwordChange")
|
@GetMapping("passwordChange")
|
||||||
public ResponseEntity<String> passwordChange(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){
|
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("修改成功!");
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok("原密码输入错误!");
|
return ResponseEntity.ok("原密码输入错误!");
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.xubx.springboot_01demo.controller.UserController;
|
||||||
import com.xubx.springboot_01demo.mapper.CommentMapper;
|
import com.xubx.springboot_01demo.mapper.CommentMapper;
|
||||||
import com.xubx.springboot_01demo.pojo.Comment;
|
import com.xubx.springboot_01demo.pojo.Comment;
|
||||||
import com.xubx.springboot_01demo.service.CommentService;
|
import com.xubx.springboot_01demo.service.CommentService;
|
||||||
|
import com.xubx.springboot_01demo.utils.token.RequestHolder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
@ -22,7 +23,7 @@ public class CommentServiceImpl implements CommentService {
|
||||||
public void addComment(Comment comment) {
|
public void addComment(Comment comment) {
|
||||||
Comment comment1 = new Comment();
|
Comment comment1 = new Comment();
|
||||||
comment1.setArticle_id(comment.getArticle_id());
|
comment1.setArticle_id(comment.getArticle_id());
|
||||||
comment1.setUsername(UserController.userName);
|
comment1.setUsername(RequestHolder.getuserId());
|
||||||
comment1.setContent(comment.getContent());
|
comment1.setContent(comment.getContent());
|
||||||
comment1.setParent_id(comment.getParent_id());
|
comment1.setParent_id(comment.getParent_id());
|
||||||
comment1.setParent_name(comment.getParent_name());
|
comment1.setParent_name(comment.getParent_name());
|
||||||
|
|
|
@ -12,13 +12,13 @@ public class RedisUtil {
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String, String> stringRedisTemplate;//这是一个使用redis的API,可以直接用StringRedisTemplate
|
private RedisTemplate<String, String> stringRedisTemplate;//这是一个使用redis的API,可以直接用StringRedisTemplate
|
||||||
|
|
||||||
public void addTokens(String username, String token) {//存入token
|
public void addTokens(String token) {//存入token
|
||||||
System.out.println(username+token+"打印addTokens");
|
System.out.println(token+"打印addTokens");
|
||||||
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
|
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
|
||||||
valueOperations.set(username, token);
|
valueOperations.set("token", token);
|
||||||
}
|
}
|
||||||
public String getTokens(String username) {//获取token
|
public String getTokens() {//获取token
|
||||||
return stringRedisTemplate.opsForValue().get(username);
|
return stringRedisTemplate.opsForValue().get("token");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delTokens(String username) {//删除token在前端已经进行
|
public void delTokens(String username) {//删除token在前端已经进行
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class TokenGenerate {
|
||||||
try {
|
try {
|
||||||
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
|
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
|
||||||
DecodedJWT jwt = verifier.verify(token);
|
DecodedJWT jwt = verifier.verify(token);
|
||||||
System.out.println("当前线程id:" + Thread.currentThread().getId());
|
RequestHolder.add(jwt.getClaim("username").asString());
|
||||||
System.out.println("认证通过:");
|
System.out.println("认证通过:");
|
||||||
System.out.println("issuer: " + jwt.getIssuer());
|
System.out.println("issuer: " + jwt.getIssuer());
|
||||||
System.out.println("username: " + jwt.getClaim("username").asString());
|
System.out.println("username: " + jwt.getClaim("username").asString());
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class TokenInterceptor implements HandlerInterceptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
String token = redisUtil.getTokens(UserController.userNameToken);
|
String token = redisUtil.getTokens();
|
||||||
if(token != null){
|
if(token != null){
|
||||||
boolean result = TokenGenerate.verify(token);
|
boolean result = TokenGenerate.verify(token);
|
||||||
if(result){
|
if(result){
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue