2016-01-27 19 views
6

私は次のようにメモリの事で作業してい:SpringでClientDetailsS​​erviceConfigurerのJDBCを使用してクライアントを追加する方法は?

@Override 
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 

     clients.inMemory() 
       .withClient("clientapp") 
       .authorizedGrantTypes("password", "refresh_token") 
       .authorities("USER") 
       .scopes("read", "write") 
       .resourceIds(RESOURCE_ID) 
       .secret("123456"); 
} 

私はJDBCの実装を使用したいと思います。このために、私は(MySQLを使用して)次の表作成しました:

-- Tables for OAuth token store 

CREATE TABLE oauth_client_details (
    client_id    VARCHAR(255) PRIMARY KEY, 
    resource_ids   VARCHAR(255), 
    client_secret   VARCHAR(255), 
    scope     VARCHAR(255), 
    authorized_grant_types VARCHAR(255), 
    web_server_redirect_uri VARCHAR(255), 
    authorities    VARCHAR(255), 
    access_token_validity INTEGER, 
    refresh_token_validity INTEGER, 
    additional_information VARCHAR(4096), 
    autoapprove    TINYINT 
); 

CREATE TABLE oauth_client_token (
    token_id   VARCHAR(255), 
    token    BLOB, 
    authentication_id VARCHAR(255), 
    user_name   VARCHAR(255), 
    client_id   VARCHAR(255) 
); 

CREATE TABLE oauth_access_token (
    token_id   VARCHAR(255), 
    token    BLOB, 
    authentication_id VARCHAR(255), 
    user_name   VARCHAR(255), 
    client_id   VARCHAR(255), 
    authentication BLOB, 
    refresh_token  VARCHAR(255) 
); 

CREATE TABLE oauth_refresh_token (
    token_id  VARCHAR(255), 
    token   BLOB, 
    authentication BLOB 
); 

CREATE TABLE oauth_code (
    code   VARCHAR(255), 
    authentication BLOB 
); 

を私は手動でMySQLのテーブルにクライアントを追加する必要がありますか?春は良いのテーブルに正しいものを挿入することを期待して

clients.jdbc(dataSource).withClient("clientapp") 
       .authorizedGrantTypes("password", "refresh_token") 
       .authorities("USER") 
       .scopes("read", "write") 
       .resourceIds(RESOURCE_ID) 
       .secret("123456"); 

が、それを行うにはいないようです:

は、私はこれを試してみました。なぜあなたは jdbc()の後にさらにチェーンすることができますか?

答えて

0

この質問はかなり古いですが、質問者の元の問題に対する回答はありませんでした。 ClientDetailsServiceConfigurerがプログラム(コンフィギュラ上jdbc(datasource)メソッドを呼び出すことによってインスタンス化される)JdbcClientDetailsServiceBuilder経由で追加されたクライアントを永続化されていませんなぜ私はそのすべてにもかかわらず、春ののOAuth2実装となじみ、自分自身を取得しながら、同じ問題でつまずいと不思議に思っていましたネット上のチュートリアルでは、Wimによって投稿されたものと同様の例が示されました。コードを深く掘り下げた後、私はその理由に気づいた。つまり、oauth_clients_detailsテーブルを更新するコードが呼び出されないからです。欠けているものは、すべてのクライアントを設定した後の次の呼び出しです:.and().build()。以下のように、ヴィムのコードは、実際に見なければならない:

clients.jdbc(dataSource).withClient("clientapp") 
      .authorizedGrantTypes("password", "refresh_token") 
      .authorities("USER") 
      .scopes("read", "write") 
      .resourceIds(RESOURCE_ID) 
      .secret("123456").and().build(); 

ほら、クライアントclientappは現在のデータベースに保存されます。

8

