'authorizeRequests()' is deprecated 해결 || Spring Security Configuration

2023. 2. 1. 15:28·🍀 Spring Boot

기존에 사용해오던 방식대로 SecurityConfig를 만들어서 403 Error를 해결하고자 하였는데,
authorizeRequests()가 deprecated 되었다고 한다. 
WebSecurityConfigurerAdapter도 deprecate되어 빈을 등록하는 방식으로 바꿔주었었는데, 이것도 바꿔보자!

아래와 같이 변경하여 사용할 수 있다.

http
	.authorizeRequests()
	.requestMatchers("/admin/**").hasRole("ADMIN")
	.requestMatchers("/**").hasRole("USER")
	.and().formLogin();
return http.build();

antMatchers를 requestMatchers로 변경.
authorizeRequest는 deprecated로 더이상 권장되지 않기 때문에 이제는 authorizeHttpRequests()를 대신 사용하는 것을 권장한다.

 

@Bean
SecurityFilterChain web(HttpSecurity http) throws Exception {
	http
		// ...
		.authorizeHttpRequests(authorize -> authorize                                  
        .requestMatchers("/resources/**", "/signup", "/about").permitAll()         
        .requestMatchers("/admin/**").hasRole("ADMIN")                             
        .requestMatchers("/db/**").access(new WebExpressionAuthorizationManager("hasRole('ADMIN') and hasRole('DBA')"))   
        // .requestMatchers("/db/**").access(AuthorizationManagers.allOf(AuthorityAuthorizationManager.hasRole("ADMIN"), AuthorityAuthorizationManager.hasRole("DBA")))   
        .anyRequest().denyAll()                                                
		);

	return http.build();
}

Authorize HttpServletRequests with AuthorizationFilter :: Spring Security

The RequestMatcher interface is used to determine if a request matches a given rule. We use securityMatchers to determine if a given HttpSecurity should be applied to a given request. The same way, we can use requestMatchers to determine the authorization

docs.spring.io

 

반응형
'🍀 Spring Boot' 카테고리의 다른 글
  • [Error] unable to evaluate the expression method threw 'org.hibernate.lazyinitia
  • [Spring] 공통 필드 하나로 묶기! || BaseEntity, JpaAuditing
  • [Spring] 의존성 주입(DI, Dependency Injection) (생성자 주입을 사용해야 하는 이유)
  • [Spring] JPA N + 1 문제 발생 원인 및 해결 방안
dmaolon
dmaolon
프로그래밍을 공부한 내용을 기록하는 공간입니다.
  • dmaolon
    기록 남기기
    dmaolon
  • 전체
    오늘
    어제
    • ALL (260)
      • ➰ Series (5)
      • 🎯PS (168)
        • Algorithm (15)
      • ☕ Java (11)
      • 🍀 Spring Boot (29)
      • 💬 Database (9)
      • 🐣 Computer Science (14)
      • 👍 Daily (4)
      • 🎁ReactJS (4)
  • 인기 글

  • 최근 댓글

  • 최근 글

  • 태그

    dfs
    Spring
    파이썬
    프로그래밍
    자바
    BFS
    알고리즘
    백준
    프로그래머스
    코딩
  • hELLO· Designed By정상우.v4.10.1
dmaolon
'authorizeRequests()' is deprecated 해결 || Spring Security Configuration
상단으로

티스토리툴바