前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springsecurity-oauth2之ClientDetailsService

Springsecurity-oauth2之ClientDetailsService

作者头像
克虏伯
发布2019-04-15 09:45:57
5.2K0
发布2019-04-15 09:45:57
举报

    Spring-security-oauth2的版本是2.0。

    如下List-1所示

List-1

代码语言:javascript
复制
package org.springframework.security.oauth2.provider;


/**
 * A service that provides the details about an OAuth2 client.
 *
 * @author Ryan Heaton
 */
public interface ClientDetailsService {

  /**
   * Load a client by the client id. This method must not return null.
   *
   * @param clientId The client id.
   * @return The client details (never null).
   * @throws ClientRegistrationException If the client account is locked, expired, disabled, or invalid for any other reason.
   */
  ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException;

}

    ClientDetailsService的实现类有InMemoryClientDetailsService和JdbcClientDetailsService。

List-2 InMemoryClientDetailsService

代码语言:javascript
复制
package org.springframework.security.oauth2.provider.client;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;

/**
 * Basic, in-memory implementation of the client details service.
 *
 * @author Ryan Heaton
 */
public class InMemoryClientDetailsService implements ClientDetailsService {

  private Map<String, ClientDetails> clientDetailsStore = new HashMap<String, ClientDetails>();

  public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
    ClientDetails details = clientDetailsStore.get(clientId);
    if (details == null) {
      throw new NoSuchClientException("No client with requested id: " + clientId);
    }
    return details;
  }

  public void setClientDetailsStore(Map<String, ? extends ClientDetails> clientDetailsStore) {
    this.clientDetailsStore = new HashMap<String, ClientDetails>(clientDetailsStore);
  }

}

    InMemoryClientDetailsService如List-2所示,将ClientDetails存储到Hashmap中。

    JdbcClientDetailsService则是将ClientDetails存储在数据库中,如下List-3所示

List-3 JdbcClientDetailsService的loadClientByClientId

代码语言:javascript
复制
public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {
  ClientDetails details;
  try {
    details = jdbcTemplate.queryForObject(selectClientDetailsSql, new ClientDetailsRowMapper(), clientId);
  }
  catch (EmptyResultDataAccessException e) {
    throw new NoSuchClientException("No client with requested id: " + clientId);
  }

  return details;
}

    如List-3,用jdbcTemplate从数据库中查询,来看下selectClientDetailsSql,如下List-4

List-4

代码语言:javascript
复制
select client_id, client_secret, resource_ids, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove from oauth_client_details where client_id = ?

    来看下ClientDetailsRowMapper,如下List-5,最终返回的是BaseClientDetails。

List-5

代码语言:javascript
复制
private static class ClientDetailsRowMapper implements RowMapper<ClientDetails> {
  private JsonMapper mapper = createJsonMapper();

  public ClientDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
    BaseClientDetails details = new BaseClientDetails(rs.getString(1), rs.getString(3), rs.getString(4),
        rs.getString(5), rs.getString(7), rs.getString(6));
    details.setClientSecret(rs.getString(2));
    if (rs.getObject(8) != null) {
      details.setAccessTokenValiditySeconds(rs.getInt(8));
    }
    if (rs.getObject(9) != null) {
      details.setRefreshTokenValiditySeconds(rs.getInt(9));
    }
    String json = rs.getString(10);
    if (json != null) {
      try {
        @SuppressWarnings("unchecked")
        Map<String, Object> additionalInformation = mapper.read(json, Map.class);
        details.setAdditionalInformation(additionalInformation);
      }
      catch (Exception e) {
        logger.warn("Could not decode JSON for additional information: " + details, e);
      }
    }
    String scopes = rs.getString(11);
    if (scopes != null) {
      details.setAutoApproveScopes(StringUtils.commaDelimitedListToSet(scopes));
    }
    return details;
  }
}

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档