์ธ์ฆ์ด ํ•„์š”ํ•œ URL ๊ณตํ†ต ๊ด€๋ฆฌ

2024. 11. 27. 04:58ยทํ”„๋กœ์ ํŠธ/NolGoat

๋ฌธ์ œ ๊ฐœ์š”

ํ˜„์žฌ, ์ธ์ฆ์ด ํ•„์š”ํ•œ 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
'ํ”„๋กœ์ ํŠธ/NolGoat' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ํšจ์œจ์ ์ธ ํ† ํฐ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ Redis ๋„์ž…
  • ์ธ์ฆ ๋ณด์•ˆ ๊ฐ•ํ™”
  • Spring Security ์ธ์ฆ ํ•„ํ„ฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ
  • ์กฐํšŒ ๋ฐฉ์‹ ๊ฐœ์„  ๋ฐ ์ธ๋ฑ์Šค ์ˆ˜์ •
yongh๐Ÿ™‚
yongh๐Ÿ™‚
yongh-dev ๋‹˜์˜ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.
  • yongh๐Ÿ™‚
    ๊ฐœ๋ฐœ ๊ธฐ๋ก
    yongh๐Ÿ™‚
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (9)
      • ํ”„๋กœ์ ํŠธ (9)
        • NolGoat (9)
  • ์ตœ๊ทผ ๊ธ€

  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.2
yongh๐Ÿ™‚
์ธ์ฆ์ด ํ•„์š”ํ•œ URL ๊ณตํ†ต ๊ด€๋ฆฌ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”