[Spring Security] Back-End - Spring Security설정(With JWT) - 1
2024. 11. 28. 11:27ㆍJAVA/Spring Security
Spring Boot 3 + Security + JWT 설정
이클립스 설정
기본 이클립스에 STS를 설치하여 사용하였다.
1. 프로젝트 생성
Finish 클릭 하여 프로젝트 생성
2. build.gradle dependencies 추가
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.0'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'kr.co.infob'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(23)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
//Mybatis 사용
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
//JWT 추가
implementation 'io.jsonwebtoken:jjwt-api:0.12.6'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6'
//JWT Token 저장을 위한 Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
//Log4jdbc
implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
JWT, Redis, Log4jdbc, Mybatis 관련 항목 추가
해당 파일을 수정하고 난 후에는 반드시 Gradle를 Refresh 해야 한다.
Project 구성 및 설정
1. Package 구조
2. Database 구성
PostgrsSQL을 사용한다.
URL에 대한 권한 관리를 Database를 통해서 관리하도록 한다.
USER_INFO : 사용자 정보를 담는 Entity
ROLE_INFO : 권한정보를 담는 Entity
MENU_INFO : 메뉴정보를 담고 있는 Entity
RESRC_INFO : 리소스(URL)정보를 담고 있는 Entity
RESRC_INFO : 리소스에 대한 정보를 권한별로 관리하는 Entity
3. application.yml 설정
처음 프로젝트를 생성하고 나면 application 설정 파일이 properties 되어 있을 것이다.
파일명을 application.properties 에서 application.yml 로 변경한다.
properties를 사용해도 되지만 나는 가독성을 위해 yml을 사용한다.
server:
port: 8080
servlet:
context-path: /
spring:
application:
name: first-jwt-project
profiles:
active:
- local
#Database Configuration
datasource:
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:postgresql://192.168.1.100:5432/testdb
username: postgres
password: testpassword!@
# hikariCP property setting
hikari:
#풀의 최대 연결 수 설정
maximum-pool-size: 10
minimum-idle: 10
#풀이 연결을 사용할 수 있을 때까지 대기하는 최대 시간(ms)을 지정
connection-timeout: 5000
connection-init-sql: SELECT 1
#연결이 풀에서 유휴 상태로 있을 수 있는 최대 시간(ms)을 지정
idle-timeout: 600000
max-lifetime: 1800000
#자동 커밋 끄기
auto-commit: true
#연결 풀의 이름을 지정
pool-name: Test HikariCP
data:
redis:
host: localhost
port: 6397
#404 같은 오류 메시지를 JSON 형태로 처리하기 위한 설정
web:
resources:
add-mappings: false
# Mybatis 설정
mybatis:
configuration:
cache-enabled: true
lazy-loading-enabled: false
aggressive-lazy-loading: true
multiple-result-sets-enabled: true
use-column-label: true
use-generated-keys: false
auto-mapping-behavior: PARTIAL
default-executor-type: SIMPLE
default-statement-timeout: 25000
map-underscore-to-camel-case: true
# 쿼리에서 Parameter가 null인 경우, 오류가 발생하는 것 방지
jdbc-type-for-null: NULL
mapper-locations: classpath*:mappers/**/*.xml
# JWT 정보 설정
jwt:
secret: af60addca9ea3e3c099551e1b6576c9966dce0a33de879dd7e160f86dbd872ca236d6e9ee66fb6e30039fe7c345324a10f3d0741b0600fa7a45df4c6691eff4f4209767ed39f51e37717d8feecd5dd14fc34ebe619e6a29ae91d9ffe134cb5718bec0b3680d6ae7fc09e67763fe7c05d05d3ba69f47211163852633755b7f861132b0c98f8d7c1af9152d547408e676867a0a32fb525a4354180f5fb6b2dc23b5faa4155b8db63385f96259a90b6ee0e74a5b90a4f0f4fa96fafc296c64588b5c009b3829ae2e1d69a1cf7569b50a65fa553350495d18816f785f961c970c0a9cb9c8da25cc5e9fa4a3e9527a132d616b232d1ee21c3bf6dc8d9e3376e2e82c0
expiration:
access: 1800000
refresh: 604800000
위 정보까지 설정하면 환경은 개발 준비는 완료 되었다.
'JAVA > Spring Security' 카테고리의 다른 글
[Spring Security] 권한 접두사(ROLE_) 제거 처리 (0) | 2024.12.06 |
---|---|
[Spring Security] Back-End - Spring Security설정(With JWT) - 3 (0) | 2024.11.28 |
[Spring Security] Back-End - Spring Security설정(With JWT) - 2 (0) | 2024.11.28 |
[Spring Security 6.0] SSO 로그인 시 Anonymous user로 처리 될 때 (0) | 2023.06.27 |
[Spring Security] JSP 파일에서 Custom Tag를 이용한 권한 체크 (0) | 2018.04.25 |