Spring Security ๋กœ๊ทธ์ธ ์ธ์ฆ ๋ฐ ์ธ๊ฐ€ (Security ํ™˜๊ฒฝ์„ค์ •, AuthenticationManger, AutenticationProvider)

2021. 12. 2. 17:50ใ†Spring

728x90

 

1. Spring Security ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ 

Spring ๋ฒ„์ „ : 5.2.13

java : 1.8

pom.xml์— ์ถ”๊ฐ€ 

 

 

1๏ธโƒฃ Spring Security Web

https://mvnrepository.com/artifact/org.springframework.security/spring-security-web

 

2๏ธโƒฃ Spring Security Core

https://mvnrepository.com/artifact/org.springframework.security/spring-security-core

 

3๏ธโƒฃ Spring Security Config

https://mvnrepository.com/artifact/org.springframework.security/spring-security-config

 

4๏ธโƒฃ Spring Security Taglibs

https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs

 

 

2. security-context.xml ์„ค์ •ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ 

๊ธฐ๋ณธ์ธ servlet-context.xml ๋ง๊ณ  ์ƒˆ๋กœ security-context.xml ํŒŒ์ผ์„ spring -> appServlet๋ฐ‘์— ์ƒ์„ฑํ•ด์ค€๋‹ค. 

๊ทธ๋ฆฌ๊ณ  namespace์—์„œ security๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

 

์›๋ž˜ ์ €๋ถ€๋ถ„์ด 5.2.xsd์ธ๊ฐ€ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑ๋˜์–ด์žˆ๋Š”๋ฐ ๋…ธ๋ž—๊ฒŒ ์น ํ•œ ๋ถ€๋ถ„์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  security-context.xml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธฐ๋ณธ์ ์ธ ํฌ๋งท์„ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

3. web.xml ์„ค์ •ํ•˜๊ธฐ  

์ƒˆ๋กœ ๋งŒ๋“  security-context.xml ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€๋กœ ์ž‘์„ฑํ•ด์ค€๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  security ๊ด€๋ จ filter๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. 

<!-- Security ๊ด€๋ จ filter ์ถ”๊ฐ€ --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

 

์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ ํ™˜๊ฒฝ์„ค์ •์ด๋‹ค..!! ์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •ํ•˜๊ณ  ์ •์ƒ์ ์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณธ๋‹ค. 

 

 

4. Spring Security Login 

 

์„ธ๊ฐ€์ง€ ๊ถŒํ•œ์„ ๋งŒ๋“ค์–ด์„œ all์€ ๋กœ๊ทธ์ธ ์—ฌ๋ถ€์™€ ์ƒ๊ด€์—†์ด ์ ‘์† ๊ฐ€๋Šฅ, member๋Š” ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๋“ค์ด ์ ‘์† ๊ฐ€๋Šฅ, admin์€ ๊ด€๋ฆฌ์ž๋กœ ์ด ๊ถŒํ•œ์„ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋“ค๋งŒ ๋กœ๊ทธ์ธํ•  ์ˆ˜ ์žˆ๊ณ , admin์€ member ์— ์ ‘์†ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. 

 

1๏ธโƒฃ SampleController.java

@GetMapping์€ @RequestMapping(method=RequestMethod.GET)์˜ ์ถ•์•ฝํ˜•์ด๋‹ค..!

 

 

2๏ธโƒฃ sample/all.jsp, member.jsp, admin.jsp 

all.jsp
member.jsp
admin.jsp

 

3๏ธโƒฃ Spring Security์—์„œ์˜ ์ธ์ฆ Authentication

AuthenticationManager : ์ธ์ฆ ๋‹ด๋‹น

AuthenticationProvider : ์ธ์ฆ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ 

 

- member 

&nbsp;security-context.xml&nbsp;์ ‘๊ทผ ์ œํ•œ ์„ค์ •&nbsp;

์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๊ณ  /sample/all์€ ์ ‘์† ๊ฐ€๋Šฅํ•˜์ง€๋งŒ /sample/member๋กœ ์ ‘์†ํ•˜๋ฉด Spring Security๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค. 

 

