@@ -225,23 +225,283 @@ $ nginx -s reload
225
225
226
226
### 2.1. pom.xml
227
227
228
-
228
+ ``` xml
229
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
230
+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
231
+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
232
+ <modelVersion >4.0.0</modelVersion >
233
+
234
+ <artifactId >spring-boot-demo-social</artifactId >
235
+ <version >1.0.0-SNAPSHOT</version >
236
+ <packaging >jar</packaging >
237
+
238
+ <name >spring-boot-demo-social</name >
239
+ <description >Demo project for Spring Boot</description >
240
+
241
+ <parent >
242
+ <groupId >com.xkcoding</groupId >
243
+ <artifactId >spring-boot-demo</artifactId >
244
+ <version >1.0.0-SNAPSHOT</version >
245
+ </parent >
246
+
247
+ <properties >
248
+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
249
+ <project .reporting.outputEncoding>UTF-8</project .reporting.outputEncoding>
250
+ <java .version>1.8</java .version>
251
+ <spring .social.version>1.1.6.RELEASE</spring .social.version>
252
+ </properties >
253
+
254
+ <dependencies >
255
+ <dependency >
256
+ <groupId >org.springframework.boot</groupId >
257
+ <artifactId >spring-boot-starter-web</artifactId >
258
+ </dependency >
259
+
260
+ <dependency >
261
+ <groupId >org.springframework.boot</groupId >
262
+ <artifactId >spring-boot-starter-test</artifactId >
263
+ <scope >test</scope >
264
+ </dependency >
265
+
266
+ <!-- oauth工具类 -->
267
+ <dependency >
268
+ <groupId >me.zhyd.oauth</groupId >
269
+ <artifactId >JustAuth</artifactId >
270
+ <version >1.6.0-beta</version >
271
+ </dependency >
272
+
273
+ <dependency >
274
+ <groupId >org.projectlombok</groupId >
275
+ <artifactId >lombok</artifactId >
276
+ <optional >true</optional >
277
+ </dependency >
278
+
279
+ <dependency >
280
+ <groupId >com.google.guava</groupId >
281
+ <artifactId >guava</artifactId >
282
+ </dependency >
283
+
284
+ <dependency >
285
+ <groupId >cn.hutool</groupId >
286
+ <artifactId >hutool-all</artifactId >
287
+ </dependency >
288
+ </dependencies >
289
+
290
+ <build >
291
+ <finalName >spring-boot-demo-social</finalName >
292
+ <plugins >
293
+ <plugin >
294
+ <groupId >org.springframework.boot</groupId >
295
+ <artifactId >spring-boot-maven-plugin</artifactId >
296
+ </plugin >
297
+ </plugins >
298
+ </build >
299
+
300
+ </project >
301
+ ```
229
302
230
303
### 2.2. application.yml
231
304
232
-
305
+ ``` yaml
306
+ server :
307
+ port : 8080
308
+ servlet :
309
+ context-path : /demo
310
+
311
+ oauth :
312
+ qq :
313
+ client-id : 1015*****
314
+ client-secret : 1f7d08df55766**************
315
+ redirect-uri : http://oauth.xkcoding.com/demo/oauth/qq/callback
316
+ github :
317
+ client-id : 2d25a70**************
318
+ client-secret : 5a2919b5fe911567343**************
319
+ redirect-uri : http://oauth.xkcoding.com/demo/oauth/github/callback
320
+ wechat :
321
+ client-id : wxdcb31**************
322
+ client-secret : b4e9dc6841ef7d**************
323
+ redirect-uri : http://oauth.xkcoding.com/demo/oauth/wechat/callback
324
+ google :
325
+ client-id : 716518501517-6dbdkapivhia806vqcjjh9nttj3**************
326
+ client-secret : 9IBornd7w1A**************
327
+ redirect-uri : http://oauth.xkcoding.com/demo/oauth/google/callback
328
+ microsoft :
329
+ client-id : 7bdce818-2c8e-4b**************
330
+ client-secret : Iu0zZ43RQydo_FkD**************
331
+ redirect-uri : https://oauth.xkcoding.com/demo/oauth/microsoft/callback
332
+ mi :
333
+ client-id : 2882303**************
334
+ client-secret : nFeTt89Yn**************
335
+ redirect-uri : http://oauth.xkcoding.com/demo/oauth/mi/callback
336
+ ` ` `
233
337
234
338
### 2.3. OAuthProperties.java
235
339
236
-
340
+ ` ` ` java
341
+ /**
342
+ * <p>
343
+ * 第三方登录配置
344
+ * </p>
345
+ *
346
+ * @package: com.xkcoding.oauth.config.props
347
+ * @description: 第三方登录配置
348
+ * @author: yangkai.shen
349
+ * @date: Created in 2019-05-17 15:33
350
+ * @copyright: Copyright (c) 2019
351
+ * @version: V1.0
352
+ * @modified: yangkai.shen
353
+ */
354
+ @Data
355
+ @Component
356
+ @ConfigurationProperties(prefix = "oauth")
357
+ public class OAuthProperties {
358
+ /**
359
+ * QQ 配置
360
+ */
361
+ private AuthConfig qq;
362
+
363
+ /**
364
+ * github 配置
365
+ */
366
+ private AuthConfig github;
367
+
368
+ /**
369
+ * 微信 配置
370
+ */
371
+ private AuthConfig wechat;
372
+
373
+ /**
374
+ * Google 配置
375
+ */
376
+ private AuthConfig google;
377
+
378
+ /**
379
+ * Microsoft 配置
380
+ */
381
+ private AuthConfig microsoft;
382
+
383
+ /**
384
+ * Mi 配置
385
+ */
386
+ private AuthConfig mi;
387
+ }
388
+ ```
237
389
238
390
### 2.4. OauthController.java
239
391
392
+ ``` java
393
+ /**
394
+ * <p >
395
+ * 第三方登录 Controller
396
+ * </p>
397
+ *
398
+ * @package: com.xkcoding.oauth.controller
399
+ * @description: 第三方登录 Controller
400
+ * @author : yangkai.shen
401
+ * @date: Created in 2019-05-17 10:07
402
+ * @copyright: Copyright (c) 2019
403
+ * @version : V1.0
404
+ * @modified: yangkai.shen
405
+ */
406
+ @RestController
407
+ @RequestMapping (" /oauth" )
408
+ @RequiredArgsConstructor (onConstructor_ = @Autowired )
409
+ public class OauthController {
410
+ private final OAuthProperties properties;
411
+
412
+ /**
413
+ * 登录类型
414
+ */
415
+ @GetMapping
416
+ public Dict loginType () {
417
+ return Dict . create()
418
+ .set(" QQ登录" , " http://oauth.xkcoding.com/demo/oauth/login/qq" )
419
+ .set(" GitHub登录" , " http://oauth.xkcoding.com/demo/oauth/login/github" )
420
+ .set(" 微信登录" , " http://oauth.xkcoding.com/demo/oauth/login/wechat" )
421
+ .set(" Google登录" , " http://oauth.xkcoding.com/demo/oauth/login/google" )
422
+ .set(" Microsoft 登录" , " http://oauth.xkcoding.com/demo/oauth/login/microsoft" )
423
+ .set(" 小米登录" , " http://oauth.xkcoding.com/demo/oauth/login/mi" );
424
+ }
425
+
426
+ /**
427
+ * 登录
428
+ *
429
+ * @param oauthType 第三方登录类型
430
+ * @param response response
431
+ * @throws IOException
432
+ */
433
+ @RequestMapping (" /login/{oauthType}" )
434
+ public void renderAuth (@PathVariable String oauthType , HttpServletResponse response ) throws IOException {
435
+ AuthRequest authRequest = getAuthRequest(oauthType);
436
+ response. sendRedirect(authRequest. authorize());
437
+ }
438
+
439
+ /**
440
+ * 登录成功后的回调
441
+ *
442
+ * @param oauthType 第三方登录类型
443
+ * @param code 携带的授权码
444
+ * @return 登录成功后的信息
445
+ */
446
+ @RequestMapping (" /{oauthType}/callback" )
447
+ public AuthResponse login (@PathVariable String oauthType , String code ) {
448
+ AuthRequest authRequest = getAuthRequest(oauthType);
449
+ return authRequest. login(code);
450
+ }
451
+
452
+ private AuthRequest getAuthRequest (String oauthType ) {
453
+ AuthSource authSource = AuthSource . valueOf(oauthType. toUpperCase());
454
+ switch (authSource) {
455
+ case QQ :
456
+ return getQqAuthRequest();
457
+ case GITHUB :
458
+ return getGithubAuthRequest();
459
+ case WECHAT :
460
+ return getWechatAuthRequest();
461
+ case GOOGLE :
462
+ return getGoogleAuthRequest();
463
+ case MICROSOFT :
464
+ return getMicrosoftAuthRequest();
465
+ case MI :
466
+ return getMiAuthRequest();
467
+ default :
468
+ throw new RuntimeException (" 暂不支持的第三方登录" );
469
+ }
470
+ }
471
+
472
+ private AuthRequest getQqAuthRequest () {
473
+ return new AuthQqRequest (properties. getQq());
474
+ }
475
+
476
+ private AuthRequest getGithubAuthRequest () {
477
+ return new AuthGithubRequest (properties. getGithub());
478
+ }
479
+
480
+ private AuthRequest getWechatAuthRequest () {
481
+ return new AuthWeChatRequest (properties. getWechat());
482
+ }
483
+
484
+ private AuthRequest getGoogleAuthRequest () {
485
+ return new AuthGoogleRequest (properties. getGoogle());
486
+ }
240
487
488
+ private AuthRequest getMicrosoftAuthRequest () {
489
+ return new AuthMicrosoftRequest (properties. getMicrosoft());
490
+ }
491
+
492
+ private AuthRequest getMiAuthRequest () {
493
+ return new AuthMiRequest (properties. getMi());
494
+ }
495
+ }
496
+ ```
241
497
242
498
## 3. 运行方式
243
499
500
+ 打开浏览器,输入 http://oauth.xkcoding.com/demo/oauth ,点击各个登录方式自行测试。
501
+
502
+ > ` Google 登录,有可能因为祖国的强大导致测试失败,自行解决~ ` :kissing_smiling_eyes :
244
503
504
+ ![ image-20190617154343815] ( assets/image-20190617154343815.png )
245
505
246
506
## 参考
247
507
0 commit comments