提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.oauth2.controller; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
4 |
import cn.stylefeng.guns.oauth2.factory.OAuthRequestFactory; |
|
5 |
import cn.stylefeng.guns.oauth2.service.LoginService; |
|
6 |
import cn.stylefeng.guns.base.oauth2.service.OauthUserInfoService; |
|
7 |
import cn.stylefeng.roses.core.base.controller.BaseController; |
|
8 |
import lombok.extern.slf4j.Slf4j; |
|
9 |
import me.zhyd.oauth.model.AuthCallback; |
|
10 |
import me.zhyd.oauth.model.AuthResponse; |
|
11 |
import me.zhyd.oauth.model.AuthUser; |
|
12 |
import me.zhyd.oauth.request.AuthRequest; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.stereotype.Controller; |
|
15 |
import org.springframework.web.bind.annotation.PathVariable; |
|
16 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
17 |
|
|
18 |
import javax.servlet.http.HttpServletResponse; |
|
19 |
import java.io.IOException; |
|
20 |
|
|
21 |
/** |
|
22 |
* OAuth统一回调地址 |
|
23 |
* |
|
24 |
* @author fengshuonan |
|
25 |
* @Date 2019/6/9 16:38 |
|
26 |
*/ |
|
27 |
@Controller |
|
28 |
@RequestMapping("/oauth") |
|
29 |
@Slf4j |
|
30 |
public class OAuthController extends BaseController { |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private LoginService loginService; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private OauthUserInfoService oauthUserInfoService; |
|
37 |
|
|
38 |
/** |
|
39 |
* 第三方登录跳转 |
|
40 |
* |
|
41 |
* @author fengshuonan |
|
42 |
* @Date 2019/6/9 16:44 |
|
43 |
*/ |
|
44 |
@RequestMapping("/render/{source}") |
|
45 |
public void renderAuth(@PathVariable("source") String source, HttpServletResponse response) throws IOException { |
|
46 |
AuthRequest authRequest = OAuthRequestFactory.getAuthRequest(source); |
|
47 |
response.sendRedirect(authRequest.authorize()); |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* 第三方登录成功后的回调地址 |
|
52 |
* |
|
53 |
* @author fengshuonan |
|
54 |
* @Date 2019/6/9 16:45 |
|
55 |
*/ |
|
56 |
@RequestMapping("/callback/{source}") |
|
57 |
public String callback(@PathVariable("source") String source, AuthCallback callback) { |
|
58 |
|
|
59 |
//通过回调的code,请求对应的oauth server获取用户基本信息和token |
|
60 |
AuthRequest authRequest = OAuthRequestFactory.getAuthRequest(source); |
|
61 |
AuthResponse authResponse = authRequest.login(callback); |
|
62 |
|
|
63 |
AuthUser oauthUser = (AuthUser) authResponse.getData(); |
|
64 |
log.info("第三方登录回调成功:" + oauthUser); |
|
65 |
|
|
66 |
//进行第三方用户登录过程 |
|
67 |
String token = loginService.oauthLogin(oauthUser); |
|
68 |
|
|
69 |
//跳转到token登录接口 |
|
70 |
return REDIRECT + "/sysTokenLogin?token=" + token; |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* 第三方登录的头像 |
|
75 |
* |
|
76 |
* @author fengshuonan |
|
77 |
* @Date 2019/6/9 16:44 |
|
78 |
*/ |
|
79 |
@RequestMapping("/avatar") |
|
80 |
public String avatar() { |
|
81 |
|
|
82 |
Long userId = LoginContextHolder.getContext().getUser().getId(); |
|
83 |
|
|
84 |
//获取第三方登录用户的头像 |
|
85 |
String avatarUrl = oauthUserInfoService.getAvatarUrl(userId); |
|
86 |
|
|
87 |
return REDIRECT + avatarUrl; |
|
88 |
} |
|
89 |
|
|
90 |
} |