SPRING/스프링 시큐리티 -JWT

JWT 실습 프로젝트 - 1 (프로젝트 생성 및 설정)

JUMP개발자 2021. 9. 11. 22:24

프로젝트 생성시 아래와 같은 dependency들을 추가합니다.

 

- JPA

- 스프링 시큐리티

- Validation

- Web

- lombok

- H2 DB

- jwt 관련 설정

    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'

    implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2'
    runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2'
    runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'

아래와 같이 Enable annotation processing을 체크해준다.

 

 

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
    defer-datasource-initialization: true

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

logging:
  level:
    me.silvernine: DEBUG

 

jwt

- HS512 알고리즘 사용하기 때문에 secret Key는 64Byte 이상이어야함.

- secretkey는 특정 문자열을 Base64로 암호화한 Key

- Token-validity 시간은 86400초