0

oauth2 (리소스 서버)와 올바르게 작동하는 스프링 부팅 응용 프로그램이 있습니다. 사용자 정의 configure(HttpSecurity http) 메소드가 없습니다. 전용oauth2를 사용하여 스프링 보안을위한 사용자 지정 구성을 추가 할 수 없습니다.

spring-boot-starter-security 
spring-security-oauth2 
spring-security-jwt 

이 pom에 추가됩니다.

이제 보호되지 않아야하는 끝점을 추가하고 싶습니다. 그래서 (많은 SO 응답을 다음) 내가 추가 시작 :

@Configuration 
public class Security extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
    } 
} 

을하고 내가 가지고 :

Cannot apply org.springframework.security.config.[email protected]6513fd22 to already built object

전체 오류 :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.[email protected]6513fd22 to already built object 

그래서 내가 어떻게 추가 내 보안을 구성해야합니다 익명 액세스를위한 끝점?

답변

1

configure() 방법의 비어있는 부분에서 오류가 발생합니다.

명시 적으로 지정해야합니다. 예를 들어 (우리의 작업 응용 프로그램에서) : /unprotected/sample/find/**/unprotected/sample/find/** 일치

@Override 
public void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
      .antMatchers("/unprotected/sample/find/**").permitAll() 
      .antMatchers("/unprotected/another/register").permitAll() 
      .anyRequest().authenticated().and() 
      .csrf().disable(); 
} 

엔드 포인트를 보호하고 다른 모든이 보호됩니다.

물론 보호되지 않은 끝점에는 @PreAuthorize()이 정의되어서는 안됩니다.