AuthController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.qxgmat.controller.admin;
  2. import com.nuliji.tools.Response;
  3. import com.nuliji.tools.ResponseHelp;
  4. import com.nuliji.tools.Transform;
  5. import com.nuliji.tools.exception.AuthException;
  6. import com.qxgmat.data.dao.entity.Manager;
  7. import com.qxgmat.data.relation.entity.ManagerRelation;
  8. import com.qxgmat.dto.admin.request.LoginDto;
  9. import com.qxgmat.dto.admin.response.LoginUserDto;
  10. import com.qxgmat.help.CaptchaHelp;
  11. import com.qxgmat.help.ShiroHelp;
  12. import com.qxgmat.service.inline.ManagerRoleService;
  13. import com.qxgmat.service.ManagerService;
  14. import com.qxgmat.service.inline.ManagerLogService;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.http.MediaType;
  19. import org.springframework.validation.annotation.Validated;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestMethod;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpSession;
  26. /**
  27. * Created by GaoJie on 2017/11/3.
  28. */
  29. @RestController("AdminAuthController")
  30. @RequestMapping("/admin/auth")
  31. @Api(tags = "验证接口", produces = MediaType.APPLICATION_JSON_VALUE)
  32. public class AuthController {
  33. @Autowired
  34. private CaptchaHelp captchaHelp;
  35. @Autowired
  36. private ManagerService managerService;
  37. @Autowired
  38. private ManagerRoleService managerRoleService;
  39. @Autowired
  40. private ShiroHelp shiroHelp;
  41. @Autowired
  42. private ManagerLogService managerLogService;
  43. @RequestMapping(value = "/token", method = RequestMethod.POST)
  44. @ApiOperation(value = "验证token", httpMethod = "POST")
  45. public Response<LoginUserDto> token(HttpSession session, HttpServletRequest request) {
  46. Manager manager = shiroHelp.getLoginManager();
  47. if (manager == null) {
  48. throw new AuthException("未登录");
  49. }
  50. ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
  51. managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
  52. managerLogService.log(request);
  53. return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
  54. }
  55. @RequestMapping(value = "/login", method = RequestMethod.POST)
  56. @ApiOperation(value = "登录", httpMethod = "POST")
  57. public Response<LoginUserDto> login(@RequestBody @Validated LoginDto loginDto, HttpSession session, HttpServletRequest request) {
  58. shiroHelp.getSession().login(shiroHelp.manager(loginDto.getUsername(), loginDto.getPassword(), loginDto.getRemember()));
  59. Manager manager = shiroHelp.getLoginManager();
  60. ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
  61. managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
  62. managerLogService.log(request);
  63. return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
  64. }
  65. @RequestMapping(value = "/logout", method = RequestMethod.POST)
  66. @ApiOperation(value = "登出", httpMethod = "POST")
  67. public Response<Boolean> logout(HttpSession session, HttpServletRequest request) {
  68. shiroHelp.logout();
  69. return ResponseHelp.success(true);
  70. }
  71. @RequestMapping(value = "/refresh", method = RequestMethod.POST)
  72. @ApiOperation(value = "刷新", httpMethod = "POST")
  73. public Response<LoginUserDto> refresh(HttpSession session) {
  74. shiroHelp.refresh();
  75. Manager manager = (Manager)shiroHelp.getLoginManager();
  76. if(manager == null) return ResponseHelp.success(new LoginUserDto());
  77. ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
  78. managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
  79. return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
  80. }
  81. }