2017-03-15 14 views
2

私はOAuth2 Securityを備えたSpring Boot REST APIを持っています。SpringBoot 1.5.x + Security + OAuth2

今日はspring-boot-starter-parentのバージョンを1.4.2から1.5.2にアップグレードしました。

変更が私を完全に混乱させました。

以前、私はREST APIをPostmanでテストできました。私のアクセストークンが誤っていたか、私は特定のリソースの権限を持っていませんでした場合、サーバーの応答は次のようでした:

{ 
    "error": "access_denied", 
    "error_description": "Access is denied" 
} 

今では... /loginページに私をリダイレクトし続ける私がログインするとき - それはどんなことなく、私のリソースを表示OAuth2認証は...

私はそれを無効にすることを試みていると私は、この魔法のプロパティを見つけました:

security.oauth2.resource.filter-order = 3 

この行には、ログインページにリダイレクトをオフに。

はしかし、私の質問は以下のとおりです。

  • セキュリティの用語では、これら2つのリリース間で何が起こったのか?
  • これは "奇妙な"行だけが有効な修正ですか?
  • このログインページの目的と使用している認証(Google Chromeで要求と応答を確認しましたが、アクセストークンとoauth2の情報が表示されないため、ユーザーリポジトリのみを使用しています)
  • 私のコードの

いくつかのより重要な部分:

のpom.xml

<!--- .... --> 
<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.2.RELEASE</version> 
</parent> 
<properties> 
    <!--- .... --> 
    <spring-security-oauth.version>2.1.0.RELEASE</spring-security-oauth.version> 
    <!--- .... --> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <!-- Monitor features --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-actuator</artifactId> 
    </dependency> 
    <!-- Security + OAuth2 --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security.oauth</groupId> 
     <artifactId>spring-security-oauth2</artifactId> 
     <version>${spring-security-oauth.version}</version> 
    </dependency> 
<!--- .... --> 

application.properties

#other properties 
security.oauth2.resource.filter-order = 3 

OAuth2.java

public class OAuth2 { 
@EnableAuthorizationServer 
@Configuration 
@ComponentScan 
public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManagerBean; 
    @Autowired 
    private UserDetailsService userDetailsService; 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("trusted_client") 
       .authorizedGrantTypes("password", "refresh_token") 
       .scopes("read", "write"); 
    } 

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

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

@EnableResourceServer 
@Configuration 
@ComponentScan 
public static class ResourceServer extends ResourceServerConfigurerAdapter { 

    @Autowired 
    private RoleHierarchy roleHierarchy; 

    private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() { 
     DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler(); 
     defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy); 
     return defaultWebSecurityExpressionHandler; 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests().expressionHandler(webExpressionHandler()) 
       .antMatchers("/api/**").hasRole("DEVELOPER"); 
    } 
} 
} 

Security.java

@EnableWebSecurity 
@Configuration 
@ComponentScan 
public class Security extends WebSecurityConfigurerAdapter { 

@Autowired 
private UserDetailsService userDetailsService; 

@Bean 
public JpaAccountDetailsService userDetailsService(AccountsRepository accountsRepository) { 
    return new JpaAccountDetailsService(accountsRepository); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 
} 

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

@Bean 
public PasswordEncoder passwordEncoder(){ 
    return new BCryptPasswordEncoder(); 
} 
} 

答えて

7

[OK]を今すぐ取得しました。

@Cleto Gadelhaは非常に有用な情報を教えてくれました。

しかし、私はリリースノートがかなり不明であるとか、いくつかの情報を見逃していると思います。 OAuth2リソースフィルタが3からSecurityProperties.ACCESS_OVERRIDE_ORDER - 1に変更されている以外は、重要な情報はデフォルトのWebSecurityConfigurerAdapterの順番が100 (source)です。

したがって、リリース1.5.x以前のOAuth2リソースサーバーの注文は、が高く、が高く、WebSecurityConfigurerAdapterだった3でした。リソースサーバの順序がSecurityProperties.ACCESS_OVERRIDE_ORDER - 1 に設定されているのOAuth2リリース1.5.xの後

(それは私が考えるInteger.MAX_VALUE - 8です)今、間違いなく優先その後、基本的なWebSecurityConfigurerAdapter下を持っています。ログインページが私のために表示される理由の後にそう

を1.5.xのために1.4.xのからの移行だ

、よりエレガントでJavaに似たスタイルのソリューションは、WebSecurityConfigurerAdapterクラス

+0

ニース調査に@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)を設定することです。ありがとうございました。 –

3

あなたの第一および第二の質問に対する答えはSpring Boot 1.5 Release Notesである:

のOAuth 2リソースフィルター

のOAuth2リソースフィルタのデフォルトの順序は、 SecurityProperties.ACCESS_OVERRIDE_ORDERに3から変更されている - これは アクチュエータのエンドポイントの後が、基本的な認証フィルタチェーンの前にそれを配置します1.。 security.oauth2.resourceを設定すると、デフォルトに戻すことができます。filter-order = 3

/loginページは、許可されていないユーザーをリダイレクトするパスです。 Custom Login Formを使用しておらず、Oauth2フィルタの位置が間違っているため、おそらくBasic Authを使用していました。

関連する問題