1. 기본 용어
1. Authentication
- 인증
- 로그인
- 유저 식별
2. Authorize
- 인가
- 접근 권한
- 인증된 유저의 권한
3. Principal
- 접근주체(객체)
- Spring Security에서 사용하는 인증, 인가 정보가 있는 객체
4. Credentials
- 리소스에 접근하는 대상의 비밀번호
2. Security 설정
1. Spring Security API POM.XML 추가
1) 프로젝트 생성시 Security API 선택
2) 기존 프로젝트에 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. Security 추가 후 비 활성화
main 메서드가 있는 클래스의 선언부에 exclude = SecurityAutoConfiguration.class 추
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
#Security 비 활성화
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
3. DB 구성
Member
ROLE
- role의 이름 구성 전부 대문자
- ROLE_***
MEMBER_ROLE
4. Security Config
Spring Security 5.7.0-M2 부터 WebSecurityConfigureAdapter는 Deprecated 됨
대신 SecurityFilterChain 을 Bean으로 등록하는 방식을 권장
1. WebSecurity 설정 클래스 생성
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
//public 을 선언하면 default로 바꾸라는 메세지 출력
WebSecurityCustomizer webSecurityConfig() {
//Security에서 무시해야하는 URL 패턴 등록
return web -> web
.ignoring()
.antMatchers("/images/**")
.antMatchers("/css/**")
.antMatchers("/js/**")
.antMatchers("/favicon/**")
.antMatchers("/resources/**");
}
@Bean
SecurityFilterChain fiterChain(HttpSecurity httpSecurity)throws Exception{
httpSecurity
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/manager").hasAnyRole("ADMIN","MANAGER")
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll();
return httpSecurity.build();
}
}
authorizeRequests()
요청 URL과 그에 관한 권한 설정 하는 곳
요청 URL 정보
antMatchers("요청 URL")
antMatchers("요청 URL", "요청 URL", ...)
해당URL의 권한 설정
- DB에는 Role명이 ROLE_NAME
- hasRole(ROLE_를제외한 나머지이름)
hasRole("권한이름")
hasAnyRole("권한이름1", "권한이름2",...) : 여러개의 권한 중 하나만 있어도 통과
- access("hasRole('ROLE_포함 전체이름')")
access("hasRole('ROLE_ADMIN')")
access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
permitAll() : 누구나 허용
denyAll() : 누구나 차단
anonymous() : 익명사용자 허용
hasAuthority("권한이름")
hasAnyAuthority("권한이름", "권한이름2",...)
authenticated() : 인증된 사용자 허용
formLogin()
5. UserDetails
Spring Security에서 제공하는 VO개념, Interface
1. 새로운 VO를 만들고 UserDetail 구현
public class MemberVO Implements UserDetails
2. 새로운 VO를 만들고 UserDetail 구현
MemberVO를 새로운 VO에 멤버변수로 활용
public class User implements UserDetails{
private MemberVO memberVo
}
6. UserDetailsService