<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.2</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.2</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId></dependency><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.72</version></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version></dependency><dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.4</version></dependency><dependency> <groupId>com.xkcoding.justauth</groupId> <artifactId>justauth-spring-boot-starter</artifactId> <version>1.4.0</version></dependency>
4.3.2 yamlserver: port: 8080spring: application: name: springboot-authjustauth: enabled: true type: GITEE: client-id: f491e924c9c7eec1e84d9d6f1cea455ff83ad25788c3d062eb22c185dcc6e403 client-secret: e442f80dd3aa418b7490dbf3ac7aedaf6bae6ae035e4a32b99bbcf181887e289 redirect-uri: http://localhost:8080/call/gitee #项目中的回调地址 cache: type: default
4.3.3 Controllerimport cn.hutool.json.JSONUtil;import com.xkcoding.justauth.AuthRequestFactory;import lombok.extern.slf4j.Slf4j;import me.zhyd.oauth.model.AuthCallback;import me.zhyd.oauth.model.AuthResponse;import me.zhyd.oauth.request.AuthRequest;import me.zhyd.oauth.utils.AuthStateUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Slf4j@RestControllerpublic class BladeSocialEndpoint { @Resource private AuthRequestFactory factory; @GetMapping("/login/{type}") // type:指的是我们要通过那个平台的授权进行登录,比如要用gitee public void login(@PathVariable String type, HttpServletResponse response) throws IOException { log.info("--------->{}", factory == null); AuthRequest authRequest = factory.get(type); response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } @GetMapping("/call/{type}") public AuthResponse login(@PathVariable String type, AuthCallback callback) { log.info("回调函数:{}",type); AuthRequest authRequest = factory.get(type); AuthResponse response = authRequest.login(callback); log.info("【response】= {}", JSONUtil.toJsonStr(response)); return response; }}
4.3.4测试 这个就很简单了,根据控制器测试即可……(图片来源网络,侵删)
0 评论