1. 목적
특정 경로에 대한 접근제어 설정을 하기 위함
2. 의존성 추가
[ pom.xml ]
<!-- security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3. class file 작성
예시)
import org.springframework.stereotype.Component;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Component
public class Security extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/path/**").access("hasIpAddress('123.123.123.123') or hasIpAddress('123.123.123.0/24') or hasIpAddress('0:0:0:0:0:0:0:1')")
.antMatchers("/path1/paht2/**").access("hasIpAddress('123.123.123.456')");
}
}
4. 비고
-. 보안을 적용하기 위한 메소드
| access(String) | 주어진 SpEL 표현식이 참이면 접근 허용 |
| anonymous() | 익명의 사용자 접근을 허용 |
| authenticated() | 인증된 사용자의 접근을 허용 |
| denyAll() | 무조건 접근 허용하지 않음 |
| fullyAuthenticated() | 사용자가 완전히 인증되면 접근 허용(기억되지 않음) |
| hasAnyAuthority(String...) | 사용자가 주어진 권한 중 어떤 것이라도 있다면 접근허용 |
| hasAnyRole(String...) | 사용자가 주어진 역할 중 어떤 것이라도 있다면 접근허용 |
| hasAuthority(String) | 사용자가 주어진 권한이 있다면 접근허용 |
| hasRole(String) | 사용자가 주어진 역할이 있다면 접근허용 |
| hasIpAddress(String) | 주어진 ip 주소로 오는 요청은 참 |
| not() | 다른 접근 방식의 효과를 무효화 |
| permitAll() | 무조건 접근 허용 |
| rememberMe() | 기억하기를 통해 인증된 사용자의 접근을 허용 |
-. Spring Security 에서 사용 가능한 SpEL
| authentication | 사용자의 인증 객체 |
| denyAll | 항상 거짓으로 평가 |
| hasAnyRole(역할목록) | 사용자가 역할 목록 중 하나라도 해당하면 참 |
| hasRole(역할) | 사용자가 역할이 있는 경우 참 |
| hasIpAddress(주소) | 주어진 ip 주소로 오는 요청은 참 |
| isAnonymous() | 사용자가 익명이면 참 |
| isAuthenticated() | 사용자가 인증된 경우 참 |
| isFullyAuthenticated() | 사용자가 완전히 인증된 경우 참(remember-me 로는 인증되지 않음) |
| isRememverMe() | 사용자가 기억하기(remember-me)로 인증된 경우 참 |
| permitAll | 항상 참 |
| principal | 사용자의 주체 객체 |