この手順を休耕してください:
あなたのサーバを起動したらSpringBootによって検出されるように、あなたのリソースフォルダ内のこのschema.sqlを置く

  1. 。あなたは春のブートを使用しない場合は何も心配はただのMySQLのアプリケーションクライアントからこのスクリプトをインポートしていない(phpMyAdminは、HeidiSQL、Navicatは...)

    drop table if exists oauth_client_details; create table oauth_client_details ( client_id VARCHAR(255) PRIMARY KEY, resource_ids VARCHAR(255), client_secret VARCHAR(255), scope VARCHAR(255), authorized_grant_types VARCHAR(255), web_server_redirect_uri VARCHAR(255), authorities VARCHAR(255), access_token_validity INTEGER, refresh_token_validity INTEGER, additional_information VARCHAR(4096), autoapprove VARCHAR(255) ); drop table if exists oauth_client_token; create table oauth_client_token ( token_id VARCHAR(255), token LONG VARBINARY, authentication_id VARCHAR(255) PRIMARY KEY, user_name VARCHAR(255), client_id VARCHAR(255) ); drop table if exists oauth_access_token; create table oauth_access_token ( token_id VARCHAR(255), token LONG VARBINARY, authentication_id VARCHAR(255) PRIMARY KEY, user_name VARCHAR(255), client_id VARCHAR(255), authentication LONG VARBINARY, refresh_token VARCHAR(255) ); drop table if exists oauth_refresh_token; create table oauth_refresh_token ( token_id VARCHAR(255), token LONG VARBINARY, authentication LONG VARBINARY ); drop table if exists oauth_code; create table oauth_code ( code VARCHAR(255), authentication LONG VARBINARY ); drop table if exists oauth_approvals; create table oauth_approvals ( userId VARCHAR(255), clientId VARCHAR(255), scope VARCHAR(255), status VARCHAR(10), expiresAt TIMESTAMP, lastModifiedAt TIMESTAMP ); drop table if exists ClientDetails; create table ClientDetails ( appId VARCHAR(255) PRIMARY KEY, resourceIds VARCHAR(255), appSecret VARCHAR(255), scope VARCHAR(255), grantTypes VARCHAR(255), redirectUrl VARCHAR(255), authorities VARCHAR(255), access_token_validity INTEGER, refresh_token_validity INTEGER, additionalInformation VARCHAR(4096), autoApproveScopes VARCHAR(255) );
  2. あなたOthorizationServer

    @Autowired private MyUserDetailsService userDetailsService; @Inject private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource;内、UserDetailsS​​erviceをあなたのデータソース、authenticationManagerを注入
  3. あなたは、この2つのBean

    @Bean public JdbcTokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean protected AuthorizationCodeServices authorizationCodeServices() { return new JdbcAuthorizationCodeServices(dataSource); }

    を作成する必要がありますし、@Configuを忘れないでくださいあなたはすでにこれをやったclients.jdbc(dataSource).withClient("clientapp") .authorizedGrantTypes("password", "refresh_token") .authorities("USER") .scopes("read", "write") .resourceIds(RESOURCE_ID) .secret("123456");

    :あなたのAuthorizationServerクラスのトップ

  4. の比は、MySQLデータベース内に作成することがあなたのクライアントアプリケーションを構成します。

  5. 最も重要なこと(と私はあなたがそれについて忘れてしまったと思います。)です。AuthorizationServerEndpointsConfigurerを使用してエンドポイントを設定するには:

    endpoints.userDetailsService(userDetailsService) .authorizationCodeServices(authorizationCodeServices()).authenticationManager(this.authenticationManager).tokenStore(tokenStore()).approvalStoreDisabled();

、それは男、それだ、今動作するはずです;)

のそしてより多くのためにお気軽に...私がお手伝いさせていただきます

私はツイーターからあなたにメッセージを送った!

+0

私はあなたの答えに従っていましたが、私がそのアイデアを述べたとおりにサーバーを構成しました。しかし、私は次のエラーが発生しています。 {"timestamp":1458109110864、 "status":401、 "error": "Unauthorized"、 "message": "クラスパスリソースで定義された名前 'scopedTarget.clientDetailsS​​ervice' springframework /セキュリティ/のOAuth2 /設定/注釈/設定/ ClientDetailsS​​erviceConfiguration.class]:ファクトリメソッドを経由してビーンのインスタンス化に失敗しました。ネストされた例外はorg.springframework.beans.BeanInstantiationExceptionです: – Waruna

+0

は[org.springframework.security.oauth2.providerをインスタンス化できませんでした。 ClientDetailsS​​ervice]:ファクトリメソッド 'clientDetailsS​​ervice'が例外をスローしました;ネストされた例外はorg.springframework.jdbc.BadSqlGrammarExceptionです:PreparedStatementCallback;不正なSQL文法[client_secret、resource_ids、scope、authorized_grant_types、web_server_redirect_uri、authority、access_token_validity、refresh_token_validity、additional_information、自動承認、クライアントID)の値(?、?、?、?、?、?、?、?、?、?、?)]; – Waruna

+0

