2017-11-21 20 views
1

私は安らかなサービスにspring oauth2を追加しました。ほとんどのサービスは私自身のポータルで消費されるので、apiを呼び出してトークンを取得するのは問題ありません。しかし、私はこのトークンの概念なしで呼び出さなければならないいくつかのWebサービスを公開しています。それらの消費者はユーザー名とパスワードを持っています。スプリングoauth2とダイジェスト認証が一緒に動作する

スワッガーの実装例が最も優れています。スワッガーページを開くには、oauthトークンの代わりにダイジェストを使用して認証する必要があります。

私は以下のコードを変更しましたが、動作しません。 この場合、リソースサーバーからoauthサーバーを呼び出す必要はありません。リソースサーバー内で以下のコードを作成しました。しかし、認証ページが自分の資格情報を受け入れた後、同じ認証ページに再度ルーティング/リダイレクトされるような問題が見られました。

助けてください。 UPDATE

@Configuration 
@EnableResourceServer 
public class ResourceServerImpl extends ResourceServerConfigurerAdapter { 


    @Override 
    public void configure(HttpSecurity http) throws Exception {  
     http.anonymous().disable().requestMatchers().antMatchers("/**").and().authorizeRequests() 
     .antMatchers(HttpMethod.POST, "/url1/path1/path2").hasAnyAuthority("FUNCTION_FN1") 
     .antMatchers(HttpMethod.GET, "/url2/path1/path2").hasAnyAuthority("FUNCTION_FN2") 
     .antMatchers("/swagger-ui.html").hasRole("USER") 
     .anyRequest().authenticated() 
     .and().formLogin().and().httpBasic() 
     .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler()); 
    } 
} 


@Configuration 
@EnableWebSecurity 
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Autowired 
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{ 
     auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
    } 
} 

私は、これはDIGESTといくつかのOAuthのであると認証されるいくつかの要求を行うことが可能であると思ういけません。だから私は、ダイジェストサポートの代わりにoauthトークンで認証されるために、今も笑いのURLを作った。

+0

あなたが設定に関するすべてのコードを提供しなかったので、なぜあなたは私がしなければならないものを、コードの変更*求めるだろうdo?* –

+0

@AtaurRahmanMunna確かに、私は提供しますが、それは実現可能です...? –

+0

*ただし、私はいくつかのWebサービス*を公開しています* - エンドポイントは他の消費者とoauth2の消費者の間で異なります。 –

答えて

1

はい、同じアプリケーションで複数の認証メカニズムを構成することは間違いありません。 WebSecurityConfigurerAdapterという別個のインスタンスが、各メカニズムごとに提供される必要があります。 @EnableResourceServerおよび@EnableAuthorizationServerは、対応するoauthアダプタを提供します。 ApiSecurityConfigurationは、基本認証のためのアダプタを提供します。ここで

basicoauth2のための最小限の作業サンプルです:

@SpringBootApplication 
public class TestApp { 

    @RestController 
    public static class Endpoints { 

     // doesn't require any authentication 
     @RequestMapping("/test") 
     public String test() { 
      return "no auth"; 
     } 

     // requires basic authentication 
     @RequestMapping("/api/test") 
     public String apiTest() { 
      return "basic auth"; 
     } 

     // requires oauth authentication 
     @RequestMapping("/oauth/test") 
     public String oauthTest() { 
      return "oauth"; 
     } 
    } 

    @Configuration 
    @EnableWebSecurity 
    public static class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.requestMatchers().antMatchers("/api/**") 
        .and() 
        .authorizeRequests().antMatchers("/api/**").hasRole("USER") 
        .and() 
        .httpBasic(); 
     } 

     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication() 
        .withUser("user") 
        .password("password") 
        .roles("USER"); 
     } 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    public static class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter { 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      clients.inMemory() 
        .withClient("test") 
        .secret("test") 
        .authorizedGrantTypes("client_credentials") 
        .authorities("USER") 
        .scopes("read", "write", "test") 
        .resourceIds("oauth2-resource") 
        .accessTokenValiditySeconds(120); 
     } 
    } 

    @Configuration 
    @EnableResourceServer 
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http.requestMatchers().antMatchers("/oauth/**") 
        .and() 
        .authorizeRequests().antMatchers("/oauth/**").authenticated(); 
     } 
    } 

    public static void main(String[] args) { 
     SpringApplication.run(TestApp.class, args); 
    } 
} 

pom.xml

<build> 
    <plugins> 
     <plugin> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
    </build> 

    <dependencies> 
    <dependency> 
     <groupId>org.springframework.security.oauth</groupId> 
     <artifactId>spring-security-oauth2</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    </dependencies> 
関連する問題