HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
🔐
Spring Security
/
endpoint 인가& 인증 적용 방법
endpoint 인가& 인증 적용 방법
endpoint 인가& 인증 적용 방법

endpoint 인가& 인증 적용 방법

보안 적용 메소드requestMachers 와 antMachers사용예시FullyAuthenticated권한 체크 시 ROLE_USER에서 Prefix빼고 인가허용하는 방법hasRole vs hasAuthority

보안 적용 메소드

notion image
@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/spiiters/me").hasAuthority("ROLE_SPITTER") .antMatchers(HttpMethod.POST,".spittles").hasAuthority("ROLE_SPITTER") .anyRequest().permitAll(); }
해당 endpoint 에 ROLE_SPITTER 권한이 있어야 한다는 것

requestMachers 와 antMachers

[stack overflow 참고]
  • requestMatchers
    • public C requestMatchers(RequestMatcher... requestMatchers)
    • For all the incoming http requests, i want to filter them based on this list of matchers(RequestMacher)
  • antMatchers의 메서드 시그니처
    • public C antMatchers(HttpMethod method)
    • public C antMatchers(HttpMethod method, String... antPatterns)
    • public C antMatchers(String... antPatterns)
    • 참고 : https://codingdog.tistory.com/entry/스프링-시큐리티에서-많이-이용되는-ant-경로-패턴에-대해-알아봅시다
      참고 : https://codingdog.tistory.com/entry/스프링-시큐리티에서-많이-이용되는-ant-경로-패턴에-대해-알아봅시다

사용예시

// requestMatchers 이용 web.ignoring() .requestMatchers( PathRequest.toStaticResources().atCommonLocations() ); // antMachers 이용 @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.cors() .and() .csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER) .and() .authorizeRequests() .antMatchers("/accounts/login", "/admins/login", "/accounts/password/reset", "/admins/password/reset", "/admins/email/find", "/docs", "/health-check/ping").permitAll() .antMatchers(POST, "/accounts").permitAll() .antMatchers(GET, "/departments").hasRole("USER") .antMatchers("/inspections/**", "/organizations/**", "/departments/**", "/admins/**").hasRole("ADMIN") .antMatchers(POST, "/accounts/status").permitAll() .anyRequest().authenticated(); http.addFilterBefore(jwtAuthenticationFilter(), HeaderWriterFilter.class); return http.build(); }
  • 위의 예시처럼 antMatchers는 먼저 선언된 것을 우선시 함
    • .antMatchers(GET, "/departments").hasRole("USER") 를 선언하고 그 후에 hasRole(”ADMIN”)으로 선언하면 Role은 User 로 설정됨

FullyAuthenticated

isFullyAuthenticated(), which returns true if the user isn't an anonymous or remember-me user:

권한 체크 시 ROLE_USER에서 Prefix빼고 인가허용하는 방법

[참고] GrantedAuthorityDefaults does not apply on ExpressionInterceptUrlRegistry#hasRole
http.authorizeRequests( request -> request .antMatchers(HttpMethod.POST, "/api/users/**").permitAll() .antMatchers(HttpMethod.GET, "/api/profiles/*").permitAll() .antMatchers(HttpMethod.GET, "/api/articles/*").permitAll() .anyRequest().hasRole("GUEST")) // .anyRequest().access("hasRole('GUEST')")
위와 같이 hasRole로 “GUEST” 명시해주면 알아서 ROLE_GUEST로 체크를 하게 됨
  • 주석처리 되어 있는 부분 처럼 access를 활용하여 직접 SpEL 을 명시하여 GUEST를 가질 때로 작성하면, Prefix 빠지고 가능함
  • 또는 hasAuthority(”GUEST”) 로도 가능함

hasRole vs hasAuthority

  • hasRole은 role related method에서 ROLE_이라는 prefix를 붙여줌
  • hasAuthority는 prefix가 하나도 안붙는다는 장점!