https://spring.io/guides/topicals/spring-security-architecture

 

Spring Security Architecture

this topical is designed to be read and comprehended in under an hour, it provides broad coverage of a topic that is possibly nuanced or requires deeper understanding than you would get from a getting started guide

spring.io

주의 사항

  1. 틀렸을 확률이 매우 큽니다.
  2. 영어를 못해서 해석이 잘못되어 있을 수도 있습니다.
  3. 뭔가 멋있어 보이려고 위에 문서에서 발취해 왔습니다.
  4. 간략히 정리한 글입니다.
  5. 계속해서 수정할 것입니다. :)

Authentication and Access Control

 

Authentication - 권한

인증을 제공하기 위한 주요 인터페이스 전략은 AuthenticiationManager입니다.

 

public interface AuthenticationManager {

  Authentication authenticate(Authentication authentication)
    throws AuthenticationException;
}

 

AuthenticationManager의 주요 기능 3가지 :

  • AuthenticationManager은 유효한 계정을 입력할 경우 인증된 계정을 리턴한다. (DB에 있는 데이터?)
  • 유효하지 않는 계정을 입력할 경우 AuthenticationException을 반환한다. 
  • 정보가 없다면 null을 반환한다.

 - 짧은 정리

DB안에 입력받은 계정의 정보가 있다면 그 어떤 

 

AuthenticationException 특징

  • 401을 반환
  • AuthenticationException은 런타임 오류

 

Customizing Authentication Managers

스프링 시큐리티는 어플리케이션에 authentication manager의 기능을 빠르게 만들 수 있는 환경 설정을 제공해줍니다.

 

 

@Configuration
@EnableWebSecurity // 스프링 시큐리티 필더가 스프링 필터체인에 등록이 됩니다.
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	// 해당 메서드의 리턴되는 오브젝트를 IoC로 등록해준다.
	@Bean
	public BCryptPasswordEncoder encodePassWd() {
		return new BCryptPasswordEncoder();
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
		http.authorizeRequests()
		.antMatchers("/user/**").authenticated()
		.antMatchers("/manager/**").access("hasRole('ROEL_ADMIN') or hasRole('ROLE_MANAGER')")
		.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
		.anyRequest().permitAll()
		.and().formLogin().loginPage("/loginForm")
		.loginProcessingUrl("/login")
		.loginProcessingUrl("/")
		;
	}
	// 로그인을 하지 않으면 /user로 들어가는 경로는 403페이지 오류가 뜸 (권한이 없다는 뜻)
}

 

Authorization - 허가

권한이 부여된 사용자만이 특정 URL을 접속할 수 있도록 도와줍니다. 로그인이 성공적으로 이루어 진다면 AccessDecisionManager가 허가의 여부를 판단합니다. 보통은 정의하지 않고 디폴트로 사용합니다.

 

 

특별한 명명 규칙이 있습니다 

1. ROLE_ADMIN or ROLE_ADMIN처럼 접두사에 ROLE_ 처럼 사용할 수 있습니다.

2. SpEL 표현식도 사용 가능합니다. 예를들어 isFullyAuthenticated() 나 hasRole('user')와 같이 사용할 수 있습니다.

access("hasRole('ROEL_ADMIN') or hasRole('ROLE_MANAGER')")
access("hasRole('ROLE_ADMIN')")

 

+ Recent posts