2017-02-08 4 views
0

기본 인증과 OAuth2를 모두 사용하는 앱이 있습니다.내 기본 인증 구성을 통해 "/ api/**"를 스프링 보안의 oauth 구성으로 허용하는 방법

일부 인증은 기본 인증을 사용하며 "/ api/**"는 OAuth2를 사용하여 인증됩니다.

현재, 나는 두 개의 자바 설정 파일 (WebSecurityConfigurerAdapterResourceServerConfigurerAdapter)

설정 파일의 각각은 public void configure(HttpSecurity http) 방법을 정의 할 수 있습니다.

내가 겪고있는 문제는 URL 요청을 받으면 기본 인증을 사용할지 아니면 oauth2를 사용할지 여부를 알기 쉽게 말해주는 것입니다. 나는이 일어날 수 있도록 requestMatchers를 사용하고 현재

:

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
{ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception 
    { 
    http 
     .csrf().disable() 
    .requestMatchers() 
     .antMatchers("/*", "/login/**", "/reviews/**") 
    .and() 
    .authorizeRequests() 
     .antMatchers("/*").permitAll() 
     .antMatchers("/js/**").permitAll() 
     .antMatchers("/img/**").permitAll() 
    .formLogin() 
     .loginPage("/login") 
     .successHandler(loginSuccessPostHandler) 
     .permitAll() 
     .and() 
    .logout() 
     .logoutSuccessUrl("/").permitAll() 
     .and() 
    .apply(getSpringSocialConfigurer()); 
    } 
} 

@Configuration 
public class OAuth2ServerConfig 
{ 
    @Configuration 
    @EnableResourceServer 
    protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter 
    { 
    @Override 
    public void configure(HttpSecurity http) throws Exception 
    { 
     http.httpBasic().disable(); 
     http.csrf().disable(); 

     http.requestMatchers() 
     .antMatchers("/api/**") 
     .and() 
     .authorizeRequests() 
     .antMatchers("/api/v1/**").access("#oauth2.hasScope('read')"); 
    } 
    } 
} 

문제는 때마다 내가 아니라 새로운 URL을 추가 할 것입니다 "/ API/**", 나는 그것을 추가해야합니다 내 WebSecurityConfig의 requestMatcher 섹션에 ... 이것은 앞으로 바보 같은 버그가 될 수 있습니다.

negative lookahead regex에 기반한 requestMatcher 검색 방법이 있습니까? 나는이 정규식을 사용하여 시도 : ^(?!/api)하지만 실제로 일치를 반환하지 않으며 "찾기 == 사실"을 반환하기 때문에, 그것은 일을 끝내지 않는 것 같습니다.

의견/제안?

답변

2

당신은 NegatedRequestMatcher를 사용할 수 있습니다.

RequestMatcher을 부정하는 RequestMatcher이 전달 예를 들어, RequestMatcher가 true를 반환 전달 경우, NegatedRequestMatcher는 false를 돌려줍니다. 전달 된 RequestMatcher가 false를 반환하면 NegatedRequestMatcher가 true를 반환합니다.

0

@Configuration 클래스에 Order(...) 주석을 사용해야합니다. OAuth2ServerConfig을 먼저 설정하고 http.requestMatchers().antMatchers("/api/**")에만 게재하고 없이 WebSecurityConfig 초 (@Order(2))를 제외한 나머지 URL을 모두 제공하십시오!

세부 정보보기 https://stackoverflow.com/a/44871933/173149