BE전문가 프로젝트

0. 프로젝트 소개(Spring Security) 및 Security에 대하여 본문

SpringBoot Security

0. 프로젝트 소개(Spring Security) 및 Security에 대하여

원호보고서 2023. 6. 24. 18:24

Spring Security

Spring 기반의 Spring하위 프레임 워크이며 애플리케이션의 보안 즉 인증과 인가를 통하여 외부 및 사용자에 대한 접근을 제한하는 것을 의미한다. 인증과 인가는 Filter를 통하여 사용자의 권한을 확인후 접근을 허용하는 프로세스를 가진다.Filter는 Dispatcher Servlet으로 가기 전에 적용되므로 가장 먼저 URL 요청을 받지만, Interceptor는 Dispatcher와 Controller사이에 위치한다는 점에서 적용 시기의 차이가 있다. Spring Security는 보안과 관련해서 체계적으로 많은 옵션을 제공해주기 때문에 개발자 입장에서는 일일이 보안관련 로직을 작성하지 않아도 된다는 장점이 있다.

 

인증 및 인가

Authentication(인증)

  • 접근하는 사용자가 본인인가에 대한 인증

Authorization(인가)

  • 접근하려는 자원에 사용자가 알맞은 권한을 가지고 있는가를 확인

 

사용 기술

  • Jwt
  • Spring Security
  • OAuth2(카카오, 구글)

 

라이브러리 추가

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '11'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    /*******************************Security start*******************************/
    implementation 'org.springframework.boot:spring-boot-starter-security:2.7.5'
    implementation group: 'org.springframework.security', name: 'spring-security-oauth2-client', version: '5.6.3'// Oauth 2.0
    implementation group: 'com.auth0', name: 'java-jwt', version: '3.19.2'			//Jwt
    /*******************************Security end*******************************/

    /* JSON library */
    implementation 'com.google.code.gson:gson:2.9.0'

    /* JPA */
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    /* thymeleaf */
    implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '3.1.0'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'


    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

jwt 라이브러리에는 두가지가 존재한다.

  • auth0
  • jjwt

두가지 전부 사용해본 결과 큰 차이를 느끼지 못했고 필자에게 익숙한 auth0을 선택하기로 했다.

implementation group: 'com.auth0', name: 'java-jwt', version: '3.19.2'

 

appication.yml 설정

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: DB주소
    username: 
    password: 비밀번호

  jpa:
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    properties:
      hibernate:
        format_sql: false
        dialect: org.hibernate.dialect.MySQL8Dialect
      javax:
        persistence:
          validation:
            mode: none
    show-sql: true

server:
  port: 8080

jwt:
  secret: 시크릿 키

 

#1 spring

  • DB에 대한 정보를 기술하였으며 mySql사용함

#2 jpa

ddl-auto - 데이터베이스 스키마 자동 생성

  • create:          기존테이블 삭제 후 다시 생성(Drop + create)
  • create-drop:  create와 같으나 종료시점에 테이블 Drop(테스트코드 실행할 때 주로 사용)
  • update:         변경분만 반영(운영 DB에는 사용하면 안됨) - 추가만 가능
  • validate:        엔티티와 테이블이 정상 매핑되어있는지만 확인
  • none:            사용하지 않음

physical-strategy

  • 테이블 명 및 컬럼명 작성한 그대로 사용(적용 안할 시 대문자로 작성해도 소문자로 바뀜)

format_sql

  • 사용된 쿼리가 들여쓰기 형태로 출력됨

dialect

  • sql마다 문법이 조금씩 다르기 때문에 사용할 DB정보를 넣음

 

Comments