๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring/Spring Security

Spring Security ์‹ค์Šต1 - url์„ค๊ณ„๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ ์ œํ•œ ๋ฐฉ๋ฒ•

by ํƒœ์˜น 2021. 8. 20.

๐Ÿ“Œ ํ•ด๋‹น ์‹ค์Šต์„ ํ•˜๊ธฐ ์ „ ์•„๋ž˜์˜ ๊ฒŒ์‹œ๋ฌผ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์„ค์ •์„ ์™„๋ฃŒํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

2021.08.19 - [Spring/๊ฐœ๋…์ง€์‹ ๋ฐ ์—๋Ÿฌ์‚ฌํ•ญ] - [Spring] Spring Web Security ์„ค์ •ํ•˜๊ธฐ

 

[Spring] Spring Web Security ์„ค์ •ํ•˜๊ธฐ

๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์›น์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฟ ํ‚ค๋ฅผ ์ด์š”ํ•˜๊ฑฐ๋‚˜ ์„ธ์…˜์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ์Šคํ”„๋ง์—์„œ๋Š” ์ด๋ฅผ Interceptor ๋“ฑ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. Spring Web Security

taetoungs-branch.tistory.com


 

์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ํ•„์š”ํ•œ URL ์„ค๊ณ„

 

์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์–ดํ•ด์•ผํ•˜๋Š” URL์„ ์„ค๊ณ„ํ•˜๋Š” ์ž‘์—…์„ ์‹ค์Šตํ•ด๋ณด์ž.

1. ๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ URL โž” /sample/all

2. ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž๋“ค์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” URL โž”  /sample/member

3. ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž๋“ค ์ค‘์—์„œ๋„ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋งŒ์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” URL โž”  /sample/admin

 

์œ„์˜ ์„ธ๊ฐ€์ง€ ๊ทœ์น™์— ๋งž๊ฒŒ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ•œ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค.

 

SampleController.java

package com.taeong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;

@Controller
@Log4j
@RequestMapping("/sample/*")
@AllArgsConstructor
public class SampleController {
	
	@GetMapping("/all")
	public void doAll() {
		log.info("do all can access everybody.");
	}
	@GetMapping("/member")
	public void doMember() {
		log.info("logined member");
	}
	@GetMapping("/admin")
	public void doAdmin() {
		log.info("admin only");
	}
}

url์— ๋งž๋Š” ๊ฐ๊ฐ์˜ ํ™”๋ฉด์„ ์ž‘์„ฑํ•œ๋‹ค. (jspํŒŒ์ผ์ด ์„ธ ๊ฐœ ๋‚˜์˜ค๋ฉด ๋จ)

 

 

url์— ํ•ด๋‹นํ•˜๋Š” ํŽ˜์ด์ง€๋ณ„๋กœ ์ ‘๊ทผํ•˜๋Š” ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ ๋‚ด์šฉ์„ ํ•™์Šตํ•ด์•ผ ํ•œ๋‹ค.

(์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์šฉ์–ด๋“ค์ด๋ผ๊ณ  ํ•œ๋‹ค.)

 

Authentication(์ธ์ฆ) : ์ž์‹ ์„ ์ฆ๋ช…ํ•˜๋Š” ๊ฒƒ

Authorization(๊ถŒํ•œ) : ๋‚จ์—๊ฒŒ ์ž๊ฒฉ์„ ๋ถ€์—ฌ๋ฐ›๋Š” ๊ฒƒ

 

๊ทธ๋‹ˆ๊นŒ... 'Authentication'์€ ๋กœ๊ทธ์ธ๊ฐ™์ด ๋‚ด๊ฐ€ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์ฆ๋ช…ํ•˜๋Š” ์ž‘์—…์ด๊ณ ,

'Authorization'๋Š” ๋‚ด๊ฐ€ ๋งŒ์•ฝ ๊ด€๋ฆฌ์ž์ธ ๊ฒฝ์šฐ, ๊ด€๋ฆฌ์ž๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฐ›๋Š” ๊ฒƒ!

 

 

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—๋Š” ์ธ์ฆ ๋‹ด๋‹น์ž ์—ญํ• ์„ ํ•˜๋Š” AuthenticationManager(์ธ์ฆ ๋งค๋‹ˆ์ €)๊ฐ€ ์žˆ๋‹ค.

