튜토리얼 코드

https://github.com/SilverNine/spring-boot-jwt-tutorial

바로가기

Spring Boot JWT Tutorial (1) – JWT 소개, 프로젝트 생성

Spring Boot JWT Tutorial (2) – Security 기본 설정, Data 설정

Spring Boot JWT Tutorial (3) – JWT 코드, Security 설정 추가

Spring Boot JWT Tutorial (4) – Repository, 로그인

Spring Boot JWT Tutorial (5) – 회원가입, 권한검증

JWT 설정 추가

application.yml

spring:

  h2:
    console:
      enabled: true

  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:

  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        format_sql: true
        show_sql: true

jwt:
  header: Authorization
  secret: c2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQtc2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQK
  token-validity-in-seconds: 86400

logging:
  level:
    me.silvernine: DEBUG

application.yml에 jwt 관련 설정을 추가합니다.

HS512 알고리즘을 사용할 것이기 때문에 secret key는 512bit, 즉 64byte 이상을 사용해야 합니다.

터미널에서 secret key를 base64로 인코딩하여 secret 항목에 채워넣습니다.

$ echo ‘silvernine-tech-spring-boot-jwt-tutorial-secret-silvernine-tech-spring-boot-jwt-tutorial-secret’|base64

build.gradle

plugins {
	id 'org.springframework.boot' version '2.4.1'
	id 'io.spring.dependency-management' version '1.0.10.RELEASE'
	id 'java'
}

group = 'me.silvernine'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'

	compile group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2'
	runtime group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2'
	runtime group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'

}

test {
	useJUnitPlatform()
}
Powered by Fruition