java - 为什么我在访问 token 时在 Spring Oauth2 中出现未经授权的 401 错误?

标签 java spring curl oauth

您好,我正在我的项目中实现 Spring Oauth 2 框架,在请求访问 token 时出现 401 未授权错误,下面是我的代码。

public class Test {


    public static void main(String[] args) {

        RestTemplate restTemplate=new RestTemplate();
        Map<String, String> map=new HashMap<String, String>();
        map.put("grant_type", "password");
        map.put("client_id", "test");
        map.put("client_secret", "test");
        map.put("username", "test");
        map.put("password", "test");
        String url="http://localhost:8080/SpringOauthServer/oauth/token?grant_type={grant_type}&client_id={client_id}&client_secret={client_secret}&username={username}&password={password}";

        OauthToken result=restTemplate.getForObject(url, OauthToken.class,map);     
        System.out.println(result.getAccess_token());
    }


}

但是当我使用下面的 curl 命令时,我得到了访问 token 。请帮助我在哪里我弄错了..

curl test:test@localhost:8080/SpringOauthServer/oauth/token -d grant_type=password -d client_id=test -d client_secret=test -d username=test   -d password=test

响应:

{
   "access_token":"d83a312b-323a-40a9-bfc4-c431c40f2ca8",
   "token_type":"bearer",
   "refresh_token":"17976f94-f3b7-4e2d-8726-3d094f7b1061",
   "expires_in":43190,
   "scope":"read write trust"
}

最佳答案

我知道这是一个旧线程,但如果有人坚持 RestTemplateOAuth2 (例如,对于集成测试)它应该如何工作。

正在获取 OAuth2 access_token使用 RestTemplate

在上面的问题中 grant_type=password这意味着您需要发送 client_idsecretauthorization http header 作为基本身份验证,您请求的其余信息作为表单数据进入 http 请求正文。

重用问题中的例子:

public class Test {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();

        // Add the basic authentication "username:password"
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // Adding form data
        Map<String, String> map = new HashMap<String, String>();
        map.put("grant_type", "password");
        map.put("client_id", "test");
        map.put("username", "test");
        map.put("password", "test");
        map.put("scope", "read,write,trust");

        // Creating HttpEntity request with the headers and form data
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

        String url="http://localhost:8080/SpringOauthServer/oauth/token";

        // Execute the request
        ResponseEntity<String> response = 
            restTemplate
                .withBasicAuth("test", "test")
                .postForEntity(
                    url,
                    request,
                    OauthToken.class
                );

        System.out.println(result.getAccess_token());
    }
}

问题出了什么问题:

curl在问题中使用的功能与提供的 java 代码完全不同。

我会把它拆掉,别忘了检查documentation here

  1. curl test:test打电话curl使用 test 的基本身份验证作为用户名和 test作为密码(格式 {USERNAME}:{PASSWORD} ),我们在 RestTemplate 中也这样做了问题 java 代码中缺少它。

  2. localhost:8080/SpringOauthServer/oauth/token curl 中使用的网址命令没有任何 url 参数,我们在 RestTemplate 中也这样做了但它被添加到问题中,这是错误的。

  3. -d grant_type=password -d client_id=test -d client_secret=test -d username=test -d password=test-d 标记参数将使curl执行 http POST请求并发送这些参数作为我们在 RestTemplate 中所做的相同的 http 正文中的表单数据。问题 java 代码中缺少它。

关于java - 为什么我在访问 token 时在 Spring Oauth2 中出现未经授权的 401 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37241302/

相关文章:

java - 数字签名 SunMSCAPI 提供商和 MS Crypto API

spring - Spring集成异常: How to log but not intercept

android - 在特定时间点在gradle中运行curl命令

java - Spring Boot + Liquibase - 自定义 LockService 类

Apache 反向代理更改状态代码

javascript - Google Drive API 使用 curl 调用创建文件而不是文件夹

java - colver中的 'true branch executed'和 'branch executed'有什么不同?

java - 在不删除任何内容的情况下获取并发修改异常

java - RuntimeException - 由 : android. database.sqlite.SQLiteException 引起:没有这样的表:tbl(代码 1)

Spring Boot - 无法使用 aspectj 进行加载时间编织工作