ใช้ LINE Login ด้วย Spring Security

Phayao Boonon
3 min readJan 17, 2019

--

LINE Login เป็นหนึ่งใน product หลักของ LINE ที่ทำให้เราสามารถทำให้ระบบของเรา Login ด้วยผู้ใช้งาน LINE ได้ และทำให้ระบบของเราเข้าถึงผู้ใช้งาน LINE ที่มีจำนวนมากได้อย่างง่ายดาย สำหรับระบบ mobile มี SDK ที่พร้อมสำหรับการใช้งาน แต่ระบบเว็บอย่าง Spring Framework ต้องใช้วิธี OAuth 2.0 และ OpenID Connect สำหรับตัวอย่างที่ LINE มีให้นั้นในเป็นกึ่ง SDK ที่เหมือนจะ manual โดยไม่ได้ใช้ Spring Framework เลย ในบทความนี้จะนำเสนอวิธีการใช้ Spring Security เพื่อใช้งาน OAuth 2.0 Let Go!!!

LINE Login

เป็นเครื่องมือที่ทำให้ผู้ใช้งานสามารถสร้าง account สำหรับ app หรือ website ได้ง่าย ด้วย LINE account เลย ซึ่งสามารถใช้งานได้ทั้ง Android, iOS และ website

สำหรับ website นั้น จะใช้วิธีโปรโตคอล OAuth 2.0 และ OpenID Connect สำหรับการ authentication และ authorization กับ LINE Login

Login Workflow

กระบวนการเว็บ Login ของ LINE Login จะใช้ OAuth 2.0 authorization code workflow และโปรโตคอล OpenID Connect ซึ่งจะต้องส่งข้อมูลไปหา Server และส่งรับ/ส่งข้อมูลกับ LINE Platform

  1. Application ส่ง User ไปที่ https://access.line.me/oauth2/v2.1/authorize โดยตรง ด้วย query parameter ที่จำเป็น
  2. จะเปิดเว็บ LINE Login บนเบราว์เซอร์และ User ทำการ Login เพี่อ authenticate หลังจาก LINE Platform ตรวจสอบข้อมูลของ User แล้ว ต้องยอมรับ permission สำหรับ Application นี้ด้วย
  3. LINE platform จะ redirect ให้ User กลับมาที่ Application ด้วย redirect_uri ด้วย authorization code และ state ใน query string
  4. Application จะต้อง request ไปที่ https://api.line.me/oauth2/v2.1/token ด้วย authorization code เพื่อขอ access token
  5. LINE Platform จะตรวจสอบข้อมูลที่ request ไปและ return กลับมากเป็น access token

ใช้ access token ที่ได้มานี้ขอข้อมูลของผู้ใช้ด้วย Social API

และจะต้องสมัคร LINE Login ด้วยขั้นตอนดังนี้

Spring Project

ใช้ Spring Initializr สร้าง Spring Boot project ด้วย dependency Web , Security เพื่อเป็นส่วนหลักของ Spring Security และ Thymeleaf สำหรับ Spring MVC เพื่อแสดงข้อมูลของ User ในหน้าเว็บ

โดยจะต้องเพิ่ม spring-security-oauth2-client dependency เพื่อใช้งาน OAuth 2.0 client ในการติดต่อกับ LINE Platform ซึ่ง dependency ของ Maven หลักๆ ดังนี้

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>

....
</dependency>
</dependencies>

Spring Security Config

สร้าง Spring Security configuration เพื่อตั้งค่าต่างๆ ให้กับ Spring Security ใช้ OAuth 2.0 ติดต่อกับ LINE Platform

ซึ่งแบ่งออกเป็น 2 ส่วน

  • LineLoginSecurityConfig เป็น inner class (หรือจะแยกเป็น class ต่างหากก็ได้) สำหรับตั้งค่า HttpSecurity ให้ใช้งาน OAuth2 Login
  • ClientRegistrationRepository เป็น Bean ตั้งค่ากับ ClientRegistration ที่เป็นตัวกำหนดค่าต่างๆ เป็นข้อมูลให้ OAuth 2.0 authorization code workflow

ค่าที่สำคัญของ ClientRegistration ที่ต้องกำหนดสำหรับ LINE Login ดังนี้

หรือเราอาจจะใช้ Spring Boot Auto-configuration ด้วยการกำหนดค่าต่างๆ ใน application.yml (หรือ application.properties) มาแทน Spring Security config เพือให้ Spring Boot สร้าง configuration ให้กับ Application เราเองโดยอัตโนมัติ

NOTE: ใช้อย่างใดอย่างหนึ่งระหว่าง Spring Security configuration หรือ Spring Boot Auto-configuration

MVC Controller และหน้า Index สำหรับแสดงข้อมูลผู้ใช้งานหลังจากที่ User login ด้วย LINE Login สำเร็จแล้ว

Run Project

เมื่อสร้างทุกอย่างเรียบร้อยแล้วตามธรรมเนียมก็ต้องรันดูว่าทำงานได้อย่างที่เราต้องการหรือไม่ด้วยคำสั่ง mvn spring-boot:run เหมือนเดิม

ทดลองเปิด http://localhost:8080 เว็บก็จะพาเราไปที่หน้า LINE Login

เมื่อเรากด Log in กระบวนการตรวจสอบของ OAuth 2.0 ด้วย Spring Security ก็จะทำงานและ request ข้อมูลของ User มาแสดงดังนี้ (ขอปิดข้อมูลสำคัญของผมไว้นะครับ)

ขออุทิศบทความนี้ให้กับความเขลาของผมเอง

สรุป

จากการสร้างระบบที่ใช้ LINE Login ด้วย Spring Security แทน LINE กึ่ง SDK ที่ให้มาเป็นตัวอย่างนั้น จะเห็นได้ว่าเพียงแค่ config ส่วนของ ClientRegistraration โดยที่ระบุเป็นข้อมูลต่างๆ ของ LINE Login OAuth 2.0 workflow ก็สามารถใช้งาน LINE Login ใน Application ของเราได้แล้ว

--

--

Phayao Boonon
Phayao Boonon

Written by Phayao Boonon

Software Engineer 👨🏻‍💻 Stay Hungry Stay Foolish

Responses (2)