2015-12-09 7 views
5

oAuth2セキュリティでいくつかのAPIエンドポイントを公開しながら、標準ログインで動作するスプリングブートmvcアプリケーションを取得しようとしています。 基本的に私の要件は次のとおりです。Springブート:mvc UIページを持つ中でoauth2でapiエンドポイントを保護する

ユーザーがホームページ(「/」)を押すと、認証済みかどうかを確認します。 ログインフォームを表示しない場合は、ホームページを表示します。 しかし、ユーザーはoauth認証トークンを要求でき、そのトークンで/ api/assignment/{id}にアクセスする必要があります。

私は標準のログインが機能するようになりますが、oauth2を動作させることはできますが、一緒に動作させることはできません。

これが現時点での私の設定です:

WebSecurityConfig

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private DataSource dataSource; 

    @Override 
    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     super.configure(http);   
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {  
     auth.jdbcAuthentication().dataSource(this.dataSource).passwordEncoder(new BCryptPasswordEncoder()); 
    } 
} 

OAuth2Config

@Configuration 
@EnableResourceServer 
@EnableAuthorizationServer 
public class OAuth2Config { 

protected static final String RESOURCE_ID = "oauthdemo"; 

@Configuration 
@EnableResourceServer 
protected static class ResourceServer extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http   
     .httpBasic().disable() 
     .authorizeRequests() 
     .antMatchers("/js/**", "/css/**", "/images/**", "/webjars/**", "/login").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login") 
      .permitAll() 
      .and() 
     .logout() 
      .permitAll(); 

    } 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
     resources.resourceId(RESOURCE_ID); 
    } 
} 


@Configuration 
@EnableAuthorizationServer 
protected static class AuthServer extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    @Qualifier("authenticationManagerBean") 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
     oauthServer.allowFormAuthenticationForClients(); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authenticationManager(authenticationManager); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("client") 
       .authorizedGrantTypes("password", "refresh_token") 
       .authorities("ROLE_USER") 
       .scopes("read") 
       .resourceIds(RESOURCE_ID) 
       .secret("secret").accessTokenValiditySeconds(3600); 
    } 

} 

}

probleホームページ( "/")を開こうとすると次のエラーメッセージが表示されます

<oauth> 
<error_description> 
Full authentication is required to access this resource 
</error_description> 
<error>unauthorized</error> 
</oauth> 

ログインページにリダイレクトされません。 私はこのページをoauth2で保護する必要はありませんが、ログインページ(私がアクセスできる「/ login」)に直接アクセスして資格情報を入力しても、「完全な認証が必要です」というエラーが表示されます。 私は基本的なhttp認証を無効にしました。

OAuth2によって保護される必要があるAPIエンドポイントから通常のユーザーUIを分離する方法を知っている人はいますか?

+0

を使用して早期のために、その構成を置くことにより、別のオプションがありますが、この問題を解決するために管理していましたか? –

+0

WebSecurityConfigに「oauthを使用して保護したいすべてのURLを無視する」設定がないと思われます。多分このようなsthを使用することができます...:http://stackoverflow.com/questions/30366405/how-to-disable-spring-security-for-particular-url –

答えて

2

あなたは/家を考慮すると、この

http   
     .authorizeRequests() 
     .antMatchers("/js/**", "/css/**", "/images/**", "/webjars/**", "/login").permitAll() 
     .antMatchers("/login").permitAll() 
     .antMatchers("/home").authenticated(); 

を試すことができますが、許可する必要がありますページです。

+0

提案していただきありがとうございますが、それは同じエラーメッセージを持っているホームページのみを認証し、現在はルートページ( "/")は認証されていません –

1

こんにちは、あなたが必要とする、あなたのケースで各設定のために使用されるフィルタを指定する必要があります。Webセキュリティconfigurtionで

 http 
     .authorizeRequests() 
      .antMatchers("/api/**","/oauth/**") 
      .permitAll() 
      .and() 
      ....... 

これは、Webセキュリティバイパスの許可/リソースサーバのURLに

をできるようになりますおよびリソースサーバーのセキュリティ構成で

 http 
     .antMatcher("/api/**") 
      .authorizeRequests() 
      .anyRequest() 
      .authenticated(); 

これにより、リソースセキュリティは "/ api/**"を除くすべてのURLをバイパスできます。あなたが設定の命令を無視することができ、このように

上記のいずれかの操作を行い、@Order

関連する問題