|
3 | 3 | import cn.hutool.core.lang.Dict; |
4 | 4 | import com.xkcoding.social.props.OAuthProperties; |
5 | 5 | import lombok.RequiredArgsConstructor; |
| 6 | +import me.zhyd.oauth.config.AuthConfig; |
| 7 | +import me.zhyd.oauth.config.AuthSource; |
| 8 | +import me.zhyd.oauth.model.AuthCallback; |
6 | 9 | import me.zhyd.oauth.model.AuthResponse; |
7 | | -import me.zhyd.oauth.model.AuthSource; |
8 | 10 | import me.zhyd.oauth.request.*; |
| 11 | +import me.zhyd.oauth.utils.AuthState; |
9 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
10 | 13 | import org.springframework.web.bind.annotation.GetMapping; |
11 | 14 | import org.springframework.web.bind.annotation.PathVariable; |
@@ -65,56 +68,72 @@ public void renderAuth(@PathVariable String oauthType, HttpServletResponse respo |
65 | 68 | * 登录成功后的回调 |
66 | 69 | * |
67 | 70 | * @param oauthType 第三方登录类型 |
68 | | - * @param code 携带的授权码 |
| 71 | + * @param callback 携带返回的信息 |
69 | 72 | * @return 登录成功后的信息 |
70 | 73 | */ |
71 | 74 | @RequestMapping("/{oauthType}/callback") |
72 | | - public AuthResponse login(@PathVariable String oauthType, String code) { |
| 75 | + public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) { |
73 | 76 | AuthRequest authRequest = getAuthRequest(oauthType); |
74 | | - return authRequest.login(code); |
| 77 | + AuthResponse response = authRequest.login(callback); |
| 78 | + // 移除校验通过的state |
| 79 | + AuthState.delete(oauthType); |
| 80 | + return response; |
75 | 81 | } |
76 | 82 |
|
77 | 83 | private AuthRequest getAuthRequest(String oauthType) { |
78 | 84 | AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); |
| 85 | + String state = AuthState.create(oauthType); |
79 | 86 | switch (authSource) { |
80 | 87 | case QQ: |
81 | | - return getQqAuthRequest(); |
| 88 | + return getQqAuthRequest(state); |
82 | 89 | case GITHUB: |
83 | | - return getGithubAuthRequest(); |
| 90 | + return getGithubAuthRequest(state); |
84 | 91 | case WECHAT: |
85 | | - return getWechatAuthRequest(); |
| 92 | + return getWechatAuthRequest(state); |
86 | 93 | case GOOGLE: |
87 | | - return getGoogleAuthRequest(); |
| 94 | + return getGoogleAuthRequest(state); |
88 | 95 | case MICROSOFT: |
89 | | - return getMicrosoftAuthRequest(); |
| 96 | + return getMicrosoftAuthRequest(state); |
90 | 97 | case MI: |
91 | | - return getMiAuthRequest(); |
| 98 | + return getMiAuthRequest(state); |
92 | 99 | default: |
93 | 100 | throw new RuntimeException("暂不支持的第三方登录"); |
94 | 101 | } |
95 | 102 | } |
96 | 103 |
|
97 | | - private AuthRequest getQqAuthRequest() { |
98 | | - return new AuthQqRequest(properties.getQq()); |
| 104 | + private AuthRequest getQqAuthRequest(String state) { |
| 105 | + AuthConfig authConfig = properties.getQq(); |
| 106 | + authConfig.setState(state); |
| 107 | + return new AuthQqRequest(authConfig); |
99 | 108 | } |
100 | 109 |
|
101 | | - private AuthRequest getGithubAuthRequest() { |
102 | | - return new AuthGithubRequest(properties.getGithub()); |
| 110 | + private AuthRequest getGithubAuthRequest(String state) { |
| 111 | + AuthConfig authConfig = properties.getGithub(); |
| 112 | + authConfig.setState(state); |
| 113 | + return new AuthGithubRequest(authConfig); |
103 | 114 | } |
104 | 115 |
|
105 | | - private AuthRequest getWechatAuthRequest() { |
106 | | - return new AuthWeChatRequest(properties.getWechat()); |
| 116 | + private AuthRequest getWechatAuthRequest(String state) { |
| 117 | + AuthConfig authConfig = properties.getWechat(); |
| 118 | + authConfig.setState(state); |
| 119 | + return new AuthWeChatRequest(authConfig); |
107 | 120 | } |
108 | 121 |
|
109 | | - private AuthRequest getGoogleAuthRequest() { |
110 | | - return new AuthGoogleRequest(properties.getGoogle()); |
| 122 | + private AuthRequest getGoogleAuthRequest(String state) { |
| 123 | + AuthConfig authConfig = properties.getGoogle(); |
| 124 | + authConfig.setState(state); |
| 125 | + return new AuthGoogleRequest(authConfig); |
111 | 126 | } |
112 | 127 |
|
113 | | - private AuthRequest getMicrosoftAuthRequest() { |
114 | | - return new AuthMicrosoftRequest(properties.getMicrosoft()); |
| 128 | + private AuthRequest getMicrosoftAuthRequest(String state) { |
| 129 | + AuthConfig authConfig = properties.getMicrosoft(); |
| 130 | + authConfig.setState(state); |
| 131 | + return new AuthMicrosoftRequest(authConfig); |
115 | 132 | } |
116 | 133 |
|
117 | | - private AuthRequest getMiAuthRequest() { |
118 | | - return new AuthMiRequest(properties.getMi()); |
| 134 | + private AuthRequest getMiAuthRequest(String state) { |
| 135 | + AuthConfig authConfig = properties.getMi(); |
| 136 | + authConfig.setState(state); |
| 137 | + return new AuthMiRequest(authConfig); |
119 | 138 | } |
120 | 139 | } |
0 commit comments