UserController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.fd.controller;
  2. import com.fd.constant.MsgCode;
  3. import com.fd.entity.UserEntity;
  4. import com.fd.repository.UserRepository;
  5. import com.fd.shiro.JWTUtil;
  6. import com.fd.util.R;
  7. import io.swagger.annotations.Api;
  8. import lombok.extern.log4j.Log4j2;
  9. import org.apache.shiro.SecurityUtils;
  10. import org.apache.shiro.authz.UnauthorizedException;
  11. import org.apache.shiro.authz.annotation.Logical;
  12. import org.apache.shiro.authz.annotation.RequiresAuthentication;
  13. import org.apache.shiro.authz.annotation.RequiresPermissions;
  14. import org.apache.shiro.authz.annotation.RequiresRoles;
  15. import org.apache.shiro.subject.Subject;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.web.bind.annotation.*;
  20. import springfox.documentation.annotations.ApiIgnore;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.util.HashMap;
  23. import java.util.concurrent.TimeUnit;
  24. /**
  25. * Created by Owen on 2019/11/11 0011 16:50
  26. */
  27. @Api(tags = "用户模块")
  28. @Log4j2
  29. @RestController
  30. public class UserController {
  31. @Autowired
  32. private UserRepository userRepository;
  33. @Autowired
  34. private RedisTemplate redisTemplate;
  35. private static final String SALT = "cesium_";
  36. @PostMapping("/login")
  37. public R login(@RequestParam("username") String username,
  38. @RequestParam("password") String password) {
  39. log.warn("run login , username:{}, password:{}", username, password);
  40. UserEntity user = userRepository.findByUsername(username);
  41. if (user == null) {
  42. return new R(53000, MsgCode.U53000);
  43. }
  44. if (!password.equals(user.getPassword())) {
  45. // 密码不相等
  46. throw new UnauthorizedException("error");
  47. }
  48. // 创建新token
  49. String token = JWTUtil.sign(username, password);
  50. // 更新到 redis, 有效期24, 旧token无效
  51. redisTemplate.opsForValue().set(user.getUsername(), token, Long.parseLong("24"), TimeUnit.HOURS);
  52. log.info("token: {}", token);
  53. HashMap<Object, Object> resultMap = new HashMap<>();
  54. resultMap.put("token", token);
  55. resultMap.put("userName", username);
  56. log.warn("end login");
  57. return new R(200, resultMap);
  58. }
  59. @GetMapping("/logout")
  60. public R logout(HttpServletRequest request) {
  61. log.info("run logout");
  62. String token = request.getHeader("Authorization");
  63. String username = JWTUtil.getUsername(token);
  64. String redisToken = (String) redisTemplate.opsForValue().get(username);
  65. // token username 一致,代表没有被踢出
  66. if (username.equals(redisToken)) {
  67. redisTemplate.delete(username);
  68. }
  69. log.info("end logout");
  70. return new R(200, MsgCode.SUCCESS);
  71. }
  72. /**
  73. * 所用用户都可以查看,但登陆跟不登录看到的东西不一样
  74. *
  75. * 用postman 模拟请求头,Authorization
  76. */
  77. @ApiIgnore
  78. @GetMapping("free")
  79. private R free(){
  80. log.info("run free");
  81. Subject subject = SecurityUtils.getSubject();
  82. if (!subject.isAuthenticated()) {
  83. log.info("没有登录, 游客模式………………");
  84. return new R(200, "没有登录, 游客模式………………");
  85. }
  86. log.info("已经登录, 登录模式………………");
  87. return new R(200, "已经登录, 登录模式………………");
  88. }
  89. @ApiIgnore
  90. @GetMapping("free1")
  91. private R free1(HttpServletRequest req){
  92. log.info("run free");
  93. String authorization = req.getHeader("Authorization");
  94. log.info("TOKEN: {}", authorization);
  95. // Subject subject = SecurityUtils.getSubject();
  96. // if (!subject.isAuthenticated()) {
  97. // log.info("没有登录, 游客模式………………");
  98. // return new R(200, "没有登录, 游客模式………………");
  99. // }
  100. log.info("已经登录, 登录模式………………");
  101. return new R(200, "已经登录, 登录模式………………");
  102. }
  103. /**
  104. * 只用登录用户才能访问
  105. */
  106. @ApiIgnore
  107. @GetMapping("/auth/a")
  108. @RequiresAuthentication
  109. public R requireAuth() {
  110. log.info("run requireAuth");
  111. userRepository.findAll();
  112. log.info("end requireAuth");
  113. return new R(200, "已经登录, 登录模式………………");
  114. }
  115. @ApiIgnore
  116. @GetMapping("/auth/k")
  117. @RequiresAuthentication
  118. public R list() {
  119. log.info("run test list");
  120. userRepository.findAll();
  121. log.info("end test list");
  122. return new R(200, "111");
  123. }
  124. /**
  125. * 需要登录
  126. * 只有admin角色可以看
  127. */
  128. @ApiIgnore
  129. @GetMapping("/auth/role")
  130. @RequiresRoles("admin")
  131. public R requireRole() {
  132. return new R(200, "只有admin 可以访问");
  133. }
  134. /**
  135. * 需要登录
  136. * 需要有view和edit 权限才可以看
  137. */
  138. @ApiIgnore
  139. @GetMapping("/auth/edit")
  140. @RequiresPermissions(logical = Logical.AND, value = {"view", "edit"})
  141. public R requirePermission() {
  142. return new R(200, "你拥有view、 edit的权限");
  143. }
  144. /**
  145. * 需要登录
  146. * 只要有view 权限都可以看
  147. */
  148. @ApiIgnore
  149. @GetMapping("/auth/view")
  150. @RequiresPermissions(logical = Logical.AND, value = {"view"})
  151. public R requirePermissionView() {
  152. return new R(200, "你拥有view的权限");
  153. }
  154. @ApiIgnore
  155. @RequestMapping(path = "/401")
  156. @ResponseStatus(HttpStatus.UNAUTHORIZED)
  157. public R unauthorized() {
  158. return new R(40001, "Unauthorized");
  159. }
  160. }