티스토리 뷰
반응형
인증(Authentication) vs 인가(Authorization)
구분 | 설명 | 예시 |
인증 (Authentication) | 사용자가 누구인지 확인하는 절차 | "너 진짜 홍길동이 맞아?" → 로그인 ID/비번 확인 |
인가 (Authorization) | 인증된 사용자가 무엇을 할 수 있는지 결정 | "홍길동은 /admin 페이지 볼 권한이 있나?" |
➡ 인증이 먼저, 그 다음에 인가가 진행됩니다.
스프링 시큐리티의 필터 기반 구조
스프링 시큐리티는 FilterChain 기반으로 동작합니다.
즉, 모든 HTTP 요청은 DispatcherServlet에 도달하기 전에 SecurityFilterChain을 거칩니다.
Client → SecurityFilterChain → DispatcherServlet → Controller |
기본적으로 다음과 같은 필터들이 있습니다.
- UsernamePasswordAuthenticationFilter → 폼 로그인 인증 처리
- BasicAuthenticationFilter → HTTP Basic 인증 처리
- ExceptionTranslationFilter → 인증/인가 예외 처리
- FilterSecurityInterceptor → 최종 권한 체크
Spring Security 동작 흐름 (폼 로그인 예시)
- 사용자가 /login에 POST 요청 (아이디/비밀번호 제출)
- UsernamePasswordAuthenticationFilter에서 인증 처리
- AuthenticationManager가 UserDetailsService 호출
→ DB 또는 메모리에서 사용자 정보 로드 - 비밀번호 비교 후 성공 시 Authentication 객체 생성
- SecurityContext에 인증 정보 저장
- 이후 요청 시 SecurityContext에서 인증 정보 확인 후 권한 부여
기본 로그인 페이지
Spring Security를 의존성에 추가하면 /login 페이지가 자동 제공됩니다.
예시:
Username: user
Password: (콘솔에 랜덤 비번 출력)
실습 예시 – SecurityFilterChain 기본 설정
📂 파일명: SecurityConfig.java
package com.example.security;
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.web.SecurityFilterChain;
@Configuration // 설정 클래스임을 명시
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// 모든 요청은 인증 필요
.authorizeHttpRequests(auth -> auth
.anyRequest().authenticated() // 모든 요청 인증 필요
)
// 기본 로그인 폼 사용
.formLogin(form -> form
.permitAll() // 로그인 페이지는 누구나 접근 가능
)
// 로그아웃 설정
.logout(logout -> logout
.permitAll()
);
return http.build();
}
}
의존성 설정
📌 Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
📌 Gradle (Groovy)
implementation 'org.springframework.boot:spring-boot-starter-security'
📌 Gradle (Kotlin DSL)
implementation("org.springframework.boot:spring-boot-starter-security")
💡 여기까지 하면 아무 설정 안 해도 기본 로그인 페이지가 뜨고, 콘솔에 랜덤 비밀번호가 출력됩니다.
반응형
'Spring Security' 카테고리의 다른 글
[Spring Security] 스프링 시큐리티 사용자 인증 구현하기 (3) | 2025.08.13 |
---|---|
[Spring Security] 스프링 시큐리티 적용하기 (0) | 2025.08.13 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 내장객체
- CSS 속성
- Java Server Page
- 미들웨어
- 스프링
- nodejs
- Spring
- Redux
- script element
- Network
- Binding
- javaserverpage
- JSP
- a 태그
- 서브넷팅
- 스프링 시큐리티
- 세션
- CSS
- 리액트
- Spring Security
- react
- Session
- 제이쿼리
- HTML
- Servlet
- el
- 서블릿
- 네트워크
- html css
- httpServletRequest
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함