1
0
mirror of synced 2026-02-07 17:57:47 +08:00
Yangkai.Shen e6876c2902 🔖 releasing 0.0.1
2019-07-23 13:51:15 +08:00
2019-07-22 15:23:07 +08:00
2019-07-23 13:51:15 +08:00
2019-07-23 11:34:53 +08:00

justauth-spring-boot-starter

Spring Boot 集成 JustAuth 的最佳实践~

JustAuth 脚手架

Demo

懒得看文档的可以直接看demo

https://github.com/xkcoding/justauth-spring-boot-starter-demo

快速开始

  • 引用依赖
<dependency>
  <groupId>com.xkcoding</groupId>
  <artifactId>justauth-spring-boot-starter</artifactId>
  <version>0.0.1</version>
</dependency>
  • 添加配置,在 application.yml 中添加配置配置信息
justauth:
  enabled: true
  type:
    QQ:
      client-id: 10**********6
      client-secret: 1f7d08**********5b7**********29e
      redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
  • 然后就开始玩耍吧~
package com.xkcoding.justauthspringbootstarterdemo;

import com.xkcoding.justauth.AuthRequestFactory;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 测试 Controller
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2019-07-22 11:17
 */
@RestController
@RequestMapping("/oauth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TestController {
    private final AuthRequestFactory factory;

    @GetMapping("/login/qq")
    public void login(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = factory.get(AuthSource.QQ);
        response.sendRedirect(authRequest.authorize());
    }

    @RequestMapping("/qq/callback")
    public AuthResponse login(AuthCallback callback) {
        AuthRequest authRequest = factory.get(AuthSource.QQ);
        AuthResponse response = authRequest.login(callback);
        // 移除校验通过的state
        AuthState.delete(AuthSource.QQ);
        return response;
    }
}

附录

justauth 配置列表

属性名 类型 默认值 可选项 描述
justauth.enabled boolean true true/false 是否启用 JustAuth
justauth.type java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig> JustAuth 配置

justauth.type 配置列表

属性名 描述
justauth.type.keys justauth.typeMap 格式的key 的取值请参考 AuthSource
justauth.type.keys.values justauth.typeMap 格式的value 的取值请参考 AuthConfig
Description
Spring Boot 集成 JustAuth 的最佳实践~
Readme 200 KiB
Languages
Java 100%