[ webSecurityConfigurerAdapter를 상속한 sercurityConfig ]
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 추가
// secured 어노테이션 활성화 & preAuthorize/PostAuthorize 어노테이션 활성화
package com.cos.security1.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @Configuration @EnableWebSecurity // 활성화 - 스프링 시큐리티 필터가 스프링 필터체인에 등록이 된다 @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) // secured 어노테이션 활성화 & preAuthorize/PostAuthorize 어노테이션 활성화 public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); // /login으로 갈때 스프링 시큐리티가 낚아채지 않도록 http.authorizeRequests() .antMatchers("/user/**").authenticated() .antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')") .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .anyRequest().permitAll()// 다른주소는 OK .and() .formLogin() .loginPage("/loginForm") // 로그인안햇을때 403페이지가 뜨는게 아니라 login페이지를 뜨게만들어줌 .loginProcessingUrl("/login") //login 주소가 호출이 되면 시큐리티가 낚아채서 대신 로그인을 진행 .defaultSuccessUrl("/"); // 로그인이 완료되면 이동할 주소 } @Bean // 해당 메서드의 리턴되는 오브젝트를 ioc로 등록 public BCryptPasswordEncoder encodePwd(){ // 비밀번호 암호화를 위해 return new BCryptPasswordEncoder(); } }
[ Contoller ]
postAuthorize는 잘 사용하지 않는다.
실행시점?의 차이.
@Secured("ROLE_ADMIN") @GetMapping("/info") public @ResponseBody String info(){ return "개인정보"; } @PreAuthorize("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')") // @PostAuthorize() @GetMapping("/data") public @ResponseBody String data(){ return "데이터정보"; }
'spring' 카테고리의 다른 글
[spring security] OAuth2.0 기본 - 구글 로그인 준비 (0) | 2022.01.17 |
---|---|
[Spring Security] 스프링 시큐리티 2. 로그인 (0) | 2022.01.16 |
[Spring Security] 스프링 시큐리티 1. 기본설정 (0) | 2022.01.16 |
[스프링부트] vs code 사용하기 메모 (0) | 2021.03.08 |
[스프링] 웹소켓 : 채팅 (0) | 2021.01.29 |
[스프링] 메일발송하기 (0) | 2021.01.25 |
[스프링] REST API / @RequestBody @ResponseBody , HttpMessageConverter (0) | 2021.01.20 |