根据token从查询个人信息接口开发
- 直接解密token,获取个人信息
- 通过token解密查询数据库获取个人信息
UserController.java
package net.ybclass.online_ybclass.controller;
import net.ybclass.online_ybclass.model.entity.User;
import net.ybclass.online_ybclass.model.request.LoginRequest;
import net.ybclass.online_ybclass.service.UserService;
import net.ybclass.online_ybclass.utils.JsonData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping("/api/v1/pri/user")
public class UserController {
@Autowired(required = false)
private UserService userService;
/**
* 注册接口
* @param userInfo
* @return
*/
@PostMapping("register")
public JsonData register(@RequestBody Map<String,String> userInfo){
return userService.save(userInfo)==1?JsonData.buildSuccess():JsonData.buildError("注册失败,请重试");
}
@PostMapping("find_phone")
public JsonData findByPhone(@Param("phone") String phone){
return JsonData.buildSuccess(userService.findByPhone(phone));
}
/**
* 登陆接口
* @param loginRequest
* @return
*/
@PostMapping("login")
public JsonData login(@RequestBody LoginRequest loginRequest){
String token = userService.findByPhoneAndPwd(loginRequest.getPhone(),loginRequest.getPwd());
return token==null?JsonData.buildError("登陆失败,账号密码错误"):JsonData.buildSuccess(token);
}
/**
* 根据用户id查询用户信息
* @param request
* @return
*/
@GetMapping("find_by_token")
public JsonData findUserInfoByToken(HttpServletRequest request){
Integer userId=(Integer) request.getAttribute("user_id");
if (userId==null) return JsonData.buildError("查询失败");
User user =userService.findByUserId(userId);
return JsonData.buildSuccess(user);
}
}
UserService.java
UserServiceImpl.java
UserMapper.java
UserMapper.xml
演示