์—ฌ๋Ÿฌ provider๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ProviderManager๋Š” ์ธ์ฆ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ AuthenticationProvider(์ธ์ฆ ์ œ๊ณต์ž)๋ผ๋Š” ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•œ๋‹ค.

 

AuthenticationProvider๋Š” ์‹ค์ œ ์ธ์ฆ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.

์ด๋•Œ, ์ธ์ฆ๋œ ์ •๋ณด์—๋Š” ๊ถŒํ•œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ™์ด ์ „๋‹ฌํ•˜๊ฒŒ ๋˜๋Š”๋ฐ(์ผ๋ฐ˜ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ์™€ ๊ด€๋ฆฌ์ž์ธ ๊ฒฝ์šฐ์˜ ๊ถŒํ•œ์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฒƒ ์ฒ˜๋Ÿผ ์ธ์ฆ ์ •๋ณด๋งˆ๋‹ค ๊ถŒํ•œ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)  ์ด ์ฒ˜๋ฆฌ๋Š” UserDetailsService๊ฐ€ ์ˆ˜ํ–‰ํ•œ๋‹ค.

UserDetailsService๋Š” ์‚ฌ์šฉ์ž์˜ ์ •๋ณด์™€ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ€์ง„ ๊ถŒํ•œ์˜ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

 

 

๊ฐœ๋ฐœ์ž๊ฐ€ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜๋Š” ๋ฐฉ์‹

1. UserDetailsService๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹ - ์‹ค์ œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹น

2. AuthenticationProvider๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹

-> ์ƒˆ๋กœ์šด ํ”„๋กœํ† ์ฝœ์ด๋‚˜ ์ธ์ฆ ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉ

 

 


 

ํ•ด๋‹น ๊ฒŒ์‹œ๋ฌผ์—์„œ ์‹ค์Šตํ•ด๋ณผ ๊ฒƒ์€ ๋ญ”๊ฐ€ ๊ทธ๋‹ค์ง€ ๋ฉ‹์žˆ์ง€๋Š” ์•Š์ง€๋งŒ๐Ÿคท‍โ™€๏ธ

์•ฝ๊ฐ„์˜ ์„ค์ •๋งŒ์œผ๋กœ ๋กœ๊ทธ์ธ๊ณผ ์ ‘๊ทผ์ œํ•œ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. 

 

 

security-context.xml์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผ ์ œํ•œ์„ ์„ค์ •ํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<security:http>
	<security:intercept-url pattern="/sample/all" access="permitAll"/>
	<security:intercept-url pattern="/sample/member" access="hasRole('ROLE_MEMBER')"/>
		<security:form-login />
	</security:http>
	<security:authentication-manager>
	</security:authentication-manager>
</beans>

<security:intercept-url> : ํŠน์ •ํ•œ URL์— ์ ‘๊ทผํ•  ๋•Œ ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๋Š” ์„ค์ •์„ ํ•˜๋Š” ๊ฒฝ์šฐ ์ด์šฉ

pattern : url์˜ ํŒจํ„ด

access : ๊ถŒํ•œ ์ฒดํฌ

๋‘ ์†์„ฑ์€ ๋ฐ˜๋“œ์‹œ <security:intercept-url>์—์„œ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ํ•„์ˆ˜ ์†์„ฑ์ด๋ผ๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด๋‘์ž!

 

์œ„์˜ ์ฝ”๋“œ์˜ ์ผ๋ถ€๋ถ„์„ ์„ค๋ช…ํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‚ด์šฉ์ด๋‹ค.

<security:intercept-url pattern="/sample/member" access="hasRole('ROLE_MEMBER')"/>

== '/sample/member'๋ผ๋Š” url์€ ROLE_MEMBER๋ผ๋Š” ๊ถŒํ•œ์ด ์žˆ๋Š” ์‚ฌ์šฉ์ž๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

access์˜ ์†์„ฑ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

1) ํ‘œํ˜„์‹ โž” ์‹ค์Šต์—์„œ๋Š” ํ‘œํ˜„์‹์„ ์ด์šฉํ•œ๋‹ค.

2) ๊ถŒํ•œ๋ช…(๋‹จ์ˆœ ๋ฌธ์ž์—ด)

 

