ํฐ์คํ ๋ฆฌ ๋ทฐ
[Spring Security] ๊ฒฝ๋ก์ @PathVariable ์ ํฌํจํ๋ API ๋ฅผ ์ธ์ฆ ์์ด๋ ํธ์ถํ๋๋ก ์ค์ ํ๊ธฐ
Nickolodeon 2023. 1. 5. 13:53๐ Introduction
ํ๋ก์ ํธ์์ ์คํ๋ง ์ํ๋ฆฌํฐ๋ฅผ ์ ์ฉํด ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ API ๊ฒฝ๋ก๋ ์ธ์ฆ ์ ์ฐจ๋ฅผ ๊ฑฐ์น๊ฒ ํ๊ณ , ํ์๊ฐ์ ๊ณผ ๋ก๊ทธ์ธ์ ํฌํจํ ๋ช๋ช API ๋ ์ธ์ฆ์ด ์์ด ํธ์ถ์ด ๊ฐ๋ฅํ๋๋ก ๋ณ๊ฒฝํด์ผ ํ๋ค. ๋ชจ๋ API ๊ฐ ์ธ์ฆ ์ ์ฐจ๋ฅผ ๊ฑฐ์น๊ฒ ํ๋ ๊ฒ์ ์ด๋ ต์ง ์์์ผ๋, ๋ช ๊ฐ์ API ๊ฐ ์ธ์ฆ์ด ์์ด๋ ํธ์ถ๋๋๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ด ๊น๋ค๋ก์ ๋ค.
๐ฉ ํ์ฌ ์ํฉ
์๋๋ ํ์ฌ ๋ณด์ ํํฐ ์ฒด์ธ์ ๊ตฌ์ฑ์ด๋ค. ๊ฐ๊ฐ ํ์๊ฐ์
๊ณผ ๋ก๊ทธ์ธ์ ๋ํ๋ด๋POST /api/v1/user/join
๊ณผ POST /api/v1/users/login
์ antMatchers
์ permitAll()
๋ฉ์๋๋ฅผ ํ์ฉํด์ ์ธ์ฆ ์ ์ฐจ๊ฐ ํ์ํ์ง ์๊ฒ ์ค์ ํด์ฃผ์๋ค.
๋ฌธ์ ๋ GET api/v1/posts/**
๋ํ ์ธ์ฆ ์์ด ํธ์ถ๋๊ฒ ์ค์ ํ๋ค๋ ์ ์์ ๋ฐ์ํ๋ค.
์ด์ ๋ ์๋์ ๊ฐ์๋ค:
- ํ์ฌ ์ค์ ๋๋ก๋ฉด ๊ฒฝ๋ก๊ฐ
api/v1/posts/
๋ก ์์ํ๋ ๋ชจ๋GET
๋ฐฉ์์ API ๋ ์ธ์ฆ ์ ์ฐจ ์์ด ํธ์ถ๋๋ค. - ๊ทธ๋ฐ๋ฐ, ํ ์์ ์์ ์ถ๊ฐํ API ๋ ๋ก๊ทธ์ธ ํ ์ฌ์ฉ์๊ฐ ์์ ์ด ์์ฑํ ํฌ์คํธ๋ฅผ ๋ชจ์์ ๋ณผ ์ ์๋ API์ธ
GET api/v1/posts/my
์ด๋ค. - ๊ทธ๋ฌ๋ฏ๋ก,
GET
๋ฐฉ์์api/v1/posts/
๋ก ์์ํ๋ ๊ฒฝ๋ก์ API ์์๋ ๋ถ๊ตฌํ๊ณ ์ธ์ฆ ์ ์ฐจ๋ฅผ ๊ฑฐ์น๋๋ก ๋ฐ๋ก ์ค์ ํด ์ฃผ์ด์ผ ํ๋ค.
์ฆ, ํ์ฌ ์ํฉ์์ GET api/v1/posts/my
๋ ์ธ์ฆ ์์ด๋ Authentication ๊ฐ์ฒด์ ๋ํ ์ ๊ทผ์ด ํ์ฉ๋์ด ์์๋ค.
์ธ์ฆ์ด ๋์ง ์์ ์ํฉ์์ Authentication
๊ฐ์ฒด์ ๊ฐ์ null
์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ ๊ทผ์ด ํ์ฉ๋์ด ์์ผ๋ฉด, ์ธ์ฆ์ด ๋์ง ์์์ ๋ authentication.getName()
์์ NullPointerException
์ด ๋ฐ์ํ๋ค. ํ์ฌ ํ๋ก์ ํธ์์๋ ์ธ์ฆ์ด ๋์ง ์์์ ๋ ์ปค์คํ
์๋ฌ์ธ INVALID\_PERMISSION
์๋ฌ๋ฅผ ๋ฐํํด์ผ ํ๊ณ , ์ด๋ ๋ก๊ทธ์ธ ์์ด๋ ์ ๊ทผ ์์ฒด๋ฅผ ํ์ฉํ์ง ์์์ผ ๋ก๊ทธ์ธ ํ์ง ์์ ์ํฉ์ ๋ํด ๋ฐํ๋ ์ ์๋ ์๋ฌ์ด๋ค.
๐ ํด๊ฒฐ
๊ทธ๋์, ์ฐ์ ํ์ฌ /api/v1/posts/**
์ ํจํด๊ณผ ์ผ์นํ๋ ๋ชจ๋ GET
API ๊ฒฝ๋ก๋ฅผ ํ์ฉํ๋ ๋ฐฉ์์ api/v1/posts/my
๋ฅผ ์ ์ธํ ํน์ API ๊ฒฝ๋ก๋ง ํ์ฉํ๋๋ก ๋ณ๊ฒฝํ๊ณ ์ ํ๋ค. ๋ณ๊ฒฝ ๋ฐฉ๋ฒ์ ์ด๋ ต์ง ์์๋ค. ๋จ์ํ antMatchers() ๋ด๋ถ์์ ์ผํ(,) ๋ฅผ ์ฌ์ฉํด ํ๋ฆฌํจ์ค๋ฅผ ํ์ฉํ API ๊ฒฝ๋ก๋ค์ ๋ช
์ํด์ฃผ๋ฉด ๋์๋ค.
.antMatchers(HttpMethod.GET, "/api/v1/posts", "/api/v1/posts/{postId}",
"/api/v1/posts/{postId}/comments").permitAll()
ํ์ง๋ง, Swagger ๋ฅผ ์ฌ์ฉํด GET
API ๋ค์ ํธ์ถํด๋ณด๋, ๋ณํจ ์์ด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ ์ฝ๋๋ก๋ GET api/v1/posts/my
์ ๋ํด์๋ ์ธ์ฆ ์ ์ฐจ ์์ด ํธ์ถ์ด ๊ฐ๋ฅํ๋ค๋ ์๋ฏธ์๋ค. ์ฐ์ฐํ๋ ๋ถ๋ถ์ด {postId}
๋ก API ๊ฒฝ๋ก์ @PathVariable
๋ฅผ ์ง์ ํ๋ ๋ถ๋ถ์ด์ด์ @PathVariable
์ด ์๋ ๊ฒฝ๋ก๋ฅผ antMatchers
์์ ๋ช
์ํ ๋ ์ฌ์ฉํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ด ๋ฌด์์ธ์ง ๊ฒ์ํด๋ณด์๋ค:
stackoverflow ์์ ๋ต์ ์ฐพ์ ์ ์์๋ค. @PathVariable
๋ค์ ๋ํด์๋ {๋ณ์๋ช
:<๋ฐ์ดํฐ ํ์ ๋ฐ๋ฅธ ์ ๊ท์>}
ํํ๋ก ๋ช
์ํด์ฃผ์ด์ผ ํ๋ค๋ ์ฌ์ค์ ์๊ฒ ๋์๊ณ , ๋ฐ๋ก ์ ์ฉํด๋ณด์๋ค:
.antMatchers(HttpMethod.GET, "/api/v1/posts", "/api/v1/posts/{postId:\\d+}",
"/api/v1/posts/{postId:\\d+}/comments").permitAll()
postId
๋ ์ ์ํ ๋ณ์์๊ธฐ ๋๋ฌธ์ ์ ๊ท์ \\d+
๋ฅผ ์ฌ์ฉํ๋ค.
๋ค์ ํ๋ก์ ํธ๋ฅผ ์คํํ์, ๋ฌธ์ ์์ด ๋ก๊ทธ์ธ ํ์ง ์์ ๊ฒฝ์ฐ์ GET api/v1/posts/my
๋ ์๋ํ INVALID_PERMISSION
์๋ฌ๋ฅผ ๋๊ณ , ๋๋จธ์ง GET
API ๋ค์ ๋ก๊ทธ์ธ ์์ด๋ ํธ์ถ์ด ๊ฐ๋ฅํ ๊ฒ์ ํ์ธํ ์ ์์๋ค!
๐ ์ถ์ฒ
How to apply Spring Security AntMatchers pattern only to url with pathVariable
I am using Spring boot and WebSecurityConfigurerAdapter to configure security. Method to configure ignored security antMatches looks like this: @Override public void configure(final
stackoverflow.com
AntPathMatcher (Spring Framework 6.0.3 API)
Combine two patterns into a new pattern. This implementation simply concatenates the two patterns, unless the first pattern contains a file extension match (e.g., *.html). In that case, the second pattern will be merged into the first. Otherwise, an Illega
docs.spring.io
'Web Framework' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- Total
- Today
- Yesterday
- DTO
- Spring Boot
- ์๊ณ ๋ฆฌ์ฆ
- ๊ธฐ์ง๊ตญ ์ค์น
- JOIN FETCH
- google cloud
- ci/cd
- ๊น๋ฉ
- @RequestBody
- Jackson
- docker
- ์ญ์ง๋ ฌํ
- ์ฝํ
- ๋์ปค
- JPQL
- JPA
- FCM
- LazyInitializationException
- ์ธ์ฆ/์ธ๊ฐ
- Firebase
- ๊ฐ์ ์๋ฒ
- gitlab
- N+1
- ํ๋ก๊ทธ๋๋จธ์ค
- Java Data Types
- spring
- json web token
- DeSerialization
- ์ง์ฐ ๋ก๋ฉ
- ์ค์๊ฐ๋ฐ์ดํฐ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |