AuthController.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.auth;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import io.swagger.annotations.ApiParam;
  21. import io.swagger.annotations.ApiSort;
  22. import lombok.AllArgsConstructor;
  23. import org.springblade.core.log.annotation.ApiLog;
  24. import org.springblade.core.secure.AuthInfo;
  25. import org.springblade.core.secure.utils.SecureUtil;
  26. import org.springblade.core.tool.api.R;
  27. import org.springblade.core.tool.utils.DigestUtil;
  28. import org.springblade.core.tool.utils.Func;
  29. import org.springblade.modules.system.entity.User;
  30. import org.springblade.modules.system.entity.UserInfo;
  31. import org.springblade.modules.system.service.IUserService;
  32. import org.springframework.web.bind.annotation.PostMapping;
  33. import org.springframework.web.bind.annotation.RequestMapping;
  34. import org.springframework.web.bind.annotation.RequestParam;
  35. import org.springframework.web.bind.annotation.RestController;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. /**
  39. * 认证模块
  40. *
  41. * @author Chill
  42. */
  43. @RestController
  44. @AllArgsConstructor
  45. @RequestMapping("blade-auth")
  46. @ApiSort(1)
  47. @Api(value = "用户授权认证", tags = "授权接口")
  48. public class AuthController {
  49. IUserService service;
  50. @ApiLog("登录用户验证")
  51. @PostMapping("token")
  52. @ApiOperation(value = "获取认证token", notes = "传入账号:account,密码:password")
  53. public R<AuthInfo> token(@ApiParam(value = "账号", required = true) @RequestParam String account,
  54. @ApiParam(value = "密码", required = true) @RequestParam String password) {
  55. if (Func.hasEmpty(account, password)) {
  56. return R.fail("接口调用不合法");
  57. }
  58. UserInfo userInfo = service.userInfo(account, DigestUtil.encrypt(password));
  59. User user = userInfo.getUser();
  60. //验证用户
  61. if (user == null) {
  62. return R.fail("用户名或密码不正确");
  63. }
  64. //设置jwt参数
  65. Map<String, String> param = new HashMap<>(16);
  66. param.put(SecureUtil.USER_ID, Func.toStr(user.getId()));
  67. param.put(SecureUtil.ROLE_ID, user.getRoleId());
  68. param.put(SecureUtil.ACCOUNT, user.getAccount());
  69. param.put(SecureUtil.USER_NAME, user.getRealName());
  70. param.put(SecureUtil.ROLE_NAME, Func.join(userInfo.getRoles()));
  71. //拼装accessToken
  72. String accessToken = SecureUtil.createJWT(param, "audience", "issuser", true);
  73. //返回accessToken
  74. AuthInfo authInfo = new AuthInfo();
  75. authInfo.setAccount(user.getAccount());
  76. authInfo.setUserName(user.getRealName());
  77. authInfo.setAuthority(Func.join(userInfo.getRoles()));
  78. authInfo.setAccessToken(accessToken);
  79. authInfo.setTokenType(SecureUtil.BEARER);
  80. //设置token过期时间
  81. authInfo.setExpiresIn(SecureUtil.getExpire());
  82. return R.data(authInfo);
  83. }
  84. }