IT/자바, 스프링
Spring Security에서 컨트롤러 메서드 별 권한 지정 (@Secured, @PreAuthorize)
thesse
2021. 10. 22. 16:11
300x250
반응형
원래 antMatchers로 지정하던 권한
@Secured와 @PreAuthorize 어노테이션으로 그자리에서 지정하기
기존
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http
//...
.and()
.authorizeRequests()
.antMatchers("/auth/test").hasRole("USER") // 이렇게 config에서 모아서 지정하는 방식
.anyRequest().permitAll()
// ...
}
}
어노테이션 사용
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) // 추가
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}
@PostMapping("/test")
@Secured("ROLE_STREAMER") // 추가
public String test(){
...
}
@PostMapping("/test2")
@PreAuthorize("hasRole('ROLE_USER')") // 추가
public String test2(){
...
}
300x250
반응형