security-context.xml์— ์ธ์ฆ๊ณผ ๊ถŒํ•œ ์ฒ˜๋ฆฌ ์„ค์ •&nbsp;

์ด๋•Œ {noop}์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” Spring Security5๋ถ€ํ„ฐ๋Š” ๋ฐ˜๋“œ์‹œ passwordEncoder๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž„์‹œ๋กœ ํ•ด๋‘”๊ฒƒ์ด๋‹ค!

authorities๋Š” ๋กœ๊ทธ์ธ ์„ฑ๊ณต์‹œ ๋ถ€์—ฌ๋˜๋Š” ๊ถŒํ•œ์ด๋‹ค.

member๋กœ ๋กœ๊ทธ์ธ ์„ฑ๊ณต์‹œ ํ™•์ธ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€&nbsp;

์ง€๊ธˆ์€ ์ผ๋‹จ ๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” session์„ ์ง์ ‘ ์‚ญ์ œํ•ด์ค˜์•ผํ•œ๋‹ค. (f12 ๋ˆŒ๋Ÿฌ์„œ ์‚ญ์ œํ•˜๊ธฐ)

 

 

- admin 

security-context.xml์— admin ์ ‘๊ทผ ์ œํ•œ ์„ค์ •
security-context.xml์— admin ์ธ์ฆ๊ณผ ๊ถŒํ•œ ์„ค์ •

 

 

admin์—์„œ๋Š” ์ด์ œ member์™€ admin ํŽ˜์ด์ง€์— ๋ชจ๋‘ ์ ‘์†ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ

member์—์„œ admin ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ ค๊ณ  ํ•˜๋ฉด 

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌํ™”๋ฉด์„ ๋ณด๊ฒŒ ๋œ๋‹ค. 

 

 

4๏ธโƒฃ Spring Security์—์„œ ์ ‘๊ทผ ์ œํ•œ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ 

์œ„์—์„œ admin ๊ถŒํ•œ์ด ์—†๋Š” member๊ฐ€ admin ํŽ˜์ด์ง€ ์ ‘์†ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์ ‘๊ทผ ์ œํ•œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ๋ฅผ AccessDeinedHandler๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

- error-page 

security-context.xml์— ์ถ”๊ฐ€&nbsp;

<security:access-denied-handler>์—์„œ๋Š” error-page๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ AccessDeniedHandler ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•ด์„œ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ง€๊ธˆ ์œ„์—์„œ๋Š” ์ ‘๊ทผ ๊ถŒํ•œ ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ /accessError uri๋กœ ํ™”๋ฉด ์ง€์ •ํ•œ๋‹ค. 

CommonController๋ฅผ ์ƒˆ๋กœ ์ž‘์„ฑํ•œ๋‹ค.&nbsp;
accessError.jsp ํ™”๋ฉด&nbsp;
์—๋Ÿฌ๋ฐœ์ƒ ์‹œ ํ™”๋ฉด&nbsp;

๊ทธ๋Ÿฌ๋ฉด 403 ์—๋Ÿฌ ํ™”๋ฉด์ด ์•„๋‹Œ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•œ ์—๋Ÿฌ ํŽ˜์ด์ง€๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

- AccessDeniedHandler ๊ตฌํ˜„ 

์ ‘๊ทผ ์ œํ•œ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” AccessDeniedHandler๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ ‘๊ทผ ์ œํ•œ ๋ฐœ์ƒ ์‹œ  ์ฟ ํ‚ค๋‚˜ ์„ธ์…˜์— ํŠน์ •ํ•œ ์ž‘์—…์„ ํ•˜๊ฑฐ๋‚˜ HttpServletResponse ์— ํŠน์ •ํ•œ ํ—ค๋” ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋“ฑ์˜ ํ–‰์œ„๋ฅผ ํ•  ๊ฒฝ์šฐ AccessDeniedHandler๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

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

 

security-context.xml์— ๋นˆ ์„ค์ •
error-page์—์„œ ref๋กœ ๋ณ€๊ฒฝ

 

 

