## 개인기록용
# Spring Boot JWT Tutorial 학습
https://github.com/jennie267/jwt-tutorial
[SpringBoot] JWT Tutorial 1 - JWT
[SpringBoot] JWT Tutorial 2 - 프로젝트 생성
[SpringBoot] JWT Tutorial 3 - Security 설정
> [SpringBoot] JWT Tutorial 4 - Data 설정
[SpringBoot] JWT Tutorial 5 - JWT 코드, Security 설정 추가
[SpringBoot] JWT Tutorial 6 - DTO, Repository, 로그인
[SpringBoot] JWT Tutorial 7 - 회원가입, 권한검증
- Datasource, JPA 설정
- Entity 설정
- H2 Console 결과 확인
1. resources에 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
defer-datasource-initialization: true
logging:
level:
study.cherry: DEBUG
2. entity package 생성, User, Authority 클래스 생성
Authority.java
package study.cherry.jwttutorial.entity;
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "authority")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Authority {
@Id
@Column(name = "authority_name", length = 50)
private String authorityName;
}
User.java
package study.cherry.jwttutorial.entity;
import lombok.*;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "user")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@Column(name = "username", length = 50, unique = true)
private String username;
@Column(name = "password", length = 100)
private String password;
@Column(name = "nickname", length = 50)
private String nickname;
@Column(name = "activated")
private boolean activated;
@ManyToMany
@JoinTable(
name = "user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "authority_name")})
private Set<Authority> authorities;
}
3. resources 밑에 data.sql 파일 생성
data.sql
INSERT INTO USER (USER_ID, USERNAME, PASSWORD, NICKNAME, ACTIVATED) VALUES (1, 'admin', '$2a$08$lDnHPz7eUkSi6ao14Twuau08mzhWrL4kyZGGU5xfiGALO/Vxd5DOi', 'admin', 1);
INSERT INTO USER (USER_ID, USERNAME, PASSWORD, NICKNAME, ACTIVATED) VALUES (2, 'user', '$2a$08$UkVvwpULis18S19S5pZFn.YHPZt3oaqHZnDwqbCW9pft6uFtkXKDC', 'user', 1);
INSERT INTO AUTHORITY (AUTHORITY_NAME) values ('ROLE_USER');
INSERT INTO AUTHORITY (AUTHORITY_NAME) values ('ROLE_ADMIN');
INSERT INTO USER_AUTHORITY (USER_ID, AUTHORITY_NAME) values (1, 'ROLE_USER');
INSERT INTO USER_AUTHORITY (USER_ID, AUTHORITY_NAME) values (1, 'ROLE_ADMIN');
INSERT INTO USER_AUTHORITY (USER_ID, AUTHORITY_NAME) values (2, 'ROLE_USER');
4. h2 console 에 대한 요청은 Spring Security가 수행되지 않도록 SecurityConfig에 configure를 override
Securityconfig.java
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers(
"/h2-console/**"
,"/favicon.ico"
);
}
...
}
5. 서버 재시작
- 테이블들이 잘 만들어지는지 확인
6. localhost:8080/h2-console 접속
7. data.sql에 넣은 insert문이 잘 들어가있는지 확인
Reference
https://github.com/SilverNine/spring-boot-jwt-tutorial