1.1.1编写userinfo接口
1.1.2 User实体类中增加转json忽略password注释
package com.geji.pojo;import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonInclude;import lombok.Data;import java.time.LocalDateTime;//lombok 在编译阶段为实体类自动生成setter,getter,tostring//步骤:pom文件中引入依赖,在实体类上添加注解@Datapublic class User {private Integer id;//主键IDprivate String username;//用户名@JsonIgnoreprivate String password;//密码private String nickname;//昵称private String email;//邮箱private String userPic;//用户头像地址private LocalDateTime createTime;//创建时间private LocalDateTime updateTime;//更新时间}
1.1.3.1实体类驼峰命名和数据库下划线命名无法匹配
1.1.3.2 需要在配置文件中配置
1.1.3在postman中测试
1.2 从拦截器中获取解析完的token而不是在controller中自己解析
1.2.1 编写工具类ThreadLocalUtil
package com.geji.utils;import java.util.HashMap;import java.util.Map;/** * ThreadLocal 工具类 */@SuppressWarnings("all")public class ThreadLocalUtil {//提供ThreadLocal对象,private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();//根据键获取值public static T get(){return (T) THREAD_LOCAL.get();}//存储键值对public static void set(Object value){THREAD_LOCAL.set(value);}//清除ThreadLocal 防止内存泄漏public static void remove(){THREAD_LOCAL.remove();}}
1.2.2 在拦截器中放数据,并且记得清数据防止内存泄漏
package com.itheima.interceptors;import com.itheima.pojo.Result;import com.itheima.utils.JwtUtil;import com.itheima.utils.ThreadLocalUtil;import jakarta.servlet.http.HttpServletRequest;import jakarta.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import java.util.Map;@Componentpublic class LoginInterceptor implements HandlerInterceptor {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//令牌验证String token = request.getHeader("Authorization");//验证tokentry {//从redis中获取相同的tokenValueOperations operations = stringRedisTemplate.opsForValue();String redisToken = operations.get(token);if (redisToken==null){//token已经失效了throw new RuntimeException();}Map claims = JwtUtil.parseToken(token);//把业务数据存储到ThreadLocal中ThreadLocalUtil.set(claims);//放行return true;} catch (Exception e) {//http响应状态码为401response.setStatus(401);//不放行return false;}}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {//清空ThreadLocal中的数据ThreadLocalUtil.remove();}}
1.2.3 在userinfo中拿数据
package com.geji.controller;import com.geji.pojo.Result;import com.geji.pojo.User;import com.geji.service.UserService;import com.geji.utils.JwtUtil;import com.geji.utils.Md5Util;import com.geji.utils.ThreadLocalUtil;import jakarta.validation.constraints.Pattern;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*;import java.util.HashMap;import java.util.Map;import java.util.concurrent.TimeUnit;@RestController@RequestMapping("/user")@Validatedpublic class UserController {@Autowiredprivate UserService userService;@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {//查询用户User u = userService.findByUserName(username);if (u == null) {//没有占用//注册userService.register(username, password);return Result.success();} else {//占用return Result.error("用户名已被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {//根据用户名查询用户User loginUser = userService.findByUserName(username);//判断该用户是否存在if (loginUser == null) {return Result.error("用户名错误");}//判断密码是否正确loginUser对象中的password是密文if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {//登录成功Map claims = new HashMap();claims.put("id", loginUser.getId());claims.put("username", loginUser.getUsername());String token = JwtUtil.genToken(claims);//把token存储到redis中//ValueOperations operations = stringRedisTemplate.opsForValue();//operations.set(token,token,1, TimeUnit.HOURS);return Result.success(token);}return Result.error("密码错误");}@GetMapping("/userInfo")public Result userInfo() {Map map = ThreadLocalUtil.get();String username = (String) map.get("username");User user = userService.findByUserName(username);return Result.success(user);}}