Spring Security를 사용하기 위한 의존 라이브러리 추가 

implementation 'org.springframework.boot:spring-boot-starter-security

 

Configuration 적용(인증방식과 웝페이지에 대한 접근방식 설정을 위함)

import org.springframework.context.annotation.Configuration;

@Configuration
public class SecurityConfiguration {
    // Spring Security의 설정 넣기.
}

 

HTTP 보안 구성 기본 구성하기

@Configuration
public class SecurityConfiguration {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // HttpSecurity를 통해 HTTP 요청에 대한 보안 설정을 구성
         http
            .csrf().disable()                       //  CSRF 보안 미설정        
            .formLogin()                            // 로그인 종류 
            .loginPage("페이지 주소")                // 로그인 페이지 설정
            .loginProcessingUrl("/process_login")   // /process_login로 로그인 요청을 전달하여 내부 인증 프로세스 실행
            .failureUrl("페이지 주소")               // 로그인 인증 실패시 주소 
            .and()                                  // 메서드 체인을 위한 메서드
            .exceptionHandling().accessDeniedPage("페이지 주소")  // 인가 실패시 주소로 이동
            .and()
            .authorizeHttpRequests()                // 클라이언트 요청시 접근 허용 여부  
            .anyRequest()                           // 모든 요청에 대해 
            .permitAll();                           // 전부 접근 허용
    		
            
            .authorizeHttpRequests(authorize -> authorize  //람다식을 이용한 권한부여
                    .antMatchers("주소/1/**").hasRole("ADMIN") //주소/1(하위포함)은 ADMIN만 접근 가능
                    .antMatchers("주소/2/*").hasRole("USER")  //주소/2(깊이 1의 하위만) USER만 접근 가능
                    .antMatchers("/**").permitAll()          // 위 URL 이외 나머진 전부 접근 가능
    }

 

'Spring Framework > Spring Security' 카테고리의 다른 글

Spring Security 란?  (0) 2022.09.23

Spring Security 란?

Spring MVC 기반 어플리케이션에서 지원 하는 프레임 워크

인증(Authentication)과 인가(Authorization) 기능 지원

 

Spring에서 Interceptor나 Servlet Filter 도 지원하고 있어 직접 구현도 가능하나 Spring Security에서 잘 만들어진 구조의  Spring Security 사용 가능

 

Spring Security에서 지원 하는 보안 기능

사용자 인증 기능 적용(폼 로그인 , 토큰 기반, OAuth2, LDAP, 클라이언트 기반)

사용자 역할(Role)에 따른 권한 적용 가능 

리소스 접근 제어 

데이터 암호화 

SSL 

웹 보안 공격 차단

 

Spring Security 적용 방법

의존 라이브러리 추가(build.gradle)

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-security'
}

'Spring Framework > Spring Security' 카테고리의 다른 글

Spring Security 기본 설정  (0) 2022.09.23

+ Recent posts