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

@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(); }
requestMachers 와 antMachers
- 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)
사용예시
// 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빼고 인가허용하는 방법
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')")
- 주석처리 되어 있는 부분 처럼 access를 활용하여 직접 SpEL 을 명시하여 GUEST를 가질 때로 작성하면, Prefix 빠지고 가능함
- 또는 hasAuthority(”GUEST”) 로도 가능함
hasRole vs hasAuthority
- hasRole은 role related method에서 ROLE_이라는 prefix를 붙여줌
- hasAuthority는 prefix가 하나도 안붙는다는 장점!