선수개념
- 인증(Authentication) : 자신을 증명하는 것
- 인가(Authorization) : 권한에 대한 승인
위는 시큐리티 기본 구조로, 여러 개의 객체가 서로 데이터를 주고 받으며 이뤄진다.
스프링 시큐리티의 핵심 개념은 인증(Authentication)
과 인가(Authorization)
으로 구성된다.
편의점에서 술을 산다고 가정해보자.
1. 사용자는 편의점에서 신분증으로 자신이 성인임을 증명한다.(인증)
2. 편의점 직원은 구매자의 신분을 확인한다.
3. 편의점 직원은 사용자가 주류를 구매할 수 있는 사람인지 판단한다.(인가)
4. 미성년자가 아닐 경우(주류 구매의 권리/권한 보유) 구매자에게 주류를 전달한다.
1 - 인증(Authentication), 자신을 증명
3 - 인가(Authorization), 권한 승인
스프링 시큐리티의 주요 필터
AuthenticationManager의 인증 처리 메서드는 파라미터/리턴 타입 Authentication
으로 동일
인증(Authentication) ≈ 주민번호
```
로그인 과정 예시)
1. 아이디/패스워드로 사용자가 어떤 사람인지 전달
2. AuthenticationManager 통해 전달된 아이디/패스워드로 실제 사용자 검증
3. UsernamePasswordAuthenticationToken이 실제 파라미터로 전달됨
```
스프링 시큐리티 필터의 주요 역할은 인증 관련된 정보를 토큰이라는 객체로 만들어 전달한다는 의미
UsernamePasswordAuthenticationFilter 클래스 코드 中
```java
String username = obtainUsername(request);
username = (username != null) ? username : "";
username = username.trim();
String password = obtaionPassword(request);
password = (password != null) ? password : "";
usernamePasswordAuthenticationToken authRequest =
new usernamePasswordAuthenticationToken(username, password);
// Allow Subclassees to set the "detail" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
```
~Token
객체 생성authenticate()
수행~Provider
내부 UserDetailsService
는 실제 인증 위한 데이터 가져오는 역할AuthenticationProvider와 하위 구현 클래스
AuthenticationManger내 'authentication()' 메서드는 인증 및 권한(Role) 정보 보유
인증/인가 처리
url 접근 -> 인증 필요 여부(필터) 판단 -> 로그인 화면 출력 -> 인증 매니저가 인증 제공자를 찾아 인증 시도 -> (제공자)UserDetailService 구현체 통한 동작
Springboot 2.0부터 지정 필수
BCryptPasswordEncoder 클래스가 가장 일반적
...
String enPw = passwordEncoder.encode(password);
boolean matchResult = password.matches(password, enPw);
System.out.println("Result : "+ matchResult);
...
특정 URL에 접근 제한하는 방식
설정 통해 패턴 지정
어노테이션 통해 적용
CSRF 설정
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
...
http.csrf().disable(); // CSRF 토큰 발행하지 않도록 설정
}
http.
User
로 표현username
사용 (ID)UserDetailsService
로 회원 존재만 우선적으로 가져옴