【问题标题】:Unauthorized error oauth2 client from database来自数据库的未经授权的错误 oauth2 客户端
【发布时间】:2019-07-13 23:32:02
【问题描述】:

我的应用中出现错误“未经授权”的问题。我正在使用 Spring Security 和 oauth2。我的客户和用户存储在数据库中。当我开始使用数据库中的客户端时,PostMan 中出现错误 401。客户端正在保存在数据库中,但是当我想从 localhost:8080/oauth/token 获取令牌访问权限时仍然出现错误。以下是我的来源:

授权服务器配置:

公共类 AuthorizationServerConfig 扩展 AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;



@Autowired
private TokenStore tokenStore;


private CustomClientDetailsService customClientDetailsService;



@Bean
PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.withClientDetails(customClientDetailsService);

}


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

}

这是我的 CustomClientDetails :

公共类 CustomClientDetails 实现 ClientDetails {

final static Logger log = LoggerFactory.getLogger(CustomClientDetailsService.class);

private static final long serialVersionUID = 6602529451366778198L;

private Clients clients;

public CustomClientDetails(final Clients clients){
    this.clients = clients;
}

@Override
public String getClientId() {
    return clients.getClientId();
}

@Override
public Set<String> getResourceIds() {
    final Set<String> resourcesIds = new HashSet<String>();
    resourcesIds.add(clients.getResourceIds());
    return resourcesIds;
}

@Override
public boolean isSecretRequired() {
    return true;
}

@Override
public String getClientSecret() {
    return clients.getClientSecret();
}

@Override
public boolean isScoped() {
    return true;
}

@Override
public Set<String> getScope() {
    final Set<String> scopes = new HashSet<String>();
    scopes.add(clients.getScope());
    return scopes;
}

@Override
public Set<String> getAuthorizedGrantTypes() {
    final Set<String> authorizedGrantTypes = new HashSet<String>();
    authorizedGrantTypes.add(clients.getAuthorizedGrantTypes());
    return authorizedGrantTypes;

}

@Override
public Set<String> getRegisteredRedirectUri() {
    final Set<String> registeredRedirectUris = new HashSet<String>();
    registeredRedirectUris.add(clients.getWebServerRedirectUri());
    return registeredRedirectUris;
}

@Override
public Collection<GrantedAuthority> getAuthorities() {
    final Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority(clients.getAuthorities()));
    return authorities;
}

@Override
public Integer getAccessTokenValiditySeconds() {
    return clients.getAccessTokenValidity();
}

@Override
public Integer getRefreshTokenValiditySeconds() {
    return clients.getRefreshTokenValidity();
}

@Override
public boolean isAutoApprove(String s) {
    return false;
}

@Override
public Map<String, Object> getAdditionalInformation() {
    final Set<String> additionalInformation = new HashSet<String>();
    additionalInformation.add(clients.getAdditionalInformation());
    return null;
}

这是一个 CustomClientDetailsS​​ervice :

公共类 CustomClientDetailsS​​ervice 实现 ClientDetailsS​​ervice {

@Autowired
private ClientsRepository clientsRepository;

@Autowired
private CustomClientDetails customClientDetails;

@Override
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

    Clients client = clientsRepository.findByClientId(clientId);

        final CustomClientDetails customClientDetails = new CustomClientDetails(client);
        return customClientDetails;
    }

来自 PostMan 的错误:

{ "时间戳": "2019-02-20T09:32:15.479+0000", “状态”:401, “错误”:“未经授权”, “消息”:“未经授权”, “路径”:“/oauth/令牌” }

【问题讨论】:

    标签: java oauth-2.0


    【解决方案1】:

    您应该在邮递员中提供client_idclient_secret,在授权部分,您可以设置基本身份验证。

    username 字段中输入您的client_id,在password 中输入您的client_secret

    【讨论】:

    • 好的,您配置了client_id 和client_secret,因为我在您的代码中没有看到它:)
    • cilent_id 和 client_secret 存储在数据库中
    • client_secret 可能必须采用散列形式。如果您将日志记录级别设置为调试甚至跟踪,日志中有什么有用的吗?
    【解决方案2】:

    “/oauth/token”处的“未授权”可能意味着您没有在请求标头中提供HTTP Basic Auth 凭据。据我回忆,该端点默认使用存储在oauth_client_details 实体中的登录名和密码进行保护。查找 client_id + client_secret 对,并通过 Authorization->Basic Auth 设置将其提供给 Postman。

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2013-08-03
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多