AuthController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package org.dromara.web.controller;
  2. import cn.dev33.satoken.annotation.SaIgnore;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import me.zhyd.oauth.model.AuthResponse;
  9. import me.zhyd.oauth.model.AuthUser;
  10. import me.zhyd.oauth.request.AuthRequest;
  11. import me.zhyd.oauth.utils.AuthStateUtils;
  12. import org.dromara.common.core.domain.R;
  13. import org.dromara.common.core.domain.model.LoginBody;
  14. import org.dromara.common.core.domain.model.RegisterBody;
  15. import org.dromara.common.core.utils.MapstructUtils;
  16. import org.dromara.common.core.utils.MessageUtils;
  17. import org.dromara.common.core.utils.StreamUtils;
  18. import org.dromara.common.core.utils.StringUtils;
  19. import org.dromara.common.social.config.properties.SocialLoginConfigProperties;
  20. import org.dromara.common.social.config.properties.SocialProperties;
  21. import org.dromara.common.social.utils.SocialUtils;
  22. import org.dromara.common.tenant.helper.TenantHelper;
  23. import org.dromara.system.domain.SysClient;
  24. import org.dromara.system.domain.bo.SysTenantBo;
  25. import org.dromara.system.domain.vo.SysTenantVo;
  26. import org.dromara.system.service.ISysClientService;
  27. import org.dromara.system.service.ISysConfigService;
  28. import org.dromara.system.service.ISysSocialService;
  29. import org.dromara.system.service.ISysTenantService;
  30. import org.dromara.web.domain.vo.LoginTenantVo;
  31. import org.dromara.web.domain.vo.LoginVo;
  32. import org.dromara.web.domain.vo.TenantListVo;
  33. import org.dromara.web.service.IAuthStrategy;
  34. import org.dromara.web.service.SysLoginService;
  35. import org.dromara.web.service.SysRegisterService;
  36. import org.springframework.validation.annotation.Validated;
  37. import org.springframework.web.bind.annotation.*;
  38. import java.net.URL;
  39. import java.util.List;
  40. /**
  41. * 认证
  42. *
  43. * @author Lion Li
  44. */
  45. @Slf4j
  46. @SaIgnore
  47. @Validated
  48. @RequiredArgsConstructor
  49. @RestController
  50. @RequestMapping("/auth")
  51. public class AuthController {
  52. private final SocialProperties socialProperties;
  53. private final SysLoginService loginService;
  54. private final SysRegisterService registerService;
  55. private final ISysConfigService configService;
  56. private final ISysTenantService tenantService;
  57. private final ISysSocialService socialUserService;
  58. private final ISysClientService clientService;
  59. /**
  60. * 登录方法
  61. *
  62. * @param loginBody 登录信息
  63. * @return 结果
  64. */
  65. @PostMapping("/login")
  66. public R<LoginVo> login(@Validated @RequestBody LoginBody loginBody) {
  67. // 授权类型和客户端id
  68. String clientId = loginBody.getClientId();
  69. String grantType = loginBody.getGrantType();
  70. SysClient client = clientService.queryByClientId(clientId);
  71. // 查询不到 client 或 client 内不包含 grantType
  72. if (ObjectUtil.isNull(client) || !StringUtils.contains(client.getGrantType(), grantType)) {
  73. log.info("客户端id: {} 认证类型:{} 异常!.", clientId, grantType);
  74. return R.fail(MessageUtils.message("auth.grant.type.error"));
  75. }
  76. // 校验租户
  77. loginService.checkTenant(loginBody.getTenantId());
  78. // 登录
  79. return R.ok(IAuthStrategy.login(loginBody, client));
  80. }
  81. /**
  82. * 第三方登录请求
  83. *
  84. * @param source 登录来源
  85. * @return 结果
  86. */
  87. @GetMapping("/binding/{source}")
  88. public R<String> authBinding(@PathVariable("source") String source) {
  89. SocialLoginConfigProperties obj = socialProperties.getType().get(source);
  90. if (ObjectUtil.isNull(obj)) {
  91. return R.fail(source + "平台账号暂不支持");
  92. }
  93. AuthRequest authRequest = SocialUtils.getAuthRequest(source, socialProperties);
  94. String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
  95. return R.ok("操作成功", authorizeUrl);
  96. }
  97. /**
  98. * 第三方登录回调业务处理 绑定授权
  99. *
  100. * @param loginBody 请求体
  101. * @return 结果
  102. */
  103. @PostMapping("/social/callback")
  104. public R<Void> socialCallback(@RequestBody LoginBody loginBody) {
  105. // 获取第三方登录信息
  106. AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody, socialProperties);
  107. AuthUser authUserData = response.getData();
  108. // 判断授权响应是否成功
  109. if (!response.ok()) {
  110. return R.fail(response.getMsg());
  111. }
  112. loginService.socialRegister(authUserData);
  113. return R.ok();
  114. }
  115. /**
  116. * 取消授权
  117. *
  118. * @param socialId socialId
  119. */
  120. @DeleteMapping(value = "/unlock/{socialId}")
  121. public R<Void> unlockSocial(@PathVariable Long socialId) {
  122. Boolean rows = socialUserService.deleteWithValidById(socialId);
  123. return rows ? R.ok() : R.fail("取消授权失败");
  124. }
  125. /**
  126. * 退出登录
  127. */
  128. @PostMapping("/logout")
  129. public R<Void> logout() {
  130. loginService.logout();
  131. return R.ok("退出成功");
  132. }
  133. /**
  134. * 用户注册
  135. */
  136. @PostMapping("/register")
  137. public R<Void> register(@Validated @RequestBody RegisterBody user) {
  138. if (!configService.selectRegisterEnabled(user.getTenantId())) {
  139. return R.fail("当前系统没有开启注册功能!");
  140. }
  141. registerService.register(user);
  142. return R.ok();
  143. }
  144. /**
  145. * 登录页面租户下拉框
  146. *
  147. * @return 租户列表
  148. */
  149. @GetMapping("/tenant/list")
  150. public R<LoginTenantVo> tenantList(HttpServletRequest request) throws Exception {
  151. List<SysTenantVo> tenantList = tenantService.queryList(new SysTenantBo());
  152. List<TenantListVo> voList = MapstructUtils.convert(tenantList, TenantListVo.class);
  153. // 获取域名
  154. String host;
  155. String referer = request.getHeader("referer");
  156. if (StringUtils.isNotBlank(referer)) {
  157. // 这里从referer中取值是为了本地使用hosts添加虚拟域名,方便本地环境调试
  158. host = referer.split("//")[1].split("/")[0];
  159. } else {
  160. host = new URL(request.getRequestURL().toString()).getHost();
  161. }
  162. // 根据域名进行筛选
  163. List<TenantListVo> list = StreamUtils.filter(voList, vo ->
  164. StringUtils.equals(vo.getDomain(), host));
  165. // 返回对象
  166. LoginTenantVo vo = new LoginTenantVo();
  167. vo.setVoList(CollUtil.isNotEmpty(list) ? list : voList);
  168. vo.setTenantEnabled(TenantHelper.isEnable());
  169. return R.ok(vo);
  170. }
  171. }