์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋กœ ๊ด€๋ฆฌ์ž, ๊ฐœ๋ฐœ์ž ๊ถŒํ•œ ์ถ”๊ฐ€ ์„ค์ •ํ•˜๊ธฐ

๊ฐ„๋‹จํ•˜๊ฒŒ ๋ถ€๊ฐ€์ ์ธ ๊ถŒํ•œ ์„ค์ •์„ ํ•ด๋ณด์ž.

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. ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ๊ธฐ๋ณธ