# [Security] ๊ถŒํ•œ ์ฒ˜๋ฆฌ @PreAuthorize, @PostAuthorize, @Secured
Study Repository

[Security] ๊ถŒํ•œ ์ฒ˜๋ฆฌ @PreAuthorize, @PostAuthorize, @Secured

by rlaehddnd0422

Spring Security๋Š” Filter๋ฅผ ํ†ตํ•ด ๊ถŒํ•œ์„ ๊ฒ€์‚ฌํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ถ”๊ฐ€์ ์œผ๋กœ ํŠน์ • URI์— ์ ‘๊ทผํ•  ๋•Œ ๊ถŒํ•œ์„ ๊ฒ€์‚ฌํ•˜์—ฌ ํŠน์ • ๊ถŒํ•œ๋งŒ ํ•ด๋‹น URI์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ„๋„์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋Ÿฐ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ธฐ์— ์•ž์„œ @Configuration์ด ๋ถ™์€ ์„ค์ •ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด @EnableGlobalMethodSecurity ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋ณ„๋„ ๊ถŒํ•œ์ฒ˜๋ฆฌ ์–ด๋…ธํ…Œ์ด์…˜์˜ ํ™œ์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@EnableGlobalMethodSecurity

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, // @Secured ํ™œ์„ฑํ™”
                            prePostEnabled = true // @PreAuthorized ํ™œ์„ฑํ™”, @PostAuthroized ํ™œ์„ฑํ™”
)
public class SecurityConfig {
  • securedEnabled : @Secured ์–ด๋…ธํ…Œ์ด์…˜ ํ™œ์„ฑํ™” ์—ฌ๋ถ€
  • prePostEnabled : @PreAuthorized, @PostAuthorized ์–ด๋…ธํ…Œ์ด์…˜ ํ™œ์„ฑํ™” ์—ฌ๋ถ€

@Secured

@Secured(value = "ROLE_ADMIN")
@GetMapping("/info")
@ResponseBody
public String info() {
    return "๊ฐœ์ธ์ •๋ณด";
}

์ผ๋ฐ˜์ ์œผ๋กœ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์†Œ๋“œ ๋ ˆ๋ฒจ์— ๋ถ™์—ฌ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, @Secured ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ปจํŠธ๋กค๋Ÿฌ์— ๋งคํ•‘๋œ URI๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ๋ฉ”์†Œ๋“œ ์‹คํ–‰ ์ „, @Secured์— value์˜ ๊ถŒํ•œ์„ ๊ฐ–๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋จผ์ € ํ™•์ธํ•˜๊ณ , ๊ถŒํ•œ์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

@PreAuthroize

// 2๊ฐœ ์ด์ƒ ๊ฑธ๊ณ ์‹ถ์„๋•Œ
@PreAuthorize("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")
@GetMapping("/data")
@ResponseBody
public String data() {
    return "๋ฐ์ดํ„ฐ์ •๋ณด";
}

@Secured ์™€ ๋‹ฌ๋ฆฌ ์กฐ๊ฑด์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผ ๋ ˆ๋ฒจ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

+ ์กฐ๊ฑด์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— AND, OR์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผ๋ ˆ๋ฒจ์„ ์—ฌ๋Ÿฌ ๊ฐœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ‘œํ˜„์‹ ์ข…๋ฅ˜

  • hasRole([role]) : ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์ด ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ถŒํ•œ๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ true
  • hasAnyRole([role1,role2]) : ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ๋”” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ถŒํ•œ ์ค‘ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š” ๊ฒฝ์šฐ true
  • principal : ์‚ฌ์šฉ์ž๋ฅผ ์ฆ๋ช…ํ•˜๋Š” ์ฃผ์š”๊ฐ์ฒด(User)๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • authentication : SecurityContext์— ์žˆ๋Š” authentication ๊ฐ์ฒด์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • permitAll : ๋ชจ๋“  ์ ‘๊ทผ ํ—ˆ์šฉ
  • denyAll : ๋ชจ๋“  ์ ‘๊ทผ ๋น„ํ—ˆ์šฉ
  • isAnonymous() : ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ต๋ช…(๋น„๋กœ๊ทธ์ธ)์ธ ์ƒํƒœ์ธ ๊ฒฝ์šฐ true
  • isRememberMe() : ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ RememberMe ์‚ฌ์šฉ์ž๋ผ๋ฉด true
  • isAuthenticated() : ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ต๋ช…์ด ์•„๋‹ˆ๋ผ๋ฉด (๋กœ๊ทธ์ธ ์ƒํƒœ๋ผ๋ฉด) true
  • isFullyAuthenticated() : ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ต๋ช…์ด๊ฑฐ๋‚˜ RememberMe ์‚ฌ์šฉ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด true

@PostAuthorize

@PostAuthorize("isAuthenticated() and (( returnObject.name == principal.name ) or hasRole('ROLE_ADMIN'))")
@RequestMapping( value = "/{id}", method = RequestMethod.GET )
public Project getProject( @PathVariable("id") long id ){
    return service.findOne(id);
}

@PostAuthorize ์–ด๋…ธํ…Œ์ด์…˜์€ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์„ ํ•˜๊ธฐ ์ง์ „์— ๊ถŒํ•œ์„ ๊ฒ€์‚ฌํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ์ž…๋‹ˆ๋‹ค.

returnObject ์˜ˆ์•ฝ์–ด๋ฅผ ํ†ตํ•ด ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<์ฐธ๊ณ  ์ž๋ฃŒ>

https://ggotae.tistory.com/entry/Secured-PreAuthorize-%EB%B9%84%EA%B5%90

 

@Secured, @PreAuthorize ๋น„๊ต

@Secured๋Š” ํ‘œํ˜„์‹ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ @PreAuthroize๋Š” ํ‘œํ˜„์‹ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์˜ˆ)@Secured({"ROLE_USER","ROLE_ADMIN"}) => OR ์กฐ๊ฑด, AND ์กฐ๊ฑด ๋ถˆ๊ฐ€๋Šฅ @PreAuthorize("hasRole('ROLE_USER') and hasRole('ROLE_ADMIN')") => and ์กฐ๊ฑด, or ์กฐ๊ฑด ๋ชจ๋‘

ggotae.tistory.com

https://steemit.com/kr-dev/@igna84/spring-security-preauthorize-postauthorize

 

https://bigdown.tistory.com/465

 

@Secured, @PreAuthorize, @PostAuthorize

ํ™œ์„ฑํ™” ๋ฐฉ๋ฒ• @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter {...} @EnableGlobalMethodSecurity ์–ด๋…ธํ…Œ์ด์…˜์˜ ์†์„ฑ์œผ๋กœ

bigdown.tistory.com

 

 

[Spring Security] @PostAuthorize ์–ด๋…ธํ…Œ์ด์…˜

์ด ๋…€์„๋„ ์•„์ฃผ ์œ ์šฉํ•œ ์• ๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค. @PreAuthorize๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ๊ถŒํ•œ์„ ํ™•์ธํ•˜์ง€๋งŒ, ์ฐจ์ด์ ์€ ๋ฉ”์„œ...

blog.naver.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