2015-04-23 40 views
2

我想要的oauth2支持添加到我的Java REST服務,遷移春天的OAuth分貝JDBC

我已經manged得到它在內存中的工作:

protected static class MyAuthorizationServerConfigurerAdapter extends 
      AuthorizationServerConfigurerAdapter { 
... 
    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients 
      .inMemory() 
       .withClient("myapp") 
        .authorizedGrantTypes("password", "refresh_token") 
        .authorities("USER") 
        .scopes("read", "write") 
        .resourceIds(RESOURCE_ID) 
        .secret("mysecret"); 
     } 
... 
    } 

問題,每時間服務器重新啓動數據庫丟失。 所以我想連接它和現有的獨立SQL數據庫,任何想法或指導如何做到這一點?

我發現有一個JDBC的選擇,但我不能找到一種方法,使工作

clients.jdbc(dataSource) 

謝謝

回答

3

你確定要放在客戶端配置數據庫?這隻會讓你動態配置新的客戶端。

我想你想要的是讓你的令牌保存在數據庫中,所以如果你重新啓動你的服務器,客戶端不會失去他們的會話。

@Override 
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception } 
    endpoints.tokenStore(new JdbcTokenStore(dataSource)).userApprovalHandler(userApprovalHandler) 
        .authenticationManager(authenticationManager); 
} 

下面是創建數據庫表中的代碼:

這可以用下面的代碼進行

CREATE TABLE `oauth_access_token` (
    `token_id` varchar(255) DEFAULT NULL, 
    `token` blob, 
    `authentication_id` varchar(255) DEFAULT NULL, 
    `user_name` varchar(255) DEFAULT NULL, 
    `client_id` varchar(255) DEFAULT NULL, 
    `authentication` blob, 
    `refresh_token` varchar(255) DEFAULT NULL 
); 

CREATE TABLE `oauth_refresh_token` (
    `token_id` varchar(256) DEFAULT NULL, 
    `token` blob, 
    `authentication` blob 
); 

來源:https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql

+0

我願把令牌在db中也是如此,但這將是下一步,將它們都做得更好 – user3394220