猿问

Java Spring Security:401 令牌 OAuth2 端点未经授权

我的 Spring Boot 项目中有一个相当基本的设置。我正在尝试设置 OAuth2 来保护我的 API,但我的/oauth/token端点遇到了问题。向我的/oauth/token端点发出 POST 或 GET 请求会导致以下响应(带有401 Unauthorized状态代码):


{

    "timestamp": "2018-09-17T16:46:59.961+0000",

    "status": 401,

    "error": "Unauthorized",

    "message": "Unauthorized",

    "path": "/oauth/token"

}

这是我的授权服务器配置。


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.context.annotation.Configuration;

import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.crypto.password.PasswordEncoder;

import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;

import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;

import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;

import org.springframework.security.oauth2.provider.token.TokenStore;


慕莱坞森
浏览 288回答 2
2回答

至尊宝的传说

原来我没有正确到达终点。我正在通过 HTTP POST 发送我的所有数据,包括客户端凭据。POST http://localhost:8080/oauth/token...client_id=client_id&secret=secret&scope=read&grant_type=password&username=user&password=password我需要使用 HTTP 基本身份验证来发送我的客户端凭据而不是发布它们:POST http://localhost:8080/oauth/tokenAuthorization: Basic Y2xpZW50X2lkOnNlY3JldA==...scope=read&grant_type=password&username=user&password=password

大话西游666

尝试AuthorizationServerConfig使用这个简单的编码器(它不加密密码)从您的课程中更改您的密码编码器。因为您没有将您的客户端密码保存在 InMemory 存储中并进行加密。private PasswordEncoder getPasswordEncoder() {    return new PasswordEncoder() {        public String encode (CharSequence charSequence) {            return charSequence.toString();        }        public boolean matches(CharSequence charSequence, String s) {            return true;        }    };}希望它会起作用。
随时随地看视频慕课网APP

相关分类

Java
我要回答