来自数据库的未经授权的错误oauth2客户端

问题描述 投票:0回答:2

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

AuthorizationServerConfig:

public class 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的错误:

{“timestamp”:“2019-02-20T09:32:15.479 + 0000”,“status”:401,“error”:“Unauthorized”,“message”:“Unauthorized”,“path”:“/ oauth / token “}

java oauth-2.0
2个回答
1
投票

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

username领域,把你的client_idpassword,把你的client_secret


0
投票

“/ oauth / token”中的“未授权”可能意味着您未在请求标头中提供HTTP Basic Auth凭据。据我所知,这个端点默认是安全的,登录名和密码存储在oauth_client_details实体中。寻找client_id + client_secret对并将其提供给Postman with Authorization-> Basic Auth settings。

© www.soinside.com 2019 - 2024. All rights reserved.