7.4 스프링 시큐리티의 기본 구조
7.4.1 SecurityContext, Authentication, GrantedAuthority
- SecurityContext는 인증, 인가 정보를 관리하는 오브젝트
- SecurityContext는 Authentication 오브젝트를 소유하고 있음
- GrantedAuthority는 인가 정보를 나타내는 오브젝트
7.4.2 AuthenticationManager와 AccessDecisionManager
- AuthenticationManager 오브젝트는 인증 처리를 실시하는 오브젝트
인증: 사용자의 신원을 검증하는 행위로서 보안 프로세스에서 첫 번째 단계
- AccessDecisionManager는 인가 처리를 실시하는 오브젝트
인가: 사용자에게 특정 리소스나 기능에 액세스할 수 있는 권한을 부여하는 프로세스
- 인증된 사용자를 대상으로 정의된 인가 정보를 바탕으로 액세스 제어를 수행하는 것이 AccessDecisionManager의 역할
- 개발자는 AccessDecisionManager를 직접 사용할 기회는 거의 없음
7.5 웹 애플리케이션과 인증
- 메모리에서 인증 정보를 관리하는 방법
@EnableWebSecurity
protected void configure(AuthenticaltionManagerBuilder auth) {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemortAuthentication()
.withUser("user").password("userpassword").authorities("ROLE_USER").and()
.withUser("admin").password("adminpassword").authorities("ROLE_ADMIN");
}
}
- 스프링 시큐리티의 JavaConfig는 WebSecurityConfigureAdapter 클래스를 상속해 작성하는데, 동시에 @EnableWebSecurity 어노테이션도 같이 설정
- 데이터베이스에서 인증 정보를 관리하는 방법
7.5.5 패스워드의 암호화
- configure 메소드 내에서 xxxAuthentication 메소드나 userDetailService 메소드가 실행된 후 password Encoder 메소드를 호출해서 BCryptPasswordEncoder 오브젝트를 인수로 입력
auth.xxxAuthentication() // 또는 userDetailService()
...(생략)...
.passwordEncoder(new BCryptPasswordEncoder());
7.5.6 로그인/로그아웃 기능의 적용
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
...(생략)...
.formLogin()
.defaultSuccessUrl("/top.jsp")
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("top.jsp")
.and()
.csrf().disable();
}
...(생략)...
}
폼 로그인 기능의 설정
- 로그인 처리의 포인트
1. 로그인 화면의 URL은 /login.jsp
2. 로그인 처리를 실행하는 URL은 /processLogin
3. 로그인 성공 후의 이동 화면은 /top.jsp
4. 로그인 실패 시의 이동 화면은 /login.jsp
@Override
proteced void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login.jsp")
.loginProcessingUrl("/processLogin")
.defaultSuccessUrl("/top.jsp")
.failureUrl("/login.jsp")
}
로그아웃 기능의 설정
- 로그아웃의 url에 액세스하면 로그아웃 처리로 SecurityContext 오브젝트가 파기되며, HttpSession 오브젝트에서도 삭제된다.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
...(생략)...
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/top.jsp")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.and()
...(생략)...
}
7.6 웹 애플리케이션과 인가(액세스 제어)
7.6.1 SpEL을 사용한 액세스 제어 정의
7.6.2 URL 단위의 액세스 제어
...(생략)...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(GET, "/top.jsp").permitAll()
.antMatchers(GET, "/login.jsp").permitAll()
.antMatchers(POST, "/processLogin").permitAll()
.antMatchers(POST, "/logout").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").authenticated()
.anyReqiest().denyAll()
.and()
...(생략)...
}
7.6.3 메소드 단위의 액세스 제어
7.6.4 AccessDeniedException 발생 시의 흐름과 에러 핸들링
- URI 단위 또는 메소드 단위로 액세스 제어를 설정한 경우, 액세스 권한이 없을 때 AccessDeniedException이 발생함
http
.exceptionHandling()
.accessDeniedPage("/accessDenied.jsp")
.and()
'개발 > Java & Kotlin' 카테고리의 다른 글
[Java] 기본 자료구조 (0) | 2022.07.08 |
---|---|
[Java] 기본 알고리즘 (0) | 2022.07.07 |
[Spring] 스프링 시큐리티 (1) (0) | 2022.07.04 |
[Spring] 스프링 AOP (2) (0) | 2022.07.04 |
[Java] 람다와 스트림(5) (0) | 2022.07.04 |