AuthController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.zhongzheng.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.zhongzheng.auth.granter.ITokenGranter;
  23. import org.zhongzheng.auth.granter.TokenGranterBuilder;
  24. import org.zhongzheng.auth.granter.TokenParameter;
  25. import org.zhongzheng.auth.utils.TokenUtil;
  26. import org.zhongzheng.auth.config.RedisFactoryConfig;
  27. import org.springframework.data.redis.core.RedisTemplate;
  28. import org.zhongzheng.common.utils.CacheNames;
  29. import org.zhongzheng.common.secure.AuthInfo;
  30. import org.zhongzheng.common.utils.R;
  31. import org.zhongzheng.common.utils.Kv;
  32. import org.zhongzheng.common.utils.Func;
  33. import org.zhongzheng.common.utils.RedisUtil;
  34. import org.zhongzheng.common.utils.WebUtil;
  35. import org.zhongzheng.common.feignclient.zzbusinessuser.entity.UserInfo;
  36. import org.springframework.web.bind.annotation.GetMapping;
  37. import org.springframework.web.bind.annotation.PostMapping;
  38. import org.springframework.web.bind.annotation.RequestParam;
  39. import org.springframework.web.bind.annotation.RestController;
  40. import javax.annotation.PostConstruct;
  41. import java.io.UnsupportedEncodingException;
  42. import java.util.UUID;
  43. import java.util.concurrent.TimeUnit;
  44. /**
  45. * 认证模块
  46. *
  47. * @author Chill
  48. */
  49. @RestController
  50. @AllArgsConstructor
  51. @Api(value = "用户授权认证", tags = "授权接口")
  52. public class AuthController {
  53. private RedisUtil redisUtil;
  54. private RedisFactoryConfig rdsFactoryConfig;
  55. @PostConstruct
  56. void afterConstruct(){
  57. System.out.println(" ++++++++++++++++++ AuthController: setConnectionFactory ");
  58. redisUtil.setConnectionFactory(rdsFactoryConfig.connectionFactory());
  59. }
  60. @PostMapping("token")
  61. @ApiOperation(value = "获取认证token", notes = "传入租户ID:tenantId,账号:account,密码:password")
  62. public R<AuthInfo> token(@ApiParam(value = "授权类型", required = true) @RequestParam(defaultValue = "password", required = false) String grantType,
  63. @ApiParam(value = "刷新令牌") @RequestParam(required = false) String refreshToken,
  64. @ApiParam(value = "租户ID", required = true) @RequestParam(defaultValue = "000000", required = false) String tenantId,
  65. @ApiParam(value = "账号") @RequestParam(required = false) String account,
  66. @ApiParam(value = "密码") @RequestParam(required = false) String password) throws UnsupportedEncodingException {
  67. String userType = Func.toStr(WebUtil.getRequest().getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE);
  68. TokenParameter tokenParameter = new TokenParameter();
  69. tokenParameter.getArgs().set("tenantId", tenantId)
  70. .set("account", account)
  71. .set("password", password)
  72. .set("grantType", grantType)
  73. .set("refreshToken", refreshToken)
  74. .set("userType", userType);
  75. ITokenGranter granter = TokenGranterBuilder.getGranter(grantType);
  76. UserInfo userInfo = granter.grant(tokenParameter);
  77. if (userInfo == null || userInfo.getUser() == null || userInfo.getUser().getId() == null) {
  78. return R.fail(TokenUtil.USER_NOT_FOUND);
  79. }
  80. return R.data(TokenUtil.createAuthInfo(userInfo));
  81. }
  82. @GetMapping("/captcha")
  83. @ApiOperation(value = "获取验证码")
  84. public R<Kv> captcha() {
  85. SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
  86. String verCode = specCaptcha.text().toLowerCase();
  87. String key = UUID.randomUUID().toString();
  88. // 存入redis并设置过期时间为30分钟
  89. redisUtil.set(CacheNames.CAPTCHA_KEY + key, verCode, 30L, TimeUnit.MINUTES);
  90. // 将key和base64返回给前端
  91. return R.data(Kv.init().set("key", key).set("image", specCaptcha.toBase64()));
  92. }
  93. }