2015-04-23 45 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