코이_CO2
LIVING IS DYING
코이_CO2
전체 방문자
오늘
어제
  • 분류 전체보기 (45)
    • TIL ⚓️ (4)
      • OT주차 (1)
      • 1주차_풀스택 미니 프로젝트 (0)
      • 1주차_언어 기초(Java) (0)
      • 2주차_프로그래밍 기초 (1)
      • 3주차_주특기 입문(Spring) (0)
      • 4주차_주특기 숙련(Spring) (2)
    • WIL ⚓️ (0)
      • OT주차 (0)
      • 1주차_언어 기초(Java) (0)
      • 2주차_프로그래밍 기초 (0)
      • 3주차_주특기 입문(Spring) (0)
      • 4주차_주특기 숙련(Spring) (0)
    • Java의 정석 📖 (4)
      • Chapter 1. 자바를 시작하기 전에 (3)
      • Chapter 2. 변수 (0)
      • Chapter 3. 연산자 (0)
      • Chapter 4. 조건문과 반복문 (1)
    • Programmers (7)
      • Lv. 1 (7)
    • 혼자 공부하는 자바 📖 (8)
      • Chapter 05 참조 타입 (0)
      • Chapter 06 클래스 (3)
      • Chapter 07 상속 (1)
      • Chapter 08 인터페이스 (1)
      • Chapter 09 중첩 클래스 & 인터페이스 (0)
      • Chapter 10 예외 처리 (1)
      • Chapter 12 스레드 (1)
    • Java (2)
    • Spring (1)
    • Python (2)
    • Mysql (4)
    • Machine Learning (6)
      • 추측 통계 (2)
    • Data Analysis (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 스터디
  • 파이썬
  • 주특기 심화주차
  • jwt
  • 혼자 공부하는 자바
  • CRUD
  • sql
  • LV1
  • PYTHON
  • 하루기록
  • 개발일지
  • TIL/WIL
  • 부트스트랩
  • programmers
  • spring
  • DTO
  • 자바
  • 개발자
  • 게시판 프로젝트
  • TIL
  • 스프링
  • 배열
  • java
  • 혼자공부하는자바
  • 코딩
  • Spring Security
  • HTML
  • 항해99
  • 프로그래머스
  • 웹개발 종합반

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
코이_CO2
Spring

Spring Security

Spring Security
Spring

Spring Security

2022. 12. 9. 15:25

Spring Security

Spring 기반의 어플리케이션 인증, 권한, 인가 등의 보안을 담당하는 스프링 하위 프레임워크

스프링 서버에 필요한 인증 및 인가를 위해 많은 기능을 제공

'인증'과 '권한'에 대한 부분을 Filter 흐름에 따라 처리

보안과 관련해 체계적으로 많은 옵션을 제공해주기 때문에 개발자 입장에서 일일이 보완관련 로직을 작성하지 않아도 되는 장점이 존재

  • Spring Security Documentation
 

Hello Spring Security :: Spring Security

You can now run the Spring Boot application by using the Maven Plugin’s run goal. The following example shows how to do so (and the beginning of the output from doing so): Running Spring Boot Application $ ./mvn spring-boot:run ... INFO 23689 --- [ resta

docs.spring.io

 

Spring Security 적용 방법

스프링 시큐리티 프레임워크 추가

build.gradle에 다음 코드 붙여넣기

// 스프링 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'

스프링 시큐리티 활성화

1. WebSecurityConfig 클래스 생성

 

2. 어노테이션 추가

  • @Configuration
  • @EnableWebSecurity

3. SecurityFilterChain을 Bean으로 등록하고 아래 코드와 같이 CSRF 등 설정

  • CSRF(사이트 간 요청 위조, Cross-site request forgery)
    • 공격자가 인증된 브라우저에 저장된 쿠키의 세션 정보를 활용하여 웹 서버에 사용자가 의도하지 않은 요청을 전달하는 것
    • CSRF 설정이 되어있는 경우 html 에서 CSRF 토큰 값을 넘겨주어야 요청을 수신 가능
    • 쿠키 기반의 취약점을 이용한 공격 이기 때문에 REST 방식의 API에서는 disable 가능
    • POST 요청마다 처리해 주는 대신 CSRF protection 을 disable
// WebSecurityConfig (springboot 2.7 이상)
package com.xxx.springsecurity.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration  // 2. Configuration, 아래 EnableWebSecurity 어노테이션 추가
@EnableWebSecurity // 스프링 Security 지원을 가능하게 함
public class WebSecurityConfig {  // 1. WebSecurityConfig 클래스 생성

    @Bean  // SecurityFilterChain을 Bean으로 등록
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // CSRF 설정
        http.csrf().disable();
        
        http.authorizeRequests().anyRequest().authenticated();

        // 로그인 사용
        http.formLogin();
        
        return http.build();
    }

}

 

 

Spring Security 주요 컴포넌트

Spring Security와 Filter

  • Spring Security는 요청이 들어오면 Servlet FilterChain을 자동으로 구성한 후 거치게 한다
  • FilterChain은 여러 Filter를 chain 형태로 묶어놓은 것을 의미
  • Filter
    • 톰캣과 같은 웹 컨테이너에서 관리되는 서블릿 기술
    • Client 요청이 전달되기 전후의 URL 패턴에 맞는 모든 요청에 필터링을 해준다
    • CSRF, XSS 등의 보안 검사를 통해 올바른 요청이 아닐 경우 이를 차단
  • Spring Security는 이러한 기능을 활용하기 위해 Filter를 사용하여 인증과 인가를 구현

SecurityFilterChain

  • Spring의 보안 Filter를 결정하는데 사용되는 필터
  • session, jwt 등의 인증 방식들을 사용하는데 필요한 설정을 분리할 수 있는 환경을 제공

AbstractAuthenticationProcessingFilter

  • 사용자의 credential을 인증하기 위한 베이스 Filter
  • SecurityFilterChain 안에서 사용자 인증 처리를 하는 Filter라고 볼 수 있음

UsernamePasswordAuthenticationFilter

  • AbstractAuthenticationProcessingFilter를 상속한 Filter
  • Form Login 기반을 사용할 때 username과 password를 확인하여 인증
  • Form Login 기반은 인증이 필요한 URL 요청이 들어왔을 때 인증이 되지 않았다면 로그인페이지를 반환하는 형태

SecurityContextHolder

  • Spring Security로 인증을 한 사용자의 상세 정보를 저장
  • SecurityContext는 SecurityContextHolder로 접근할 수 있으며 Authentiaction 객체를 가지고 있다.

// 예시코드
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
context.setAuthentication(authentication);

SecurityContextHolder.setContext(context);

Authentication

  • 현재 인증된 사용자를 나타내며 SecurityContext에서 가져올 수 있다
  • principal : 사용자를 식별  (Username / Password 방식으로 인증할 때 보통 UserDetails 인스턴스)
  • credentials : 주로 비밀번호, 대부분 사용자 인증에 사용한 다음 비움
  • authorities : 사용자에게 부여한 권한을 GrantedAuthority로 추상화하여 사용
    <UserDetails>
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        UserRoleEnum role = user.getRole();
        String authority = role.getAuthority();
        System.out.println("authority = " + authority);

        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authority);
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(simpleGrantedAuthority);

        return authorities;
    }

Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());

// UsernamePasswordAuthenticationToken는 
// Authentication을 implements한 AbstractAuthenticationToken의 하위 클래스로, 
// 인증객체를 만드는데 사용된다.

UserDetailsService

  • username/password 인증 방식을 사용할 때 사용자를 조회하고 검증한 후 UserDetails를 반환
  • Custom하여 Bean으로 등록 후 사용이 가능

UserDetails

  • 검증된 UserDetails는 UsernamePasswordAuthenticationToken 타입의 Authentication을 만들 때 사용되며 해당 인증객체는 SecurityContextHoler에 세팅
  • Custom하여 사용 가능
  • Spring Security
  •  
  • Spring Security 적용 방법
  •  
  • Spring Security 주요 컴포넌트
코이_CO2
코이_CO2
나에게 찾아오는 뻔한 매일을 언제나 값지게 여길 줄 아는 내가 되기를

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.