ตัวอย่างการเขียน Spring-boot Reactive OAuth Client
ตอนนี้มีตัวอย่างสำหรับ
- Line
- มีความรู้เรื่อง OAuth 2.0
- ถ้ายังไม่แม่น สามารถอ่านเพิ่มเติมได้ที่ OAuth 2.0 คืออะไร ทำงานยังไง แบบ Step by Step
- ลงทะเบียน Apps ของ Facebook และ Config OAuth ที่หน้า Facebook Developer
- สร้าง Google Project ที่หน้า Console พร้อมทั้ง Config ค่าต่าง ๆ สำหรับ OAuth
- สร้าง Line Channel ที่หน้า Line Developer พร้อมทั้ง Config ค่าต่าง ๆ สำหรับ OAuth
- เมื่อได้
clientId
และclientSecret
มาแล้ว ก็สามารถทดสอบการเขียน Code / ใช้งานได้ - หมายเหตุ โต ๆ อย่าลืม set Redirect URIs ของ Google, Facebook, Line ฯลฯ ให้ตรงกับที่เราจะ redirect กลับ
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<java.version>${java.version}</java.version>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
- Dependency
java-jwt
ใช้สำหรับกรณีที่ OAuth Provider นั้นรองรับ OIDC (OpenID Connect
) แล้วมีการ returnid_token
กลับมาให้ด้วย ซึ่ง token นี้เป็น jwt เราก็จะใช้ dependency นี้ในการ decodeid_token
ออกมาเป็นข้อมูล User เช่น อย่างกรณีของ Line เป็นต้น
@SpringBootApplication
@ComponentScan(basePackages = {"me.jittagornp"})
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
@RestController
public class IndexController {
@GetMapping({"", "/"})
public Mono<String> hello() {
return Mono.just("OAuth Client.");
}
}
@Slf4j
public abstract class OAuthControllerAdapter {
protected abstract OAuthClient getOAuthClient();
protected abstract String getAuthorizationCodeEndpoint();
protected abstract String getAccessTokenEndpoint();
protected abstract String getUserInfoEndpoint();
protected abstract OAuthUserInfo mapUserInfo(final Map<String, Object> userInfo);
...
}
@Slf4j
@RestController
@RequestMapping("/google/oauth")
public class GoogleOAuthController extends OAuthControllerAdapter {
@Override
protected OAuthClient getOAuthClient() {
return OAuthClient.builder()
.clientId("3355xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com")
.clientSecret("D9pnYkQxxxxxxxxxxxxxxxxxx")
.scope("profile email")
.build();
}
...
}
@Slf4j
@RestController
@RequestMapping("/facebook/oauth")
public class FacebookOAuthController extends OAuthControllerAdapter {
@Override
protected OAuthClient getOAuthClient() {
return OAuthClient.builder()
.clientId("314846xxxxxxxxxxxxxxxxxx")
.clientSecret("647a877e6d5d9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.scope("email")
.build();
}
...
}
อย่าลืมแก้ค่า clientId
กับ clientSecret
ก่อน Run Code
cd ไปที่ root ของ project จากนั้น
$ mvn clean package
$ mvn spring-boot:run
เปิด browser แล้วเข้า http://localhost:8080
ให้ไปที่ url นี้
เมื่อได้ authorization code มาแล้วให้ นำ code มาขอ access_token ที่ url นี้
ให้ไปที่ URL นี้
เมื่อได้ authorization code มาแล้วให้ นำ code มาขอ access_token ที่ url นี้