프로젝트 중 Spring - Security 개발을 했다. 심도있게 알아보고 적용하자.
1 ) Spring Security
Spring Security는 Spring 기반의 애플리케이션의 보안(인증과 권한, 인가 등)을 담당하는 스프링 하위 프레임워크이다. Spring Security는 '인증'과 '권한'에 대한 부분을 Filter 흐름에 따라 처리하고 있다. Spring Security는 보안과
관련해서 체계적으로 많은 옵션을 제공해주기 때문에 개발자 입장에서는 일일이 보안관련 로직을 작성하지
않아도 된다는 장점이 있다.
→ ex) 로그인을 하지 않았는데 mypage 페이지의 문제가 생기는 경우
2 ) Spring Security Architecture
3 ) 인증(Authorization)과 인가(Authentication)
● 인증(Authentication) : 해당 사용자가 본인이 맞는지를 확인하는 절차
● 인가(Authorization) : 인증된 사용자가 요청한 자원에 접근 가능한지를 결정하는 절차
→ 기본적으로 인증 절차를 거친 후에 인가 절차를 진행한다.
ex) 사용자가 로그인을 했는데(인증) 관리자 페이지에 접근이 가능한가?(인가)
● Principal(접근 주체) : 보호받는 Resource에 접근하는 대상 즉, 사용자다.
● Credential(비밀번호) : Resource에 접근하려는 대상의 비밀번호 즉, 사용자의 비밀번호
4 ) Spring Security 주요 모듈
Spring Security의 주요 모듈은 아래와 같이 구성된다.
[ SecurityContextHolder ]
SecurityContextHolder는 기본적으로 SecurityContextHolder.MODE_INTHRITABLETHREADLOCAL
방법과 SecurityContextHolder.MODE_THERADLOCAL 방법을 제공한다.
[ SecurityContext ]
Authentication을 보관하는 역할을 하며, SecurityContext를 통해 Authentication객체를 꺼내올 수 있다.
[ Authentication ]
현재 접근하는 주체의 정보(로그인하려는 사용자)와 권한을 담는 인터페이스이다.
Authentication 객체는 Security Context에 저장되며(위의 설명과 같다), SecurityContextHolder를 ㅌ오해
SecurityContext에 접근하며, SecuriyContext를 통해 Authentication에 접근할 수 있다.
[ AuthenticationProvider ]
실제 인증에 대한 부분을 처리한다. 인증 전의 Authentication객체를 받아서 인증이 완료된 객체를
반환하는 역할을 한다. 아래와 같은 AuthenticationProvider 인터페이스를 구현해서 Custom한
AuthenticationProvider을 작성해서 AuthenticationManager에 등록하면 된다.
[ AuthenticationManager ]
인증에 대한 부분은 SpringSecurity의 AuthenticationManager을 통해서 처리하게 된다.
실질적으로 AuthenticationManager에 등록된 AutenticationProvider에 의해 처리된다.
AuthenticationManager를 implements한 Manager는 실제 인증 과정에 대한 로직을 가지고 있는
AuthenticationProvider을 List로 가지고 있는다.
[ UserDetails ]
인증에 성공하여 생성된 UserDetails 객체는 Authentication객체를 구현한 UsernamePasswordAuthentication
Token을 생성하기 위해 사용된다. 사용자가 직접 implements해서 처리할 수 있다.
[ UserDetailService ]
UserDetailService 인터페이스는 UserDetails 객체를 반환하는 단 하나의 메소드를 가지고 있는데,
UserRepository를 주입받아 DB와 연결하여 처리한다. UserDetails 인터페이스는 아래와 같다.
[ Password Encoding ]
AuthenticationManagerBuilder.userDetailsService().passwordEncoder()를 통해 패스워드 암호화에
사용될 PasswordEncoder 구현체를 지정할 수 있다.
[ GrantedAuthority ]
GrantAuthority는 현재 사용자(principal)가 가지고 있는 권한을 의미한다. ROLE_ADMIN이나
ROLE_USER와 같이 ROLE_***** 의 형태로 사용하며, 보통 "roles"라고 한다.
GrantedAuthority 객체는 UserDetailsService에 의해 불러올 수 있고, 특정 자원에 대한 권한이
있는지를 검사하여 접근 허용 여부를 결정한다.
'Spring_SpringBoot > 이론' 카테고리의 다른 글
[ AccessDeniedHandler ] VS [ AuthenticationEntryPoint ] Spring Security (0) | 2024.06.28 |
---|---|
애노테이션(Annotation) - Component, Bean (0) | 2024.06.26 |
[ DTO ] vs [ VO ] (0) | 2024.06.21 |
[JSON] vs [XML] (0) | 2024.06.15 |
[ War ] vs [ Jar ] (0) | 2024.06.15 |