AuthController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springblade.modules.auth.controller;
  17. import com.wf.captcha.SpecCaptcha;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import io.swagger.annotations.ApiParam;
  21. import lombok.AllArgsConstructor;
  22. import org.springblade.common.cache.CacheNames;
  23. import org.springblade.core.secure.AuthInfo;
  24. import org.springblade.core.tool.api.R;
  25. import org.springblade.core.tool.support.Kv;
  26. import org.springblade.core.tool.utils.Func;
  27. import org.springblade.core.tool.utils.RedisUtil;
  28. import org.springblade.core.tool.utils.WebUtil;
  29. import org.springblade.modules.auth.granter.ITokenGranter;
  30. import org.springblade.modules.auth.granter.TokenGranterBuilder;
  31. import org.springblade.modules.auth.granter.TokenParameter;
  32. import org.springblade.modules.auth.utils.TokenUtil;
  33. import org.springblade.modules.system.entity.UserInfo;
  34. import org.springframework.web.bind.annotation.*;
  35. import java.util.UUID;
  36. import java.util.concurrent.TimeUnit;
  37. /**
  38. * 认证模块
  39. *
  40. * @author Chill
  41. */
  42. @RestController
  43. @AllArgsConstructor
  44. @RequestMapping("blade-auth")
  45. @Api(value = "用户授权认证", tags = "授权接口")
  46. public class AuthController {
  47. private RedisUtil redisUtil;
  48. @PostMapping("token")
  49. @ApiOperation(value = "获取认证token", notes = "传入租户ID:tenantId,账号:account,密码:password")
  50. public R<AuthInfo> token(@ApiParam(value = "授权类型", required = true) @RequestParam(defaultValue = "password", required = false) String grantType,
  51. @ApiParam(value = "刷新令牌") @RequestParam(required = false) String refreshToken,
  52. @ApiParam(value = "租户ID", required = true) @RequestParam(defaultValue = "000000", required = false) String tenantId,
  53. @ApiParam(value = "账号") @RequestParam(required = false) String account,
  54. @ApiParam(value = "密码") @RequestParam(required = false) String password) {
  55. String userType = Func.toStr(WebUtil.getRequest().getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE);
  56. TokenParameter tokenParameter = new TokenParameter();
  57. tokenParameter.getArgs().set("tenantId", tenantId)
  58. .set("account", account)
  59. .set("password", password)
  60. .set("grantType", grantType)
  61. .set("refreshToken", refreshToken)
  62. .set("userType", userType);
  63. ITokenGranter granter = TokenGranterBuilder.getGranter(grantType);
  64. UserInfo userInfo = granter.grant(tokenParameter);
  65. if (userInfo == null || userInfo.getUser() == null) {
  66. return R.fail(TokenUtil.USER_NOT_FOUND);
  67. }
  68. return R.data(TokenUtil.createAuthInfo(userInfo));
  69. }
  70. @GetMapping("/captcha")
  71. @ApiOperation(value = "获取验证码")
  72. public R<Kv> captcha() {
  73. SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
  74. String verCode = specCaptcha.text().toLowerCase();
  75. String key = UUID.randomUUID().toString();
  76. // 存入redis并设置过期时间为30分钟
  77. redisUtil.set(CacheNames.CAPTCHA_KEY + key, verCode, 30L, TimeUnit.MINUTES);
  78. // 将key和base64返回给前端
  79. return R.data(Kv.init().set("key", key).set("image", specCaptcha.toBase64()));
  80. }
  81. @PostMapping("/logout")
  82. @ApiOperation(value = "登出")
  83. public R<Kv> logout() {
  84. // 登出预留逻辑
  85. return R.data(Kv.init().set("code", "200").set("msg", "操作成功"));
  86. }
  87. }