728x90
반응형
SecurtyConfig 설정 1
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll();
}
}
@Configuration : spring의 환경 설정과 관련된 파일이라는 것을 알림. 메모리에 올려준다.
@EnableWebSecurity (스프링 시큐리티 사용을 위한 어노테이션)
- springSecurityFilterChain이 스프링 필터체인에 자동으로 등록된다
(=SecurityConfig(내가 지금부터 등록할 필터)가 기본 필터 체인에 등록된다)
WebSecurityConfigureAdapter
- 사용자 지정 자체 보안 구성을 원할 때 상속받아 사용하는 클래스
- 해당 클래스를 상속 받아 구현하게 되면 기본 자동 구성이 비활성화되고, 사용자 지정 보안 구성이 활성화된다.
- @EnableWebSecurity와 웹 기반 보안을 위해 같이 사용된다.
- WebSecurity와 HttpSecurity를 사용자가 원하는대로 지정할 수 있도록 해준다
configure(WebSecurity) / configure(HttpSecurity)
- configure(WebSecurity)
- 특정 요청들을 무시하고 싶을 때 사용
- configure(HttpSecurity)
- 특정 http 요청에 대해 웹 기반 보안을 구성할 수 있다
csrf(Cross Site Request Forgery)
- 사용자의 의지와는 무관한 수정/삭제/등록 등의 행위를 요청하는 것
.antMatchers()로 지정할 수 있는 항목
.authenticated() : 인증된 사용자에게만 접근 허용
.access("hasRole('ROLE_ADMIN')") : 특정 권한을 가지는 사용자에게만 접근 허용
.permitAll() : 모든 요청에 대해 접근 허용
.denyAll() : 모든 요청에 대해 접근 제한
.anonymous() : 인증되지 않은 사용자에게 접근 허용
.hasIpAddress() : 특정 ip 주소를 가지는 사용자에게만 접근 허용
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
-> 'ROLE_ADMIN'이 아닌 사용자가 접근 요청을 했을 경우 403 접근 권한 에러 발생
SecurtyConfig 설정 2
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/loginForm")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/");
}
}
.formLogin().loginPage("/loginForm")
- form을 통한 login 활성화. login 페이지에 접근했을 때 사용자에게 보여줄 페이지 url 지정
- 권한이 없는 페이지로 요청이 들어갈 때, 로그인 페이지로 이동된다
.loginProcessingUrl("/login")
- login 주소가 호출되면 시큐리티가 낚아채서 대신 로그인을 진행해준다.(controller에 /login을 만들지 않아도 된다)
- 로그인 form의 action과 일치시켜 줘야 한다.
.defaultSuccessUrl("/")
- 로그인 성공 시 '/' url 요청
@EnableGlobalMethodSecurity
- securedEnabled = true : @Secured 어노테이션 활성화
- prePostEnabled = true : @PreAuthorize, @PostAuthorize 어노테이션 활성화
728x90
반응형
'웹 프로그래밍 > Spring' 카테고리의 다른 글
[Spring] DTO의 @Builder, @RequestBody와 생성자 (0) | 2022.03.10 |
---|---|
[Spring] Spring Validator BindingReuslt/Errors 사용 (0) | 2021.08.24 |
[Spring Security] #1 Spring Security 시작하기 (0) | 2021.05.20 |
[Spring Security] Spring Security란? (0) | 2021.05.14 |
[JPA] JPA(Java Persistence API)란? (0) | 2021.05.04 |