20

我有一个当前使用 Spring OAuth 2.0 In Memory Token Store 的应用程序。我需要将 Spring Security OAuth 2.0 JAR 转换为使用持久文件而不是内存中的文件,以确保访问令牌在服务器重新启动时有效。Spring OAuth 2.0 JAR 提供了使用 JdbcTokenStore 方法支持 MYSQL 数据库的例程,但我找不到任何说明如何更改默认配置(使用 InMemoryTokenStore 方法)以利用受支持的 Jdbc 方法的文档。

我想听听已经实现 Spring Security OAuth 2.0 JdbcTokenStore 方法的人的意见,他们可以提供执行此操作所需的配置示例,也可以将我指向描述该过程的文档。我在互联网上到处搜索,但找不到任何此类文档。

我已经找到了 Token Store 的 Spring Security OAuth 2.0 模式文件,如果有人感兴趣的话,只能在 Test Resource 目录中找到。任何 Pivotal 文档网站都没有记录它的存在。如有必要,我可以通读 Pivotal 源代码的其余部分,但我希望有人可以让我免于使用此路径。

提前感谢您提供的任何帮助。

4

2 回答 2

22

您需要将 bean 实现类从 InMemoryTokenStore 更改为 JdbcTokenStore。通过这种更改,您还需要在构造函数中传递一个数据源。

我已经在玩弄它的同时做到了这一点。你可以在这里找到

并且 spring 安全配置在此处专门更改。MySql 架构在这里

于 2014-01-12T05:23:50.490 回答
5

我就是这样做的。

第 1 步:创建 2 个表(oauth_access_token 和 oauth_refresh_token)

CREATE TABLE `oauth_access_token` (
`authentication_id` varchar(255) NOT NULL,
`token_id` varchar(255) NOT NULL,
`token` blob NOT NULL,
`user_name` varchar(255) NOT NULL,
`client_id` varchar(255) NOT NULL,
`authentication` blob NOT NULL,
`refresh_token` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `oauth_access_token`
ADD PRIMARY KEY (`authentication_id`);


CREATE TABLE `oauth_refresh_token` (
`token_id` varchar(255) NOT NULL,
`token` blob NOT NULL,
`authentication` blob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

第二步:配置 AuthorizationServerConfig 类

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private TokenStore tokenStore;

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("my-trusted-client")
            .authorizedGrantTypes("client_credentials", "password","refresh_token")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT").scopes("read", "write", "trust")
            .resourceIds("oauth2-resource")
            .accessTokenValiditySeconds(5000)
            .refreshTokenValiditySeconds(50000)
            .secret(passwordEncoder.encode("secret"));
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.checkTokenAccess("isAuthenticated()");
}

}

第 3 步:

@Configuration
public class AppConfig {

@Value("${spring.datasource.url}")
private String datasourceUrl;

@Value("${spring.datasource.driver-class-name}")
private String dbDriverClassName;

@Value("${spring.datasource.username}")
private String dbUsername;

@Value("${spring.datasource.password}")
private String dbPassword;

@Bean
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(dbDriverClassName);
    dataSource.setUrl(datasourceUrl);
    dataSource.setUsername(dbUsername);
    dataSource.setPassword(dbPassword);
    return dataSource;
}

@Bean
public TokenStore tokenStore() {
    return new JdbcTokenStore(dataSource());
}
}
于 2018-04-28T09:25:45.657 回答