μŠ€ν”„λ§ μ‹œνλ¦¬ν‹°λ‘œ κ΄€λ¦¬μž, 개발자 κΆŒν•œ μΆ”κ°€ μ„€μ •ν•˜κΈ°

κ°„λ‹¨ν•˜κ²Œ 뢀가적인 κΆŒν•œ 섀정을 ν•΄λ³΄μž.

Secured

일반적으둜 κΈ€λ‘œλ²Œν•œ κΆŒν•œ μ²˜λ¦¬λŠ” SecureConfig 클래슀의 configure() λ©”μ„œλ“œμ—μ„œ 체이닝을 톡해 μ„€μ •ν•œλ‹€. ν•˜μ§€λ§Œ μ–΄λ…Έν…Œμ΄μ…˜λ§Œ μ„€μ •ν•œλ‹€λ©΄ μ»¨νŠΈλ‘€λŸ¬μ—μ„œλ„ 각 μš”μ²­ λ©”μ„œλ“œ 별 κΆŒν•œ 섀정이 κ°€λŠ₯ν•˜λ‹€.

SecureConfig에 μ–΄λ…Έν…Œμ΄μ…˜ μ„€μ •ν•˜κΈ°

클래슀 λ ˆλ²¨μ— @EnableGlobalMethodSecurity μ–΄λ…Έν…Œμ΄μ…˜μ„ μΆ”κ°€ν•œλ‹€.

HTML
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)    // secured μ–΄λ…Έν…Œμ΄μ…˜ ν™œμ„±ν™”, preAuthorize, postAuthorize μ–΄λ…Έν…Œμ΄μ…˜ ν™œμ„±ν™”
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    ...

}
  • securedEnabled = true : 컨트둀러 μš”μ²­ λ©”μ„œλ“œμ—μ„œ @Secured μ–΄λ…Έν…Œμ΄μ…˜μ„ μ„€μ •ν•  수 μžˆλ‹€.
  • prePostEnabled = true : 컨트둀러 μš”μ²­ λ©”μ„œλ“œμ—μ„œ @PreAuthorize, @PostAuthorize μ–΄λ…Έν…Œμ΄μ…˜μ„ μ„€μ •ν•  수 μžˆλ‹€.

컨트둀러 μš”μ²­ λ©”μ„œλ“œ 별 κΆŒν•œ μ„€μ •ν•˜κΈ°

컨트둀러의 μš”μ²­ λ©”μ„œλ“œμ— @Secured, @PreAuthorize @PostAuthorize λ“±μ˜ μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬ 각 μš”μ²­ λ©”μ„œλ“œ 별 κΆŒν•œ 처리λ₯Ό ν•œλ‹€.

@Controller
public class IndexController {

	@Secured("ROLE_ADMIN")
	@GetMapping("/info")
	public @ResponseBody String info() {
		return "개인 정보";
	}
	
	@PreAuthorize("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")     // κΆŒν•œ μ—¬λŸ¬κ°œ μ„€μ •ν•˜κΈ° μœ„ν•΄ 'hasRole() or hasRole()'
	@GetMapping("/data")
	public @ResponseBody String data() {
		return "데이터 정보";
	}
}
  • @Secured : ν•΄λ‹Ή μ–΄λ…Έν…Œμ΄μ…˜μ„ 뢙인 μš”μ²­ λ©”μ„œλ“œμ—λ§Œ κ°„λ‹¨ν•˜κ²Œ κΆŒν•œ μ„€μ •
  • @PreAuthorize : ν•΄λ‹Ή μ–΄λ…Έν…Œμ΄μ…˜μ„ 뢙인 μš”μ²­ λ©”μ„œλ“œκ°€ μ‹€ν–‰λ˜κΈ° 전에 쑰건을 검사
  • 일반적으둜 @PreAuthorize λ³΄λ‹€λŠ” @Secured()둜 κ²€μ‚¬ν•˜λŠ” κ²½μš°κ°€ 많음

이 κ²°κ³Ό κ΄€λ¦¬μž, 개발자 계정 λ³„λ‘œ μ ‘κ·Όν•  수 μžˆλŠ” νŽ˜μ΄μ§€κ°€ 달라진닀!



Reference

μΈν”„λŸ° μ΅œμ£Όν˜Έλ‹˜ μŠ€ν”„λ§λΆ€νŠΈ μ‹œνλ¦¬ν‹° & JWT κ°•μ˜ - μ„Ήμ…˜ 0. μŠ€ν”„λ§ μ‹œνλ¦¬ν‹° κΈ°λ³Έ