Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TIL 1월 24일

whipbaek edited this page Jan 24, 2023 · 12 revisions

김수찬 🤟

Facts

  • somthing..

Feeling

  • somthing..

Finding

  • somthing..

Future (Optional)



김현우 😀

Facts

  • somthing..

Feeling

  • somthing..

Finding

  • somthing..

Future (Optional)



박규현 🤑

Facts

  • somthing..

Feeling

  • somthing..

Finding

  • somthing..

Future (Optional)



백종인 🐭

Facts

  • Spring Security 관련 공부

Feeling

  • 진짜 노답이라 느꼈는데, 하나씩 뜯어서 찾아보다 보니 조금은 알겠다.
  • 아직 할 거 천지인데.. 여기에 시간이 이만큼 뺏기면 안되는데 걱정이 많다.

Finding


In Spring Security

  • 인증(Authentication) : 해당 사용자가 본인이 맞는지를 확인하는 절차.
  • 인가(Authorization) : 인증된 사용자가 요청한 '특정 자원' 에 접근 가능한지를 결정하는 절차
  • Authentication 이 이루어지고 나서, Authorizatino이 이루어진다.

Spring Security Moudules

  • Authentication
    • 현재 접근하는 주체의 정보와 권한을 담은 인터페이스이다. Authentication 객체는 Security Context에 저장되며, SecurityContextHolder를 통하여 SecurityContext에 접근하고, SecurityContext를 통해 Authentication에 접근할 수 있다.
public interface Authentication extends Principal, Serializable {
 
 // 현재 사용자의 권한 목록을 가져옴
 Collection<? extends GrantedAuthority> getAuthorities();
 
 // credential(주로 비밀번호)을 가져옴
 Object getCredentials();
 
 Object getDetails();
 
 // Principal 객체를 가져옴
 Object getPrincipal();
 
 // 인증 여부를 가져옴
 boolean isAuthenticated();
 
 // 인증 여부를 설정함
 void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}
  • SecurityContext
    • Authentication 객체를 보관하는 역할을 한다. SecurityContextHolder를 통해 Authenticatino 객체를 가져올 수 있다.
  • SecurityContextHolder
    • 보안 주체의 세부정부를 포함하여 응용프로개름의 현재 보안 컨텍스트에 대한 세부 정보가 저장된다. image
  • UsernamePasswordAuthenticationToken
    • UsernamePasswordAuthenticationToken은 Authentication을 implements한 AbstractAuthenticationToken의 하위 클래스로, User의 ID가 Principal 역할을 하고, Password가 Credential의 역할을 한다. UsernamePasswordAuthenticationToken의 첫 번째 생성자는 인증 전의 객체를 생성하고, 두번째 생성자는 인증이 완려된 객체를 생성한다.
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
 // 주로 사용자의 ID에 해당함
 private final Object principal;
 // 주로 사용자의 PW에 해당함
 private Object credentials;
 
 // 인증 완료 전의 객체 생성
 public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
		super(null);
		this.principal = principal;
		this.credentials = credentials;
		setAuthenticated(false);
	}
 
 // 인증 완료 후의 객체 생성
 public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
			Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		this.credentials = credentials;
		super.setAuthenticated(true); // must use super, as we override
	}
}
public abstract class AbstractAuthenticationToken implements Authentication, CredentialsContainer {
}

  • AuthenticationProvider
    • AuthenticationProvider에서는 실제 인증에 대한 부분을 처리하는데, 인증 전의 Authentication객체를 받아서 인증이 완료된 객체를 반환하는 역할을 한다. 아래와 같은 AuthenticationProvider 인터페이스를 구현해서 Custom한 AuthenticationProvider을 작성해서 AuthenticationManager에 등록하면 된다.
public interface AuthenticationProvider {
	// 인증 전의 Authenticaion 객체를 받아서 인증된 Authentication 객체를 반환
 Authentication authenticate(Authentication var1) throws AuthenticationException;
 boolean supports(Class<?> var1);
 
}

  • Authentication Manager
    • 인증에 대한 부분은 SpringSecurity의 AuthenticatonManager를 통해서 처리하게 되는데, 실질적으로는 AuthenticationManager에 등록된 AuthenticationProvider에 의해 처리된다. 인증이 성공하면 2번째 생성자를 이용해 인증이 성공한(isAuthenticated=true) 객체를 생성하여 Security Context에 저장한다. 그리고 인증 상태를 유지하기 위해 세션에 보관하며, 인증이 실패한 경우에는 AuthenticationException를 발생시킨다.
    • AuthenticationManager를 implements한 ProviderManager는 실제 인증 과정에 대한 로직을 가지고 있는 AuthenticationProvider를 List로 가지고 있으며, ProviderManager는 loop를 통해 모든 provider를 조회하면서 authenticate 처리를 한다.
public interface AuthenticationManager {
 
 Authentication authenticate(Authentication authentication) throws AuthenticationException;
}

  • UserDetails
    • 인증에 성공하여 생성된 UserDetails는 Authentication 객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용된다. UserDetails 인터페이스를 살펴보면 아래와 같이 정보를 반환하는 메서드를 가지고 있다. UserDetails 를 implements 하여 처리할 수 있다.
public interface UserDetails extends Serializable {
 Collection<? extends GrantedAuthority> getAuthorities();
 
 String getPassword();
 
 String getUsername();
 
 boolean isAccountNotExpried();
 
 boolean isAccountNonLocked();
 
 boolean isCrendentailsNotExpired();
 
 boolean isEnabled();
}

  • UserDetailService
    • UserDetails 객체를 반환하는 단 하나의 메서드를 가지고 있는데 일반적으로 UserRepository에서 주입하여 DB와 연결하여 처리한다.
public interface UserDetailsService {
 UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException
}

image


  • GrantedAuthority
    • GrantAuthority는 현재 사용자(principal)가 가지고 있는 권한을 의미한다. ROLE_ADMIN이나 ROLE_USER와 같이 ROLE_*의 형태로 사용하며, 보통 "roles"라고 한다. GrantedAuthority 객체는 UserDetailsService에 의해 불러올 수 있고, 특정 자원에 대한 권한이 있는지를 검사하여 접근 허용 여부를 결정한다.

  • PasswordEncoding
    • AuthenticationManagerBuilder.userDetailsService().passwordEncoder()를 통해 패스워드 암호화에 사용될 PasswordEncoder 구현체를 지정할 수 있다.
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
	auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder(){
	return new BCryptPasswordEncoder();
}

Future (Optional)



허다은 🐣

Facts

  • somthing..

Feeling

  • somthing..

Finding

  • somthing..

Future (Optional)

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /