【问题标题】:Java Spring Boot - Unauthorized, PostmanJava Spring Boot - 未经授权,邮递员
【发布时间】:2019-11-09 13:35:45
【问题描述】:

按照该教程后,我运行了我的 Spring 应用程序:https://www.youtube.com/watch?v=IOgCMtYMr2Q&list=PLcoE64orFoVsxAam_BuQBrNC8IO238SwH&index=2 一切正常,但是...当我尝试执行 POST 请求时

localhost:8080/oauth/token?grant_type=password&username=user&password=passwordPostman 给了我这个问题:

{
    "timestamp": "2019-06-27T13:46:33.144+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/oauth/token"
}

其中一个 cmets 可能是一个答案 “Rocketman 没有说(或者我错过了)你需要指定基本身份验证、客户端 ID 和机密。一旦完成,我就能够获得访问令牌。” 但我不知道该怎么做。

【问题讨论】:

  • 其实,你需要在 blogApplication.java 中设置用户名和密码,然后尝试点击 OAuth URL,它会给出访问令牌。您需要将标头键作为授权传递,将值作为承载访问令牌传递。它会工作
  • WWW-Authenticate →Bearer realm="oauth2-resource", error="unauthorized", error_description="访问此资源需要完全认证"

标签: java spring maven oauth-2.0 postman


【解决方案1】:

您需要指定基本身份验证、客户端 ID 和密码。在这种情况下,客户端 ID 是 my-trusted-client,密码是 secret。下面是获取toke的完整代码:

    login(){
            var params = new URLSearchParams();
            params.append('grant_type', 'password');
            params.append('username', this.username);
            params.append('password',this.password);
            axios({
                method:'post',
                url:'oauth/token',
                auth:{username:'my-trusted-client',password:'secret'},
                headers: {"Content-type": "application/x-www-form-urlencoded; charset=utf-8"},
                data:params
            }).then(function(response){
                set_cookie("access_token",response.data.access_token);
                document.location.replace("/");
            });
        }

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2017-07-27
    • 2018-07-01
    • 1970-01-01
    • 2018-07-04
    • 2017-02-23
    • 2021-03-19
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多