文章目录
前言
上一篇 OAuth2与spring-security-oauth2
基于上文配置好之后继续
client数据库配置
java配置
之前在DemoAuthorizationServerConfiguration中的配置
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients.inMemory()
.withClient("test-client").secret("$2a$08$YGw560YLRWHg3Hl29ZlmdOfAeyRQ2u0kDiqUyQ62Y1pkW5n4a.hjO")
.authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
.authorities("ROLE_CLIENT").redirectUris("http://localhost:8084/oauth/callback")
.scopes("read", "write", "all");// 请求参数scope必须为集合中的某个值
// @formatter:on
}
这里使用的是内存型的ClientDetailsService的实现类InMemoryClientDetailsService,现在需要配置成数据库型ClientDetailsService的实现类JdbcClientDetailsService
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(new JdbcClientDetailsService(dataSource));
}
sql脚本
使用JdbcClientDetailsService需要创建对应的表,这些建表sql在spring-security-oauth2源码中,有兴趣的可以去源码中找一找。这些建表sql默认为mysql语法。
-- used in tests that use HSQL
create table oauth_client_details (
client_id VARCHAR(256) PRIMARY KEY,
resource_ids VARCHAR(256),
client_secret VARCHAR(256),
scope VARCHAR(256),
authorized_grant_types VARCHAR(256),
web_server_redirect_uri VARCHAR(256),
authorities VARCHAR(256),
access_token_validity INTEGER,
refresh_token_validity INTEGER,
additional_information VARCHAR(4096),
autoapprove VARCHAR(256)
);
INSERT INTO `oauth_client_details