๐Ÿ‘‡ ์‹ค์Šต์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์ง€๋งŒ access์˜ ์†์„ฑ๊ฐ’์œผ๋กœ ๋‹จ์ˆœ ๋ฌธ์ž์—ด์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ

๋”๋ณด๊ธฐ

์•„๋ž˜์™€ ๊ฐ™์ด use-expressions๋ฅผ false๋กœ ์ง€์ •ํ•ด์„œ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค.

(<security:http>๋Š” ๊ธฐ๋ณธ ์„ค์ •์ด ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ)

<security:http auto-config="true" use-expressions="false">
    <security:intercept-url pattern="/sample/userPage" access="ROLE_MEMBER" />
    <security:intercept-url pattern="/sample/adminPage" access="ROLE_ADMIN" />

๊ทธ๋Ÿฌ๋‚˜ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ๊ถŒ์žฅ๋˜๋ฏ€๋กœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

security-context.xml์—์„œ url์— ๋”ฐ๋ผ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋‹ค๋ฅด๊ฒŒ ์ง€์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ ์„œ๋ฒ„๋ฅผ ๋Œ๋ ค์„œ /all์™€ /member์— ์ ‘๊ทผํ•ด๋ณด๋ฉด ๋‹ค๋ฅธ ์ƒํ™ฉ์ด ์ผ์–ด๋‚จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

/sample/all๋กœ ์ ‘๊ทผํ•œ ๊ฒฝ์šฐ

/sample/member๋กœ ์ ‘๊ทผํ•œ ๊ฒฝ์šฐ

/sample/member๋Š” ๋กœ๊ทธ์ธํŽ˜์ด์ง€(/login)๋กœ ๊ฐ•์ œ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

(์šฐ๋ฆฌ๋Š” /login์ปจํŠธ๋กค๋Ÿฌ๋‚˜ jsp๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์ง€ ์•Š์•˜๋‹ค!๐Ÿ˜ฎ)

์ด๋กœ์จ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋Š” ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํŽ˜์ด์ง€๋ผ๋Š” ์ ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์•„์ง ์šฐ๋ฆฌ๋Š” ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€์ ์ธ ์„ค์ •์„ ํ†ตํ•ด ์ง€์ •ํ•œ ์•„์ด๋””์™€ ํŒจ์Šค์›Œ๋“œ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด์–ด์„œ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ๋‹ค.

 


๋‹จ์ˆœ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—์„œ์˜ ์˜๋ฏธ ์ฐจ์ด

(์ผ๋ฐ˜์ ์ธ ์‹œ์Šคํ…œ์—์„œ์˜ ์˜๋ฏธ์™€ ๋‹ค๋ฅด๋‹ˆ ์ฃผ์˜ํ•  ๊ฒƒ!)

username : ์ผ๋ฐ˜ ์‹œ์Šคํ…œ์—์„œ์˜ userid์™€ ๊ฐ™์Œ (!= ์œ ์ €๋ช…)

user : ์ธ์ฆ ์ •๋ณด์™€ ๊ถŒํ•œ์„ ๊ฐ€์ง„ ๊ฐ์ฒด (!= ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด)

 

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ๋‹จ์ˆœํžˆ ๋กœ๊ทธ์ธ์ด ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

๋ฉ”๋ชจ๋ฆฌ์ƒ์— ๋ฌธ์ž์—ด์„ ์ง€์ •ํ•˜๊ณ  ํ•ด๋‹น ์•„์ด๋””/ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์˜€์„ ๋•Œ ๋™์ž‘ํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ์ž‘์—…์„ ํ•œ๋‹ค.

 

security-context.xml์— ์ฃผ์„์œผ๋กœ ํ‘œ์‹œ๋œ ๋ถ€๋ถ„ ์•„๋ž˜ ์ฝ”๋“œ๋“ค์„ ์ˆ˜์ •ํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<security:http>
		<security:intercept-url pattern="/sample/all"
			access="permitAll" />
		<security:intercept-url
			pattern="/sample/member" access="hasRole('ROLE_MEMBER')" />
		<security:form-login />
	</security:http>
	<security:authentication-manager>
	<!-- ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„ -->
		<security:authentication-provider>
			<security:user-service>
				<security:user name="member" password="{noop}member"
					authorities="ROLE_MEMBER"/>
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>