5๏ธโƒฃ Spring Security Login ํŽ˜์ด์ง€ ์ปค์Šคํ…€ํ•˜๊ธฐ 

security-context.xml

์ € ํƒœ๊ทธ๋Š” Security์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์ด๋‹ค.

 

์ด๊ฑธ ์ด์ œ customLogin์œผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ์–ด Login ํŽ˜์ด์ง€๋ฅผ ์ปค์Šคํ…€ํ•  ๊ฒƒ์ด๋‹ค.

CommonController์— ์ถ”๊ฐ€&nbsp;

 

customLogin.jsp&nbsp;

์ด๋•Œ post๋กœ submit ํ•ด์•ผํ•˜๋ฉฐ,  name์€ userid๊ฐ€ ์•„๋‹Œ username์ด๋‹ค!!

 

6๏ธโƒฃ Spring Security Login ์„ฑ๊ณต ํŽ˜์ด์ง€ 

๋กœ๊ทธ์ธ ์„ฑ๊ณต ์ดํ›„์— ํŠน์ • ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ AutenticationScueessHandler๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. 

์—ฌ๊ธฐ์„œ๋Š” ๋กœ๊ทธ์ธ ํ•˜๊ณ  ๋‚˜์„œ ํ•ด๋‹นํ•˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋Š” ๋™์ž‘์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

security-context.xml ์— customLoginSucessHandler ๋นˆ ์ถ”๊ฐ€&nbsp;

 

 

7๏ธโƒฃ Spring Security Logout 

security-context.xml์— ๋กœ๊ทธ์•„์›ƒ ์ถ”๊ฐ€&nbsp;

invalidate-session์€ ๋กœ๊ทธ์•„์›ƒ์‹œ ์„ธ์…˜ ๋ฌดํšจํ™” ์„ค์ •์ด๋ฉฐ, ์ฟ ํ‚ค ์ง€์šฐ๋Š” ์„ค์ •์€ delete-cookies ์ง€์ • ๊ฐ€๋Šฅํ•˜๋‹ค.

CommonController.java์— ์ถ”๊ฐ€

 

customLogout.jsp

๋กœ๊ทธ์•„์›ƒ ์‹ค์ œ ์ž‘์—…์€ customLogout์ด ์ฒ˜๋ฆฌํ•˜๋ฉฐ post๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•œ๋‹ค. 

(post์ธ ์ด์œ ๋Š” ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ๊ธฐ๋ณธ ์„ค์ •์ด๋ผ์„œ!)

admin.jsp์— ๋กœ๊ทธ์•„์›ƒ ์ถ”๊ฐ€&nbsp;

์œ„์—์„œ get๋ฐฉ์‹๋„ ๊ฐ™์ด ์ง€์ •ํ•œ ์ด์œ ๋Š” admin ํŽ˜์ด์ง€์—์„œ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด post๋ฐฉ์‹์ด ์•„๋‹Œ get ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

 

5. ์ตœ์ข… ๊ฒฐ๊ณผ 

1๏ธโƒฃ sample/admin ์ ‘์† 

๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— loginํŽ˜์ด์ง€๋กœ ์ด๋™ 

 

2๏ธโƒฃ customLogin ํŽ˜์ด์ง€๋กœ ์ด๋™ 

3๏ธโƒฃ admin์œผ๋กœ ๋กœ๊ทธ์ธ 

4๏ธโƒฃ ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด logout ํŽ˜์ด์ง€๋กœ ์ด๋™ (get)

5๏ธโƒฃ ๋กœ๊ทธ์•„์›ƒ (post)

 

 

 

Spring Security๊นŒ์ง€ ๋ฐฐ์› ์œผ๋‹ˆ ์ด์ œ ๋‹ค์Œ์—๋Š” ๊ณ„์† ๋ฐœ์ „์‹œ์ผœ์˜ค๋˜ ๊ฒŒ์‹œํŒ์— ๋Œ€ํ•ด์„œ ๋กœ๊ทธ์ธ์„ ์ ์šฉํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค! 

 

 

 

 

 

 

 

 

 

 

 

728x90