【问题标题】:Why i am getting unauthorized 401 error in Spring Oauth2 while accessing token?为什么我在访问令牌时在 Spring Oauth2 中收到未经授权的 401 错误?
【发布时间】:2016-09-11 11:57:28
【问题描述】:

您好,我正在我的项目中实现 Spring Oauth 2 框架,我在请求访问令牌时收到 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 命令时,我得到了访问令牌。请帮助我在哪里弄错了..

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"
}

【问题讨论】:

    标签: java spring curl oauth


    【解决方案1】:

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

    使用 RestTemplate 获取 OAuth2 access_token

    在上述问题中,grant_type=password 表示您需要在authorization http 标头中发送client_idsecret 作为基本身份验证,您的请求的其余信息将发送到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/tokencurl 命令中使用的 url 没有任何 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 请求并将这些参数作为http 正文中的表单数据发送,我们在RestTemplate 中做了同样的事情,它是问题 java 代码中缺少。

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 2017-06-20
      • 1970-01-01
      • 2010-10-03
      • 2019-08-09
      • 2020-11-08
      • 2022-07-18
      • 2023-03-03
      • 2020-03-14
      相关资源
      最近更新 更多