Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions keyword/chapter10/Spring Security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## ✅ 스프링 시큐리티란❓

---

> 스프링 기반 웹 애플리케이션에서 **인증**과 **인가**를 담당하는 스프링의 보안 프레임워크이다.
>
- 스프링 기반 애플리케이션의 보안을 처리하는 표준 도구로 널리 사용되며, 다양한 기능과 유연성을 제공한다.

### ✔️ 주요 기능

---

1. **인증 (Authentication)**
- 사용자가 누구인지 확인하는 과정으로, ID와 비밀번호, OAuth, SAML 등 다양한 인증 방식을 지원한다.
2. **인가 (Authorization)**
- 인증된 사용자가 애플리케이션 내에서 특정 리소스나 기능에 접근할 권한이 있는지 확인한다.
3. **보안 필터 (Security Filters)**
- 요청이 애플리케이션에 도달하기 전에 여러 단계의 필터를 통해 보안 검사를 수행한다.
- UsernamePasswordAuthenticationFilter, BasicAuthenticationFilter 등이 있다.

### ✔️ 작동 원리

---

> 스프링 시큐리티는 필터 체인 (Filter Chain) 구조를 기반으로 작동한다.
>
- HTTP 요청이 들어오면, 요청이 다양한 보안 필터를 통과하면서 인증 및 권한 부여 절차가 수행된다.
- 주요 흐름은 다음과 같다.
1. **HTTP 요청 → 필터 체인**
- 요청이 들어오면, 스프링 시큐리티는 등록된 보안 필터를 차례로 실행한다.
2. **인증 (Authentication)**
- 사용자가 제공한 자격 증명이 유효한지 확인한다.
- 인증 성공 시, SecurityContext에 인증 정보를 저장한다.
3. **인가 (Authorization)**
- 사용자가 요청한 리소스나 기능에 접근할 권한이 있는지 검사한다.
4. **응답 (Response)**
- 요청이 승인되면, 애플리케이션에서 비즈니스 로직을 처리한 후 응답을 반환한다.
95 changes: 95 additions & 0 deletions keyword/chapter10/인증 & 인가.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## ✅ 인증 (Authentication)

---

> 인증이란 **“당신은 누구입니까?”**라는 질문에 대한 답을 확인하는 과정이다.
>
- 즉, 사용자가 시스템에 접근하기 위해 자신을 증명하는 단계이다.

### ✔️ 동작 과정

---

1. 사용자는 자격 증명을 애플리케이션에 제공.
2. 애플리케이션은 이를 스프링 시큐리티가 제공하는 인증 메커니즘으로 검증.
3. 인증 성공 시, 사용자 정보는 Authentication 객체에 저장 및 SecurityContext에 추가.
4. 인증이 실패하면, 요청은 거부됨.

### ✔️ 인증에 사용되는 주요 구성 요소

---

- `AuthenticationFilter`

인증 프로세스를 처리하는 주요 인터페이스. 다양한 인증 방법을 조합할 수 있다.

- `AuthenticationProvider`

실제 인증 로직을 처리하는 컴포넌트로, 기본 구현체로는 DaoAuthenticationProvider가 있으며, 사용자 정보를 DB와 비교하여 인증한다.

- `UserDetailsService`

사용자 정보를 로드하는 서비스로, DB에서 사용자 정보를 가져오거나, 외부 인증 시스템과 연동하는 데 사용된다.

- `UserDetails`

사용자 객체의 인터페이스로, 사용자 이름, 비밀번호, 권한 등이 정보를 포함한다.


### ✔️ 스프링 시큐리티의 인증 흐름

---

1. 사용자가 로그인 요청을 보냄
2. UsernamePasswordAuthenticationFilter가 요청을 가로채고 사용자 입력값을 Authentication 객체로 변환
3. AuthenticationManager가 인증 처리 시작.
4. AuthenticationProvider가 실제 인증 수행.
5. 성공 시, SecurityContext에 Authentication 객체 저장.

## ✅ 인가 (Authorization)

---

> 인가는 **“당신이 이 작업을 수행할 권한이 있습니까?”**라는 질문에 대한 답을 확인하는 과정이다.
>
- 즉, 인증된 사용자가 시스템의 특정 리소스나 기능에 접근할 수 있는지를 결정한다.

### ✔️ 권한 부여의 유형

---

1. **URL 기반 접근 제어**
- 요청 URL, HTTP 메서드, 사용자 역할 등에 따라 접근을 제어한다.

```java
http
.authorizeHttpRequests((request) -> request
.requestMatchers("/", "/home", "/signup", "/members/signup", "/css/**").permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
)
```

2. **메서드 기반 접근 제어**
- 컨트롤러나 서비스 레벨에서 메서드별로 권한을 설정한다.
- @PreAuthorize 또는 @PostAuthorize 어노테이션을 사용.

```java
@PreAuthorize("hasRole('ADMIN')")
public String adminOnlyFunction() {
return "Admin 승인";
}
```

3. **도메인 객체 기반 접근 제어**
- 특정 데이터에 대한 접근 권한을 결정한다.
- 예를 들어, 사용자가 자신이 소유한 데이터만 수정할 수 있도록 제어한다.

### ✔️ 스프링 시큐리티의 인가 흐름

---

1. 인증된 사용자가 요청을 보냄.
2. 스프링 시큐리티는 AccessDecisionManager를 통해 권한 부여 로직을 실행.
3. 요청에 필요한 권한이 사용자에게 있는지 확인.
4. 권한이 없으면 AccessDeniedException을 발생시키고 403 Forbidden 응답 반환.
Binary file added mission/chapter10/DB 추가(2).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mission/chapter10/DB 추가.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mission/chapter10/카카오 로그인.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mission/chapter10/쿼리.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mission/chapter10/회원가입.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.