user์˜ id๋Š” 'member'๋กœ, password๋„ 'member'๋กœ ์ค€ ์ƒํ™ฉ์ด๋‹ค.

authorities์†์„ฑ์—๋Š” member์—๊ฒŒ ์‚ฌ์šฉ์ž ๊ถŒํ•œ์„ ์ฃผ์–ด ๋กœ๊ทธ์ธ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

password์˜ {noop}์€ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ 5๋ฒ„์ „๋ถ€ํ„ฐ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” PasswordEncoder๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋ฌธ์ž์—ด์ด๋‹ค.

๋ญ... ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ PasswordEncoder๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ž„์‹œ ๋ฐฉํŽธ์œผ๋กœ ํฌ๋งทํŒ… ์ฒ˜๋ฆฌ๋ฅผ ์ง€์ •ํ•ด์„œ ํŒจ์Šค์›Œ๋“œ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค(5๋ฒ„์ „์˜ ๊ฒฝ์šฐ)๋Š”๋ฐ ํŒจ์Šค์›Œ๋“œ ์ธ์ฝ”๋”ฉ ์ฒ˜๋ฆฌ์—†์ด ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ํŒจ์Šค์›Œ๋“œ ์•ž์— {noop}์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. ๐Ÿคท‍โ™€๏ธ(?)

 

๋‹ค์‹œ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด์„œ (๋‹ค์‹œ ์„œ๋ฒ„ ์‹คํ–‰ ์•ˆํ•ด์ฃผ๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.)

/member๋กœ ์ ‘๊ทผํ•œ ๋’ค, member/member๋กœ ๋กœ๊ทธ์ธํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด /memberํŽ˜์ด์ง€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋กœ๊ทธ์ธ์„ ํ•œ ๊ฒฝ์šฐ, ์˜ค๋ฅธ์ชฝ์ฒ˜๋Ÿผ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์˜ Applicationํƒญ์„ ํ™•์ธํ•ด๋ณด๋ฉด Cookies์— 'JSESSIONID'์™€ ๊ฐ™์ด ์„ธ์…˜์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์„ธ์…˜ ์ฟ ํ‚ค์˜ ์กด์žฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

JSESSIONID = Tomcat์—์„œ ๋ฐœํ–‰ํ•˜๋Š” ์ฟ ํ‚ค ์ด๋ฆ„ ์ด๊ธฐ ๋•Œ๋ฌธ์— WAS๋งˆ๋‹ค ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋‘์ž.

 

๋กœ๊ทธ์•„์›ƒ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น JSESSIONID์ฟ ํ‚ค๋ฅผ ๊ฐ•์ œ๋กœ ์‚ญ์ œํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 


์—ฌ๋Ÿฌ ๊ถŒํ•œ์„ ๊ฐ€์ง€๋Š” ์‚ฌ์šฉ์ž ์„ค์ •

 

๊ด€๋ฆฌ์ž๊ฐ™์ด ์ผ๋ฐ˜์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ๋„ ๊ฐ€์ง€๊ณ , ๊ด€๋ฆฌ์ž์˜ ๊ถŒํ•œ๋„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ ๊ฒฝ์šฐ 'ROLE_ADMIN'์™€ 'ROLE_MEMBER'๋ผ๋Š” 2๊ฐœ์˜ ๊ถŒํ•œ์„ ๊ฐ€์ง€๋„๋ก ์ง€์ •ํ•ด์ค€๋‹ค.

security-context.xml์— ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด๋†“์€ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<security:http>
		<security:intercept-url pattern="/sample/all"
			access="permitAll" />
		<security:intercept-url
			pattern="/sample/member" access="hasRole('ROLE_MEMBER')" />
			<security:intercept-url
			pattern="/sample/admin" access="hasRole('ROLE_ADMIN')" /><!-- ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„ -->
		<security:form-login />
	</security:http>
	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
                            <security:user name="member" password="{noop}member" authorities="ROLE_MEMBER"/>
                            <!-- ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„ -->
                            <security:user name="admin" password="{noop}admin" authorities="ROLE_MEMBER, ROLE_ADMIN"/>
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>

์ถ”๊ฐ€๋œ ๋ถ€๋ถ„์„ ์„ค๋ช…ํ•˜์ž๋ฉด..

