带弹簧安全oauth2的SpringBoot,oauth令牌无法存储在mysql中?

时间:2016-12-27 09:27:54

标签: java spring-security spring-security-oauth2 oauth2

我使用springboot和spring security oauth2来创建授权服务器。 我想存储客户端信息,访问令牌存储在mysql中。

Mysql架构是:

    -- used in tests that use HSQL
create table oauth_client_details (
  client_id VARCHAR(64) 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)
);

create table oauth_client_token (
  token_id VARCHAR(256),
  token BLOB ,
  authentication_id VARCHAR(64) PRIMARY KEY,
  user_name VARCHAR(256),
  client_id VARCHAR(256)
);

create table oauth_access_token (
  token_id VARCHAR(256),
  token BLOB ,
  authentication_id VARCHAR(64) PRIMARY KEY,
  user_name VARCHAR(256),
  client_id VARCHAR(256),
  authentication BLOB ,
  refresh_token VARCHAR(256)
);

create table oauth_refresh_token (
  token_id VARCHAR(256),
  token BLOB ,
  authentication BLOB 
);

create table oauth_code (
  code VARCHAR(256), authentication BLOB 
);

create table oauth_approvals (
    userId VARCHAR(256),
    clientId VARCHAR(256),
    scope VARCHAR(256),
    status VARCHAR(10),
    expiresAt TIMESTAMP,
    lastModifiedAt TIMESTAMP
);


-- customized oauth_client_details table
create table ClientDetails (
  appId VARCHAR(64) PRIMARY KEY,
  resourceIds VARCHAR(256),
  appSecret VARCHAR(256),
  scope VARCHAR(256),
  grantTypes VARCHAR(256),
  redirectUrl VARCHAR(256),
  authorities VARCHAR(256),
  access_token_validity INTEGER,
  refresh_token_validity INTEGER,
  additionalInformation VARCHAR(4096),
  autoApproveScopes VARCHAR(256)
);

我向oauth_client_details插入一行:

insert into oauth_client_details(client_id, resource_ids, client_secret, scope, authorized_grant_types,
    web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information
    , autoapprove)
values(
    '385163bb-8795-40a1-bcba-004701750f21','', '75c33a55-51f7-4c3f-ae80-604b1176d100', 'session', 'authorization_code'
    , 'http://www.google.com', 'authorities what', 60, 120, '{}', 'false'
);

AuthorizationServerConfiger是:

    @Configuration
public class AuthorizationServerConfigurerImpl extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private AuthorizationServerTokenServices tokenService;
    @Autowired
    private ClientDetailsServiceConfigurer clienService;

    @Bean
    public DefaultTokenServices tokenService(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setTokenStore(tokenStore());
        //tokenServices.setAccessTokenValiditySeconds(60);
        //tokenServices.setRefreshTokenValiditySeconds(600);
        tokenServices.setClientDetailsService(clientService());
        return tokenServices;
    }

    @Bean
    public JdbcTokenStore tokenStore(){
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public JdbcClientDetailsService clientService(){
        return new JdbcClientDetailsService(dataSource);
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // @formatter:off
//        clients.inMemory().withClient("my-trusted-client")
//                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
//                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT").scopes("read", "write", "trust")
//                .accessTokenValiditySeconds(60);
        // @formatter:on
        clients.jdbc(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenServices(tokenService);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
    }

}

application.yml:

server:
  port: 8080

# http basic authorization
security:
  basic:
    enabled: true
  user:
    name: root
    password: 123456
    role: user  


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring_oauth2
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

我可以正确授权,但生成的令牌无法存储在mysql表oauth_client令牌中。我的问题是什么?

0 个答案:

没有答案