OAuth2 JustAuth实战

tech2022-09-27  102

1.创建第三方平台应用,申请配置信息

基本上按照这篇博客的教程完成。

以github为例,在developer settings–>OAuth App上申请第三方应用,获得配置信息,以下是注册好应用后的浏览信息页面:

2.加入JustAuth工具类

2.1创建spring boot项目,加入pring-boot-starter-web和lombok依赖

2.2加入JustAuth依赖

// ... <dependency> <groupId>me.zhyd.oauth</groupId> <artifactId>JustAuth</artifactId> <version>1.15.1</version> </dependency> // ...

最终获得pom.xml如下

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>me.zhyd.oauth</groupId> <artifactId>JustAuth</artifactId> <version>1.15.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

3.简洁的三部曲

添加一个Controller类,用于处理页面和后端之间的请求,

3.1 申请授权页面

由于集成的是github,所以请求链接的动态参数{source}传入的是github,所以我们会在 http://localhost:8080/oauth/render/github上请求授权。

@RequestMapping("/render/{source}") public void renderAuth(HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(); String authorizeUrl = authRequest.authorize(AuthStateUtils.createState()); response.sendRedirect(authorizeUrl); }

3.2 获取AuthRequest,生成授权url

因为是向Github请求授权,所以直接实例化AuthGithubRequest( ),创建授权申请时填入之前在Github上获得的客户端Id和Secret,以及授权完毕后的跳转url。

3.3认证登录

在授权页面同意授权后,会跳转到redirect_url也就是http://localhost:8080/oauth/callback/github,并携带code、state等参数,callback是页面request的入参。

@RequestMapping("/callback/{resource}") public Object login(AuthCallback callback) { AuthRequest authRequest = getAuthRequest(); return authRequest.login(callback); }

回调时会报告Http实现类未指定,这个在之前的教程中也给出了原因和解决方案,加入依赖即可。 会返回如图页面,这表示github授权成功啦。url中包含了code和state,response会返回一个JSON,accesstoken就是OAuth的通行令牌,可以通过它有限定期限地访问github中授权用户的信息。

上面的教程还提出了state的自定义redis缓存和集成gitlab私服,在yt的项目中都有运用到。

最新回复(0)