SocialLoginController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package cc.mrbird.febs.auth.controller;
  2. import cc.mrbird.febs.auth.entity.BindUser;
  3. import cc.mrbird.febs.auth.entity.UserConnection;
  4. import cc.mrbird.febs.auth.service.SocialLoginService;
  5. import cc.mrbird.febs.common.core.entity.FebsResponse;
  6. import cc.mrbird.febs.common.core.exception.FebsException;
  7. import cc.mrbird.febs.common.core.utils.FebsUtil;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import me.zhyd.oauth.model.AuthCallback;
  11. import me.zhyd.oauth.model.AuthUser;
  12. import me.zhyd.oauth.request.AuthRequest;
  13. import me.zhyd.oauth.utils.AuthStateUtils;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.security.oauth2.common.OAuth2AccessToken;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.ui.Model;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.validation.Valid;
  22. import javax.validation.constraints.NotBlank;
  23. import java.io.IOException;
  24. import java.util.List;
  25. /**
  26. * @author MrBird
  27. */
  28. @Slf4j
  29. @Controller
  30. @RequiredArgsConstructor
  31. @RequestMapping("social")
  32. public class SocialLoginController {
  33. private static final String TYPE_LOGIN = "login";
  34. private static final String TYPE_BIND = "bind";
  35. private final SocialLoginService socialLoginService;
  36. @Value("${febs.frontUrl}")
  37. private String frontUrl;
  38. /**
  39. * 登录
  40. *
  41. * @param oauthType 第三方登录类型
  42. * @param response response
  43. */
  44. @ResponseBody
  45. @GetMapping("/login/{oauthType}/{type}")
  46. public void renderAuth(@PathVariable String oauthType, @PathVariable String type, HttpServletResponse response) throws IOException, FebsException {
  47. AuthRequest authRequest = socialLoginService.renderAuth(oauthType);
  48. response.sendRedirect(authRequest.authorize(oauthType + "::" + AuthStateUtils.createState()) + "::" + type);
  49. }
  50. /**
  51. * 登录成功后的回调
  52. *
  53. * @param oauthType 第三方登录类型
  54. * @param callback 携带返回的信息
  55. * @return String
  56. */
  57. @GetMapping("/{oauthType}/callback")
  58. public String login(@PathVariable String oauthType, AuthCallback callback, String state, Model model) {
  59. try {
  60. FebsResponse febsResponse = null;
  61. String type = StringUtils.substringAfterLast(state, "::");
  62. if (StringUtils.equals(type, TYPE_BIND)) {
  63. febsResponse = socialLoginService.resolveBind(oauthType, callback);
  64. } else {
  65. febsResponse = socialLoginService.resolveLogin(oauthType, callback);
  66. }
  67. model.addAttribute("response", febsResponse);
  68. model.addAttribute("frontUrl", frontUrl);
  69. return "result";
  70. } catch (Exception e) {
  71. String errorMessage = FebsUtil.containChinese(e.getMessage()) ? e.getMessage() : "第三方登录失败";
  72. model.addAttribute("error", e.getMessage());
  73. return "fail";
  74. }
  75. }
  76. /**
  77. * 绑定并登录
  78. *
  79. * @param bindUser bindUser
  80. * @param authUser authUser
  81. * @return FebsResponse
  82. */
  83. @ResponseBody
  84. @PostMapping("bind/login")
  85. public FebsResponse bindLogin(@Valid BindUser bindUser, AuthUser authUser) throws FebsException {
  86. OAuth2AccessToken oAuth2AccessToken = this.socialLoginService.bindLogin(bindUser, authUser);
  87. return new FebsResponse().data(oAuth2AccessToken);
  88. }
  89. /**
  90. * 注册并登录
  91. *
  92. * @param registUser registUser
  93. * @param authUser authUser
  94. * @return FebsResponse
  95. */
  96. @ResponseBody
  97. @PostMapping("sign/login")
  98. public FebsResponse signLogin(@Valid BindUser registUser, AuthUser authUser) throws FebsException {
  99. OAuth2AccessToken oAuth2AccessToken = this.socialLoginService.signLogin(registUser, authUser);
  100. return new FebsResponse().data(oAuth2AccessToken);
  101. }
  102. /**
  103. * 绑定
  104. *
  105. * @param bindUser bindUser
  106. * @param authUser authUser
  107. */
  108. @ResponseBody
  109. @PostMapping("bind")
  110. public void bind(BindUser bindUser, AuthUser authUser) throws FebsException {
  111. this.socialLoginService.bind(bindUser, authUser);
  112. }
  113. /**
  114. * 解绑
  115. *
  116. * @param bindUser bindUser
  117. * @param oauthType oauthType
  118. */
  119. @ResponseBody
  120. @DeleteMapping("unbind")
  121. public void unbind(BindUser bindUser, String oauthType) throws FebsException {
  122. this.socialLoginService.unbind(bindUser, oauthType);
  123. }
  124. /**
  125. * 根据用户名获取绑定关系
  126. *
  127. * @param username 用户名
  128. * @return FebsResponse
  129. */
  130. @ResponseBody
  131. @GetMapping("connections/{username}")
  132. public FebsResponse findUserConnections(@NotBlank(message = "{required}") @PathVariable String username) {
  133. List<UserConnection> userConnections = this.socialLoginService.findUserConnections(username);
  134. return new FebsResponse().data(userConnections);
  135. }
  136. }