Hyunseok
프로그래밍/개인홈페이지 :: Spring boot + JWT + OAuth2 + Redis로 Restful 로그인을 만들어보자 1
2022. 11. 28. 11:17

첫 팀 프로젝트에서 못해서 분했던 OAuth2 로그인.. 이번에는 꼭 해보리다 생각하고 시큐리티를 건드리기 시작했다 

 

인터넷 보니 온갖 여러 가지 방법으로 만들어놨던데.. 나도 그 줄에 껴보려한다 

 

일단 요구사항부터 알아보자 

 

1. spring boot가 rest서버로만 사용된다 (JSP thymeleaf 사용 x)

2. 로그인은 액세스 + 리프래시 토큰으로만 이루어진다 

3. 로그인 유지는 쿠키를 이용하여 유지한다

 

기타 세팅으로는

maven + spring boot 3.0이 사용되었다

 

이 글에서 설명하는 내용은 이렇게 크게 3가지, 작게2가지가 요구된다 

 

 

제일 먼저 pom.xml에 레디스와 JWT를 사용하기 위한 dependency를 넣는다 

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.7.5</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-api</artifactId>
    <version>0.11.5</version>
</dependency>

(분명 이 기능 만들 때만 해도 2.7.5였는데 지금 레포 들어가 보니 3.0버전이 올라와있다..)

 

그리고 Redis를 컴퓨터에 깔아준다 

(흑흑 그냥 mysql 쓰면서 수동으로 지워줄까도 생각했지만 레디스가 너무 재밌어 보였다)

 

개인적으로 도커를 깔아서 쓰고 있으니 docker compose파일을 작성한다

 

version: '3'
services:
  redis:
    image: redis:latest
    command: redis-server --port 12621
    container_name: redis_service
    hostname: redis_service
    labels:
      - "name=redis"
      - "mode=standalone"
    ports:
      - 12621:12621

compose.yml작성 후

docker compose up -d

 

그러면 Redis의 설정이 끝난다 

 

그런 뒤 GCE에 가서 

 

클라이언트 

클라이언트 보안 비밀

의 값을 가져와서 application.properties에다 아래와 같이 써준다

spring.security.oauth2.client.registration.google.client-id=클라이언트보안비밀
spring.security.oauth2.client.registration.google.client-secret=클라이언트보안비밀
spring.security.oauth2.client.registration.google.scope=profile,email

참고로 scope 안으면 작동을 안 하니 꼭 써주도록 하자 (안 쓰고 계속 시도했다가 15분 정도 날렸다)

 

이제 본격적으로 spring boot로 가서 설정을 건드려보자 

 

2편

2022.11.28 - [프로그래밍/ㄴ 개인홈페이지] - :: Spring boot + JWT + OAuth2 + Redis로 Restful 로그인을 만들어보자 2

 

:: Spring boot + JWT + OAuth2 + Vue + Redis 로그인을 만들어보자 2

이 글은 아래의 글의 2편입니다 2022.11.28 - [프로그래밍/ㄴ 개인홈페이지] - :: Spring boot + JWT + OAuth2 + Vue + Redis 로그인을 만들어보자 1 이번 편에서는 util들을 설정해보자 간단한게 JWT와 Redis용 유틸

hbyun.tistory.com

 


프로그래밍/개인홈페이지의 다른 글