<security:intercept-url pattern="/sample/admin" access="hasRole('ROLE_ADMIN')" />

/sample/admin url์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์„ค์ •ํ•จ

 

<security:user name="admin" password="{noop}admin" authorities="ROLE_MEMBER, ROLE_ADMIN"/>

admin/admin์œผ๋กœ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๋Š” 'ROLE_MEMBER'์™€ 'ROLE_ADMIN'๋ผ๋Š” 2๊ฐœ์˜ ๊ถŒํ•œ์„ ๊ฐ€์ง

 

์ด ๊ฒฝ์šฐ, admin์€ /member์™€ /admin์— ๋ชจ๋‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 


์ ‘๊ทผ ์ œํ•œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ

'ROLE_MEMBER' ๊ถŒํ•œ๋งŒ ๊ฐ€์ง„ member์‚ฌ์šฉ์ž๊ฐ€ /adminํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด 'ROLE_ADMIN'๊ถŒํ•œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์•„ ์ ‘๊ทผ์— ์ œํ•œ์„ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค.

ํ•ด๋‹น ์ƒํ™ฉ์ฒ˜๋Ÿผ ์ ‘๊ทผ์„ ์‹œ๋„ํ•˜๋ฉด ์•„๋ž˜์˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ Forbidden์—๋Ÿฌ๋ฅผ ๋ณด๊ฒŒ ๋œ๋‹ค.

 

member/member๋กœ ๋กœ๊ทธ์ธํ•˜๊ณ  /sample/admin url๋กœ ์ ‘๊ทผํ•œ ๊ฒฝ์šฐ

์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ์—์„œ๋Š” ์ ‘๊ทผ ์ œํ•œ์— ๋Œ€ํ•ด์„œ ํŠน์ •ํ•œ URL๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ AccessDeniedHandler๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๊ฒฝ์šฐ <security:access-denied-handler>๋ฅผ security-context.xml์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

1) ํŠน์ •ํ•œ URL์„ ์ง€์ •ํ•˜๋Š” ๊ฒฝ์šฐ

error-page๋ฅผ ์ง€์ •ํ•ด์„œ /accessError๋ผ๋Š” url๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ ์ ‘๊ทผ ์ œํ•œ ํ™”๋ฉด์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

security-context.xml์— ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด๋†“์€ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<security:http auto-config="true" use-expressions="true">	<!-- ์ถ”๊ฐ€๋จ -->
		<security:intercept-url pattern="/sample/all"
			access="permitAll" />
		<security:intercept-url
			pattern="/sample/member" access="hasRole('ROLE_MEMBER')" />
			<security:intercept-url
			pattern="/sample/admin" access="hasRole('ROLE_ADMIN')" />
		<security:form-login />
		<security:access-denied-handler error-page="/accessError"/>	<!-- ์ถ”๊ฐ€๋จ -->
	</security:http>
	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
				<security:user name="member" password="{noop}member"
					authorities="ROLE_MEMBER"/>
					<security:user name="admin" password="{noop}admin"
					authorities="ROLE_MEMBER, ROLE_ADMIN"/>
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>

<security:access-denied-handler>์˜ error-page์†์„ฑ์— /accessError url์„ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

/accessError ์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด CommonController.javaํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

 

CommonController.java

package com.taeong.controller;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import lombok.extern.log4j.Log4j;

@Controller
@Log4j
public class CommonController {	//๊ฐ„๋‹จํžˆ ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋งŒ์„ Model์— ์ถ”๊ฐ€
	
	@GetMapping("/accessError")
	public void accessDenied(Authentication auth, Model model) {
		//Authenticationํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋„๋ก ์„ค๊ณ„ํ•ด์„œ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
		log.info("access Denied : "+auth);
		model.addAttribute("msg","Access Denied");
	}
}

 

accessError.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/security/tags"
	prefix="sec"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Access Denied Page</h1>
	<h2>
		<c:out value="${SPRING_SECURITY_403_EXCEPTION.getMessage() }" />
	</h2>
	<h2>
		<c:out value="${msg}" />
	</h2>
</body>
</html>

Access Denied์˜ ๊ฒฝ์šฐ๋Š” 403 ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

JSP์—์„œ๋Š” HttpServlet Request ์•ˆ์— 'SPRING_SECURITY_403_EXCEPTION'์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ Access DeniedException ๊ฐ์ฒด๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.