のネストされた例外はorg.h2.jdbc.JdbcSQLExceptionです:テーブル\ "OAUTH_CLIENT_DETAILS \"が見つかりません。 SQL文:\ ninsert oauth_client_detailsに(client_secret、resource_ids、スコープ、authorized_grant_types、web_server_redirect_uri、当局、access_token_validity、refresh_token_validity、additional_information、autoapprove、CLIENT_ID)の値(、、、、、、、、、????????? ?、?)[42102-190]」、 『パス』: 『/ OAuthの/トークン』} – Waruna

0

@AndroidLoverの回答は良いですが、単純化することができます。 jdbcトークンストアが必要な場合を除き、oauth_access_token、oauth_refresh_tokenなどのテーブルを作成する必要はありません。

jdbcクライアント詳細サービスのみが必要なので、実行する必要があるのは次のとおりです。
1。例えば、oauth_client_detailsクライアントの詳細テーブルを作成します。

drop table if exists oauth_client_details; 
    create table oauth_client_details (
    client_id VARCHAR(255) PRIMARY KEY, 
    resource_ids VARCHAR(255), 
    client_secret VARCHAR(255), 
    scope VARCHAR(255), 
    authorized_grant_types VARCHAR(255), 
    web_server_redirect_uri VARCHAR(255), 
    authorities VARCHAR(255), 
    access_token_validity INTEGER, 
    refresh_token_validity INTEGER, 
    additional_information VARCHAR(4096), 
    autoapprove VARCHAR(255) 
    ); 

2.、あなたはMyBatisのを使用することができます(たとえば、私はこのケースでは春のJPAを使用しています、UserDetailインタフェースを実装するユーザモデルを作成し、JDBC 、何でも):

@Entity 
@Table(name = "users") 
public class User implements UserDetails { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "user_id", nullable = false, updatable = false) 
private String id; 

@Column(name = "username", nullable = false, unique = true) 
private String username; 

@Column(name = "password", nullable = false) 
private String password; 

@Column(name = "enabled", nullable = false) 
@Type(type = "org.hibernate.type.NumericBooleanType") 
private boolean enabled; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

public void setEnabled(boolean enabled) { 
    this.enabled = enabled; 
} 

@Override 
public Collection<? extends GrantedAuthority> getAuthorities() { 
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 
    authorities.add((GrantedAuthority)() -> "ROLE_USER"); 
    return authorities; 
} 

@Override 
public String getPassword() { 
    return this.password; 
} 

@Override 
public String getUsername() { 
    return this.username; 
} 

@Override 
public boolean isAccountNonExpired() { 
    return true; 
} 

@Override 
public boolean isAccountNonLocked() { 
    return true; 
} 

@Override 
public boolean isCredentialsNonExpired() { 
    return true; 
} 

@Override 
    public boolean isEnabled() { 
     return this.enabled; 
    } 
} 

3.カスタムユーザー詳細サービスを作成します。あなたの実装では、あなたのDAOのサービスを注入すべきである旨の通知(私の場合、私はjpaRepositoryを注入した。)と、あなたのDAOサービスMUSTは、ユーザ名でユーザを検索する方法を持っている:

ついに
@Service("userDetailsService") 
public class UserService implements UserDetailsService { 

@Autowired 
UserRepository userRepository; 

@Override 
public UserDetails loadUserByUsername(String userName) throws 
UsernameNotFoundException { 
    return userRepository.findByUsername(userName); 
} 
} 

4あなたの認証サーバーを設定してください:

@Configuration 
@EnableAuthorizationServer 
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter { 

@Autowired 
@Qualifier("dataSource") 
DataSource dataSource; 

@Autowired 
@Qualifier("userDetailsService") 
private UserDetailsService userDetailsService; 


@Autowired 
private AuthenticationManager authenticationManager; 

@Override 
public void configure(AuthorizationServerEndpointsConfigurer configurer) { 
    configurer 
      .authenticationManager(authenticationManager)     
      .approvalStoreDisabled() 
      .userDetailsService(userDetailsService); 
} 


@Override 
public void configure(ClientDetailsServiceConfigurer clients) throws Exception 
{ 
    clients 
      .jdbc(dataSource) 
      .inMemory().withClient("my-trusted- 
client").secret("secret").accessTokenValiditySeconds(3600) 
      .scopes("read", "write").authorizedGrantTypes("password", 
"refresh_token").resourceIds("resource"); 
} 
} 
関連する問題