[SpringSecurity] 게시글 포스팅 시 403에러 해결하기

Kaite.Kang·2023년 3월 8일
0
post-thumbnail

1. 글 포스팅시 403에러

스프링부트를 EC2에 배포하고, RDS 연동 후 프로젝트를 재시작해도 게시글 데이터가 유지되는지 확인하고자 한다. EC2 도메인으로 접속하여 로그인 후 글 포스팅할 때 아래와 같이 403에러가 발생한다.

2. 원인

  • 사용자에게 포스팅할 수 있는 권한이 없어서 발생하는 문제이다.
MariaDB [freelec]> select * from user;
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
| id | created_date               | modified_date              | email                 | name        | picture                                                                                | role  |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
|  1 | 2023-02-18 14:01:58.740955 | 2023-02-18 14:01:58.740955 | a4141407@gmail.com    | jhkang      | https://lh3.googleusercontent.com/a/AEdFTp6SVePx6mVgcwkTrS0VBO7BvOtT05cojUg52oRL=s96-c | GUEST |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
1 rows in set (0.01 sec)

3. 해결 방법

  • RDS 에 접속하여 데이터베이스에서 User 권한 GUEST에서 USER 로 바꿔준다.

    • SELECT * FROM USER;
      UPDATE user SET role = 'USER' where name='';
  • 재로그인 후 다시 글쓰기하면 포스트가 정상적으로 등록된다.

  • 실행 절차(SQL)

$ mysql -u admin --host freelec-springbooot2-webservice.************.rds.amazonaws.com -P 3306 -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 53981
Server version: 10.6.10-MariaDB managed by https://aws.amazon.com/rds/

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| freelec            |
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| sys                |
| tmp                |
+--------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> use freelec;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [freelec]> show tables;
+---------------------------+
| Tables_in_freelec         |
+---------------------------+
| SPRING_SESSION            |
| SPRING_SESSION_ATTRIBUTES |
| posts                     |
| test                      |
| user                      |
+---------------------------+
5 rows in set (0.00 sec)

MariaDB [freelec]> select * from user;
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
| id | created_date               | modified_date              | email                 | name        | picture                                                                                | role  |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
|  1 | 2023-02-18 14:01:58.740955 | 2023-02-18 14:01:58.740955 | a4141407@gmail.com    | jhkang      | https://lh3.googleusercontent.com/a/AEdFTp6SVePx6mVgcwkTrS0VBO7BvOtT05cojUg52oRL=s96-c | GUEST |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
3 rows in set (0.01 sec)

MariaDB [freelec]> UPDATE user SET role = 'USER' where name='강주희';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [freelec]> select * from user;
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
| id | created_date               | modified_date              | email                 | name        | picture                                                                                | role  |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
|  1 | 2023-02-18 14:01:58.740955 | 2023-02-18 14:01:58.740955 | a4141407@gmail.com    | jhkang      | https://lh3.googleusercontent.com/a/AEdFTp6SVePx6mVgcwkTrS0VBO7BvOtT05cojUg52oRL=s96-c | USER  |
+----+----------------------------+----------------------------+-----------------------+-------------+----------------------------------------------------------------------------------------+-------+
1 rows in set (0.00 sec)

4. 재확인

로그인 후 게시글에 포스팅을 등록한다.

프로젝트를 배포 스크립트를 실행하여 재시작한다. 이전에는 메모리에 데이터가 저장되어서 프로젝트를 재시작하면 데이터가 초기화되었다.

$ ./deploy.sh
> Git Pull
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 20 (delta 5), reused 19 (delta 4), pack-reused 0
Unpacking objects: 100% (20/20), 1.83 KiB | 233.00 KiB/s, done.
From https://github.com/KangJuHui/springboot2-webservice
   ec93723..bdd008b  master     -> origin/master
Updating ec93723..bdd008b
Fast-forward
 build.gradle                                           |  3 +++
 out/production/resources/application-test.properties   | 12 ------------
 src/main/resources/application-real.properties         |  3 +++
 src/test/java/com/spring/book/HelloControllerTest.java | 24 ++++++------------------
 4 files changed, 12 insertions(+), 30 deletions(-)
 delete mode 100644 out/production/resources/application-test.properties
 create mode 100644 src/main/resources/application-real.properties
> 프로젝트 Build 시작
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :test
2023-02-07 23:45:17.793  INFO 11278 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-02-07 23:45:17.805  INFO 11278 --- [ionShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
Hibernate: drop table if exists posts
2023-02-07 23:45:17.807  WARN 11278 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 90121, SQLState: 90121
2023-02-07 23:45:17.808 ERROR 11278 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper   : Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
2023-02-07 23:45:17.814  WARN 11278 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 90121, SQLState: 90121
2023-02-07 23:45:17.814 ERROR 11278 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper   : Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
2023-02-07 23:45:17.815  WARN 11278 --- [ionShutdownHook] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method failed on bean with name 'entityManagerFactory': org.hibernate.exception.JDBCConnectionException: Unable to release JDBC Connection used for DDL execution
2023-02-07 23:45:17.816  INFO 11278 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-02-07 23:45:17.823  INFO 11278 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

BUILD SUCCESSFUL in 39s
8 actionable tasks: 6 executed, 2 up-to-date
> step1 디렉토리로 이동
> Build 파일 복사
> 현재 구동중인 애플리케이션 pid 확인
현재 구종 중인 애플리케이션 pid:
> 현재 구동 중인 애플리케이션이 없으므로 종료하지 않았습니다.
> 새 애플리케이션 배포
> JAR Name: springboot2-webservice-1.0-SNAPSHOT-plain.jar
$ nohup: appending output to ‘nohup.out’

$ cat nohup.out |egrep 'http|JVM'
2023-02-07 17:57:47.250  INFO 11932 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-02-07 17:57:54.253  INFO 11932 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-02-07 17:57:54.322  INFO 11932 --- [           main] com.spring.book.Application              : Started Application in 12.643 seconds (JVM running for 13.856)

RDS 연동 후에는 애플리케이션 재시작 후에도 데이터가 유지되는 것을 확인할 수 있다.

0개의 댓글