본문 바로가기
Spring/SpringSecurity

[Spring / Spring Security] 01. 스프링 시큐리티 개요 및 특징, 방법

by SIXXXX_ 2022. 9. 12.
728x90

개요

Sping Security : 인증, 인가 기능을 가진 프레임워크

 : 보안을 위한 표준이라고 생각

 

Spring Security ↔ 인터셉터, 필터 기반 보안 기능 구현

 

공식문서 : https://spring.io/projects/spring-security

 

Spring Security

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

spring.io

 

 

특징

- 확장성 고려됨 → 로그인 기능 구현

- 라이브러리 : spring-security-oauth2-autoconfigure 사용

spring-security-oauth2-autoconfigure : 기존에 안전하게 작동하던 코드를 사용하는 것, Client 라이브러리 사용해서 진행

spring-security-oauth 프로젝트

- 유지상태로 결정, 버그수정 정도의 기능만 추가되었다.

- springboot용 starter 출시

- 확장 포인트를 고려해서 설계된 상태

- client 인증정보만 입력하기

- enum 값으로 대체, CommonOAtu2Provider라는 enum새롭게 추가 : 구글, 페이스북, 옥타, 깃허브, 외 다른 블로그들 추가 가능

 

tokenUri, userInfoUri, userNameAttributeName, jwkSetUri, authorizationUri, scope(”openid”, “profile”, “email”) 등이 필요

 

방법(Google Cloud Platform, OAuth2.0 : https://developers.google.com/identity/protocols/oauth2/)

사용자인증정보 → OAuth 클라이언트 ID →동의화면 구성 → Google API 범위 email, profile, openid

OAuth 클라이언트 ID 만들기 → 웹 애플리케이션 유형

Oauth2.0 클라이언트 구성 중

  • 승인된 리디랙션 URI : 구글에서 리다이렉트할 URL: 기본적으로는 {도메인}/login/oautho2/code/{소셜서비스코드}로 리다이렉트 URL을 지원, 사용자가 별도로 리다이렉트 URL을 지원하는 Controller를 만들 필요가 없고 시큐리티에서 이미 구현해놓은 상태
  • AWS 서버에 배포하게 되면 localhost 외에 추가로 주소를 넣어야 한다.
  • 클라이언트 ID, 클라이언트 보안 비밀코드를 프로젝트에서 설정
  • src/main/resources/ : application-oauth.properties 파일 생성

 

scope= profile,email

open id provider로 인식(google 인증) : open id 라는 scope

OAuth2Service(나머지 네이버, 카카오 등) 각각 만들어줘야 한다.

 

application-xxx.propertiese : xxx라는 이름의 profile이 생성 관리, profile = xxx라는 식으로 호출하면 해당

properties의 설정들을 가져올 수 있다.

 

추가 내용

spring.profies.include=oauth

 

클라이언트 아이디와 클라이언트 보안 비밀은 보안이 중요한 정보

application-oauth.properties 파일이 올라가는 것을 방지하기 위해

 

.gitignore에 코드추가

  • application-oauth.properties

커밋 파일 목록에 properties 나오지 않으면 성공 →  나온다면 캐시문제

 

 

 

@Enumerated(EnumType.STRING)

  • JPA로 데이터베이스로 저장할 때 Enum값을 어떤 형태로 저장할지를 결정한다.
  • 기본적으로는 int 로 된 숫자가 저장된다.
  • 숫자로 저장되면 데이터베이스로 확인할때, 그 값이 무슨 코드를 의미하는지 알 수가 없다.
  • 그래서 문자열(EnumType.STRING)로 저장할 수 있도록 선언한다
  • 각 사용자의 권한을 관리할 Enum클래스 Role을 생성한다.