본문으로 바로가기

Spring Boot 기반으로 개발하는 Spring Security #0

목차

    스프링 시큐리티의 의존성 추가 시 일어나는 일

    • 서버가 기동되면 스프링 시큐리티의 초기화 작업 및 보안 설정이 이루어짐
    • 별도의 설정이나 구현을 하지 않아도 기본적인 웹 보안 기능이 현재 시스템에 연동되어 작동
      • 모든 요청은 인증이 되어야 자원에 접근 가능.
      • 인증 방식은 폼 로그인 방식과 httpBasic 로그인 방식을 제공
      • 기본 로그인 페이지 제공
      • 기본 계정 한 개를 제공

    추가 목표

    • 계정 추가, 권한 추가, DB 연동 등.
    • 기본적인 보안 기능 외에 시스템에서 필요로 하는 더 세부적이고 추가적인 보안기능이 필요.

    사용자 정의 보안 기능 구현

    • WebSecurityConfigurerAdapter 클래스
      • 스프링 시큐리티의 웹 보안 기능 초기화 및 설정. (가장 기본적인 초기 설정)
      • HttpSecurity 생성.
    • HttpSecurity 클래스
      • 세부적인 보안 기능을 설정할 수 있는 API 제공.
      • 인증 API
        • http.formLogin()
        • http.logout()
        • http.csrf()
        • http.httpBasic()
        • http.SessionManagement()
        • http.RememberMe()
        • http.ExceptionHandling()
        • http.addFilter()
      • 인가 API
        • http.authorizeRequests()
        • http.antMatchers("/admin")
        • http.hasRole(USER)
        • http.permitAll()
        • http.authenticated()
        • http.fullyAuthentication()
        • http.access(hasRole(USER))
        • http.denyAll()

    스프링 시큐리티에서 제공되는 기본 설정

    	private void applyDefaultConfiguration(HttpSecurity http) throws Exception {
    		http.csrf();
    		http.addFilter(new WebAsyncManagerIntegrationFilter());
    		http.exceptionHandling();
    		http.headers();
    		http.sessionManagement();
    		http.securityContext();
    		http.requestCache();
    		http.anonymous();
    		http.servletApi();
    		http.apply(new DefaultLoginPageConfigurer<>());
    		http.logout();
    	}

    Form Login 인증

            http.formLogin() // Form 로그인 인증 기능 작동
                    .loginPage() // 사용자 정의 로그인 페이지
                    .defaultSuccessUrl() // 로그인 성공 후 이동 페이지
                    .failureUrl() // 로그인 실패 후 이동 페이지
                    .usernameParameter() // 아이디 파라메터명 설정
                    .passwordParameter() // 패스워드 파라메터명 설정
                    .loginProcessingUrl() // 로그인 Form Action Url
                    .successHandler() // 로그인 성공 핸들러
                    .failureHandler() // 로그인 실패 핸들러