๋ฌธ์ ๊ฐ์
ํ์ฌ, ์ธ์ฆ์ด ํ์ํ URL ๋ชฉ๋ก์ `AuthFilter`์ `SecurityConfig`์์ ์ฌ์ฉ๋๊ณ ์์ต๋๋ค.
- AuthFilter์์๋ ์ด๋ค ์์ฒญ์ด ๋ค์ด์ฌ ๋, ์ธ์ฆ์ด ํ์ํ ๊ฒฝ์ฐ Access ํ ํฐ์ ๊ฒ์ฆํฉ๋๋ค. ์ด๋, ์ธ์ฆ์ด ํ์ํ URL ๋ชฉ๋ก์ ํตํด ํํฐ๋งํ์ง ์์ผ๋ฉด, ์ธ์ฆ์ด ํ์ ์๋ ์์ฒญ๊น์ง Access ํ ํฐ์ ๊ฒ์ฆํ์ฌ ์ธ์ฆ ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ค์ ๋ฉ์๋๋ฅผ ํตํด URL ํต๊ณผ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return super.shouldNotFilter(request);
}
- SecurityConfig์์๋ ํด๋น URL์ ์ ๊ทผํ๋ ์ฌ์ฉ์๊ฐ ์ธ์ฆ๋ ์ฌ์ฉ์์ธ์ง ํ์ธํด์ผ ํฉ๋๋ค. ์๋์ ๊ฐ์ด requestMatchers().authenticated() ์ค์ ์ ํตํด ์ธ์ฆ์ด ํ์ํ URL์ ์ ์ํฉ๋๋ค.
.authorizeHttpRequests(auth -> auth
.requestMatchers().authenticated()
.anyRequest().permitAll()
)
์ด์ฒ๋ผ ์ธ์ฆ์ด ํ์ํ URL ๋ชฉ๋ก์ด ๋ ๊ณณ์์ ์ฌ์ฉ๋๋๋ฐ, ๊ฐ๊ฐ์ ๋ฐ๋ก ๊ด๋ฆฌํ๋ค ๋ณด๋ฉด ์๋ก์ด URL์ด ์ถ๊ฐ๋ ๋๋ง๋ค ์์ชฝ์ ๋ชจ๋ ๋ฐ์ํด์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์๊น๋๋ค. ๋ํ, ํ์ชฝ์์ URL์ด ๋๋ฝ๋ ๊ฐ๋ฅ์ฑ๋ ์์ด ์ ์ง๋ณด์์ ์ข์ง ๋ชปํฉ๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
์์ ์ธ๊ธํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด `AuthUrlManager`๋ฅผ ๋์ ํ์ฌ ์ธ์ฆ์ด ํ์ํ URL์ ํ๊ณณ์์ ๊ด๋ฆฌํ๋๋ก ํ๊ณ , ์ด๋ฅผ AuthFilter์ SecurityConfig์์ ๊ณตํต์ผ๋ก ์ฐธ์กฐํ๋๋ก ๊ตฌํํ์ต๋๋ค.
- AuthUrlManager ๊ตฌํ
public class AuthUrlManager {
public static RequestMatcher[] getUserRequestMatchers() {
return new RequestMatcher[]{
new AntPathRequestMatcher("/api/auths/me"),
new AntPathRequestMatcher("/api/auths/logout"),
new AntPathRequestMatcher("/api/stores/**"),
new AntPathRequestMatcher("/api/reviews/**"),
new AntPathRequestMatcher("/api/bookmarks/**"),
new AntPathRequestMatcher("/api/search/**"),
new AntPathRequestMatcher("/api/inquiries", "POST"),
new AntPathRequestMatcher("/api/inquiries/{inquiryId}", "PUT"),
new AntPathRequestMatcher("/api/inquiries/{inquiryId}", "DELETE"),
new AntPathRequestMatcher("/api/notices", "POST"),
new AntPathRequestMatcher("/api/notices/{noticeId}", "PUT"),
new AntPathRequestMatcher("/api/notices/{noticeId}", "DELETE")
};
}
}
- AuthFilter์์์ ํ์ฉ
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return Arrays.stream(AuthUrlManager.getUserRequestMatchers())
.noneMatch(matcher -> matcher.matches(request));
}
- SecurityConfig์์์ ํ์ฉ
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
...
.authorizeHttpRequests(auth -> auth
.requestMatchers(AuthUrlManager.getUserRequestMatchers()).authenticated()
.anyRequest().permitAll()
...
)
return http.build();
}
๊ฒฐ๊ณผ
AuthUrlManager๋ฅผ ๋์ ํจ์ผ๋ก์จ ์๋ก์ด URL์ด ์ถ๊ฐ๋๋๋ผ๋ AuthUrlManager๋ง ์์ ํ๋ฉด ๋๋ฏ๋ก, ์ ์ง๋ณด์๊ฐ ๊ฐํธํด์ก์ต๋๋ค.
'ํ๋ก์ ํธ > NolGoat' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ์จ์ ์ธ ํ ํฐ ๊ด๋ฆฌ๋ฅผ ์ํ Redis ๋์ (1) | 2024.11.29 |
---|---|
์ธ์ฆ ๋ณด์ ๊ฐํ (0) | 2024.11.29 |
Spring Security ์ธ์ฆ ํํฐ ์์ธ ์ฒ๋ฆฌํ๊ธฐ (0) | 2024.11.27 |
์กฐํ ๋ฐฉ์ ๊ฐ์ ๋ฐ ์ธ๋ฑ์ค ์์ (0) | 2024.11.26 |
MySQL ์กฐํ ์ฑ๋ฅ ๊ฐ์ (0) | 2024.11.26 |