MakerHyeon

[SpringBoot] Security 권한 부분 설정 본문

SpringBoot/Security

[SpringBoot] Security 권한 부분 설정

유쾌한고등어 2023. 1. 20. 14:17

[SpringBoot] Security 권한 부분 설정


 

1. 권한 업데이트

# MySQL
update user set role = 'ROLE_MANAGE' where id = 2;
update user set role = 'ROLE_ADMIN' where id = 3;

 

2. SecurityConfig @EnableGlobalMethodSecurity 어노테이션 붙여주기

// Secure,preAuthorize annotation 활성화
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfig {
...

 

3. 메소드별 권한 설정

// IndexController.java

@Secured("ROLE_ADMIN") // 메소드에 권한걸기
@GetMapping("/info")
public @ResponseBody String info(){
    return "개인정보";
}

//메소드 실행직전실행됨.여러 ROLE걸기
@PreAuthorize("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")
@GetMapping("/data")
public @ResponseBody String data(){
    return "데이터정보";
}

 

 

 

 

Comments