๐Ÿ›ผ study/๐Ÿ“ฒ tech

[spring] spring security ๊ธฐ๋ณธ ๊ฐœ๋…

jcowwk 2024. 4. 17. 17:34

spring security ๊ธฐ๋ณธ ๊ฐœ๋…


์Šคํ”„๋ง ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ ์ž…๋‹ˆ๋‹ค !

์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์„ธ์š” :)

 

1. spring security

2. ๋™์ž‘ ๊ณผ์ •

3. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•


1. spring security

์Šคํ”„๋ง ๊ธฐ๋ฐ˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ธ์ฆ, ๊ถŒํ•œ, ๊ด€๋ฆฌ, ๋ฐ์ดํ„ฐ ๋ณดํ˜ธ ๋“ฑ์˜ ๋ณด์•ˆ ์„ค์ •์„ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

 

 

- ์ธ์ฆ

์‚ฌ์šฉ์ž์˜ ์‹ ์›์„ ํ™•์ธํ•˜๋Š” ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ๋‹ค๋ฃฌ๋‹ค.

ํผ ๊ธฐ๋ฐ˜์˜ ๋กœ๊ทธ์ธ, OAuth, HTTP Basic ์ธ์ฆ ๋“ฑ์„ ์ง€์›ํ•œ๋‹ค.

 

- ๊ถŒํ•œ

์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด ํŠน์ • ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค.

 

- ๋ณด์•ˆ ํ•„ํ„ฐ

SecurityFilterChain์€ HTTP ๋ณด์•ˆ ํ•„ํ„ฐ๋“ค์˜ ์ฒด์ธ์„ ๊ตฌ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๋ณด์•ˆ์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•  ๋•Œ ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

์œ„์˜ ์„ค์ • ํด๋ž˜์Šค์—์„œ OAuth2 ๋ณด์•ˆ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ ์™ธ์—๋Š” UsernamePasswordAuthenticationFilter๋Š” ํผ ๊ธฐ๋ฐ˜ ๋กœ๊ทธ์ธ์„ ์ฒ˜๋ฆฌ, BasicAuthenticationFilter๋Š” HTTP Basic ์ธ์ฆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2. ๋™์ž‘ ๊ณผ์ •

1. ์‚ฌ์šฉ์ž์˜ ์ธ์ฆ ์š”์ฒญ์ด ๋“ค์–ด์˜จ๋‹ค.

2. Authotication Filter์—์„œ ์ธ์ฆ ์š”์ฒญ์„ ๊ฑธ๋Ÿฌ Authotication Manger๋กœ ๋ณด๋‚ธ๋‹ค.

3. Authotication Manager๋Š” ๋“ฑ๋ก๋œ Authotication Provider๋ฅผ ์กฐํšŒํ•˜๋ฉฐ ์ธ์ฆ์„ ์š”์ฒญํ•œ๋‹ค.

4. ํ•ด๋‹น Authotication Provider์—์„œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

5. ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ UserDetails ๊ฐ์ฒด์— ๋‹ด์•„์„œ ๊ฐ์ฒด๋ฅผ returnํ•œ๋‹ค.

6. ๊ฐ์ฒด๋Š” SecurityContextHolder์— ์ €์žฅ๋˜์–ด ์œ ์ € ์ •๋ณด๋ฅผ Controller์—์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 

3. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

- build.gradle

implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-security-test'

 

์ฒซ ๋ฒˆ์งธ๋กœ spring security๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

- SecurityConfig.java

public SecurityFilterChain ํ•จ์ˆ˜๋ช…(HttpSecurity httpSecurity) throws Exception {} ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด์ค€๋‹ค.


์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

https://www.elancer.co.kr/blog/view?seq=235

 

Spring Security๋ž€? ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋ถ€ํ„ฐ ์„ค์ • ๋ฐฉ๋ฒ•๊นŒ์ง€ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค! I ์ด๋žœ์„œ ๋ธ”๋กœ๊ทธ

ํ™ˆํŽ˜์ด์ง€์— ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ธฐ๋Šฅ์„ ๋น ๋ฅด๊ฒŒ ๋ถ€์—ฌํ•ด ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๋ณดํ˜ธ ๊ธฐ๋Šฅ์„ ์†์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” Spring์˜ ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜์ธ ‘Spring Security’์— ๋Œ€ํ•ด ์ด๋žœ์„œ์—์„œ ์ž์„ธํžˆ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. I spring

www.elancer.co.kr

 

https://spring.io/guides/gs/securing-web (spring security ๊ณต์‹ ์‚ฌ์ดํŠธ)

 

Getting Started | Securing a Web Application

Suppose that you want to prevent unauthorized users from viewing the greeting page at /hello. As it is now, if visitors click the link on the home page, they see the greeting with no barriers to stop them. You need to add a barrier that forces the visitor

spring.io

 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋งŒ๋‚˜์š” !