인프런 커뮤니티 질문&답변

유요한님의 프로필 이미지

작성한 질문수

Spring Boot JWT Tutorial

Security 설정, Data 설정

sql 에러

해결된 질문

작성

·

1.1K

0

@Entity
@Table(name = "users")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {


    @JsonIgnore
    @Id
    @Column(name = "userId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column(name="userName", length = 50, unique = true)
    private String userName;

    @JsonIgnore
    @Column(name = "userPw", length = 100)
    private String password;

    @Column(name = "nickName", length = 50)
    private  String nickName;

    @JsonIgnore
    @Column(name = "activated")
    private boolean activated;


    // @ManyToMany와 @JoinTable은 User객체와 권한객체의 다대다 관계를
    // 일대다, 다대일 관계의 조인 테이블로 정했다는 뜻입니다.
    @ManyToMany
    @JoinTable(
            name = "user_authority",
            joinColumns = {@JoinColumn(name = "userId", referencedColumnName = "userId")},
            inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "authority_name")})
    private Set<Authority> authorities;
package com.example.jwt_security.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;
}

Hibernate:

drop table if exists authority CASCADE

Hibernate:

drop table if exists user_authority CASCADE

Hibernate:

drop table if exists users CASCADE

Hibernate:

create table authority (

authority_name varchar(50) not null,

primary key (authority_name)

)

Hibernate:

create table user_authority (

user_id bigint not null,

authority_name varchar(50) not null,

primary key (user_id, authority_name)

)

Hibernate:

create table users (

user_id bigint generated by default as identity,

activated boolean,

nick_name varchar(50),

user_pw varchar(100),

user_name varchar(50),

primary key (user_id)

)

Hibernate:

alter table users

add constraint UK_k8d0f2n7n88w1a16yhua64onx unique (user_name)

Hibernate:

alter table user_authority

add constraint FK6ktglpl5mjosa283rvken2py5

foreign key (authority_name)

references authority

Hibernate:

alter table user_authority

add constraint FKhi46vu7680y1hwvmnnuh4cybx

foreign key (user_id)

references users

 

insert into users (userName, userPw, nickName, activated) values ('admin', '$2a$08$lDnHPz7eUkSi6ao14Twuau08mzhWrL4kyZGGU5xfiGALO/Vxd5DOi', 'admin', 1);
insert into users (userName, userPw, nickName, activated) values ('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');

여기서 into 뒤에가 에러가 생기네요 테이블까지는 제대로 생성됐는데 왜그러는지 모르겠어요

답변 2

0

정은구님의 프로필 이미지
정은구
지식공유자

안녕하세요 🙂

Spring Boot 3.4.0 (SNAPSHOT) 버전에 맞춰 샘플 코드를 업데이트했습니다.

아래 링크에서 Java와 Kotlin 버전의 최신 샘플 코드를 확인하실 수 있으니 참고 부탁드립니다.

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

Kotlinhttps://github.com/SilverNine/spring-boot-jwt-tutorial-kotlin

0

Please attach the error log.

유요한님의 프로필 이미지
유요한
질문자

 

:: Spring Boot :: (v2.7.8)

2023-02-06 22:09:19.774 INFO 21336 --- [ main] c.e.jwt_security.JwtSecurityApplication : Starting JwtSecurityApplication using Java 11.0.17 on DESKTOP-T2712FI with PID 21336 (C:\study_\Spring\springBoot\JWT_security\out\production\classes started by user in C:\study_\Spring\springBoot\JWT_security)

2023-02-06 22:09:19.775 INFO 21336 --- [ main] c.e.jwt_security.JwtSecurityApplication : No active profile set, falling back to 1 default profile: "default"

2023-02-06 22:09:20.097 INFO 21336 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2023-02-06 22:09:20.104 INFO 21336 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.

2023-02-06 22:09:20.359 INFO 21336 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)

2023-02-06 22:09:20.365 INFO 21336 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2023-02-06 22:09:20.365 INFO 21336 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.71]

2023-02-06 22:09:20.420 INFO 21336 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2023-02-06 22:09:20.420 INFO 21336 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 623 ms

2023-02-06 22:09:20.435 INFO 21336 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

2023-02-06 22:09:20.466 INFO 21336 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.

2023-02-06 22:09:20.472 INFO 21336 --- [ main] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:tcp://localhost/~/test'

2023-02-06 22:09:20.532 INFO 21336 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]

2023-02-06 22:09:20.553 INFO 21336 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.14.Final

2023-02-06 22:09:20.621 INFO 21336 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}

2023-02-06 22:09:20.669 INFO 21336 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

Hibernate:

 

drop table if exists authority CASCADE

Hibernate:

 

drop table if exists user_authority CASCADE

Hibernate:

 

drop table if exists users CASCADE

Hibernate:

 

create table authority (

authority_name varchar(50) not null,

primary key (authority_name)

)

Hibernate:

 

create table user_authority (

user_id bigint not null,

authority_name varchar(50) not null,

primary key (user_id, authority_name)

)

Hibernate:

 

create table users (

user_id bigint generated by default as identity,

activated boolean,

nick_name varchar(50),

user_pw varchar(100),

user_name varchar(50),

primary key (user_id)

)

Hibernate:

 

alter table users

add constraint UK_k8d0f2n7n88w1a16yhua64onx unique (user_name)

Hibernate:

 

alter table user_authority

add constraint FK6ktglpl5mjosa283rvken2py5

foreign key (authority_name)

references authority

Hibernate:

 

alter table user_authority

add constraint FKhi46vu7680y1hwvmnnuh4cybx

foreign key (user_id)

references users

2023-02-06 22:09:20.951 INFO 21336 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]

2023-02-06 22:09:20.956 INFO 21336 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2023-02-06 22:09:21.030 INFO 21336 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@4cf01c41, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@57bac3f0, org.springframework.security.web.context.SecurityContextPersistenceFilter@e1a150c, org.springframework.security.web.header.HeaderWriterFilter@70840a5a, org.springframework.security.web.csrf.CsrfFilter@4805069b, org.springframework.security.web.authentication.logout.LogoutFilter@6edd4fe2, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@4e9ea32f, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@407b41e6, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3291d9c2, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@55f6f965, org.springframework.security.web.session.SessionManagementFilter@7a34c1f6, org.springframework.security.web.access.ExceptionTranslationFilter@4f6ff62, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@3909a854]

2023-02-06 22:09:21.033 WARN 21336 --- [ main] o.s.s.c.a.web.builders.WebSecurity : You are asking Spring Security to ignore Ant [pattern='/h2-console/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2023-02-06 22:09:21.033 INFO 21336 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will not secure Ant [pattern='/h2-console/**']

2023-02-06 22:09:21.033 WARN 21336 --- [ main] o.s.s.c.a.web.builders.WebSecurity : You are asking Spring Security to ignore Ant [pattern='/favicon.ico']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2023-02-06 22:09:21.033 INFO 21336 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will not secure Ant [pattern='/favicon.ico']

2023-02-06 22:09:21.058 WARN 21336 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

2023-02-06 22:09:21.228 INFO 21336 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

2023-02-06 22:09:21.235 INFO 21336 --- [ main] c.e.jwt_security.JwtSecurityApplication : Started JwtSecurityApplication in 1.681 seconds (JVM running for 2.035)

A file path that is implicitly relative to the current working directory is not allowed in the database URL "jdbc:h2:tcp://localhost:9092/default". Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-210]