member/member๋กœ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๊ฐ€ /admin์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ, ์ด์ „์—๋Š” Forbidden ์—๋Ÿฌ ํ™”๋ฉด์ด ๋–ด์ง€๋งŒ ํ•ด๋‹น ์ž‘์—…์„ ํ†ตํ•ด accessError.jsp์˜ ๋‚ด์šฉ์ด ๋ณด์ด๊ฒŒ ๋œ๋‹ค.

 

member/member๋กœ ๋กœ๊ทธ์ธํ•ด์„œ adminํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ log์ฐฝ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผ์„ ์‹œ๋„ํ•œ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (log.info("access Denied : "+auth); ๋ถ€๋ถ„)

INFO : com.taeong.controller.CommonController - access Denied : org.springframework.security.authentication.UsernamePasswordAuthenticationToken@ff722209: Principal: org.springframework.security.core.userdetails.User@bfc28a9a: Username: member; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_MEMBER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffde5d4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 71A6D88EE2BE6A8CCEEF15BC9B1A015D; Granted Authorities: ROLE_MEMBER

 

 

2) AccessDeniedHandler ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ

<security:access-denied-handler error-page="/accessError">๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” error-page๋งŒ์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผํ–ˆ๋˜ url์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

์œ„์˜ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด ๊ทธ๋Œ€๋กœ /sample/admin์— ์œ„์น˜ํ•˜๊ณ  ์žˆ์ง€๋งŒ ํ™”๋ฉด๋งŒ accessError.jsp์˜ ๋‚ด์šฉ๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ ‘๊ทผ์ด ์ œํ•œ๋œ ๊ฒฝ์šฐ์— ๋‹ค์–‘ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ง์ ‘ AccessDeniedHandler ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ข‹๋‹ค.

์ƒˆ๋กœ securityํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  (๋‚ด ๊ฒฝ์šฐ์—๋Š” com.taeong.security๋ผ๋Š” ํŒจํ‚ค์ง€๋ช…์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€๋‹ค.) CustomAccessDeniedHandler.java๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.

AccessDeniedHandler ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ inplementsํ•ด์ฃผ๊ณ  ์˜ค๋ฒ„๋ผ์ด๋”ฉ์œผ๋กœ AccessDeniedHandler์˜ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ตฌํ˜„ํ•ด์ค€๋‹ค.

 

CustomAccessDeniedHandler.java

package com.taeong.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import lombok.extern.log4j.Log4j;

@Log4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler{
	
	//AccessDeniedHandler ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException, ServletException {
		log.error("Access Denied Handler");
		log.error("Redirect....");
		response.sendRedirect("/accessError");
	}
	
}

 

security-context.xml์—์„œ๋Š” error-page์†์„ฑ ๋Œ€์‹ ์— CustomAccessDeniedHandler๋ฅผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์„œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ <bean>์„ security-context.xml์— ์ถ”๊ฐ€ํ•œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„ -->
	<bean id="customAccessDenied" class="com.taeong.security.CustomAccessDeniedHandler"></bean>
	
	<security:http auto-config="true" use-expressions="true">
		<security:intercept-url pattern="/sample/all"
			access="permitAll" />
		<security:intercept-url
			pattern="/sample/member" access="hasRole('ROLE_MEMBER')" />
			<security:intercept-url
			pattern="/sample/admin" access="hasRole('ROLE_ADMIN')" />
		<security:form-login />
		<!-- <security:access-denied-handler error-page="/accessError"/>  --><!-- ์‚ญ์ œ๋œ ๋ถ€๋ถ„ -->
		<security:access-denied-handler ref="customAccessDenied"/><!-- ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„ -->
	</security:http>
	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
				<security:user name="member" password="{noop}member"
					authorities="ROLE_MEMBER"/>
					<security:user name="admin" password="{noop}admin"
					authorities="ROLE_MEMBER, ROLE_ADMIN"/>
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
	
</beans>

 

์„œ๋ฒ„๋ฅผ ์žฌ์‹คํ–‰ํ•˜์—ฌ member/member๋กœ ๋กœ๊ทธ์ธํ•œ ๋’ค /admin url๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด accessError๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋Œ“๊ธ€