为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕莱坞森 2021-09-26 17:10:29
我的 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;
查看完整描述

2 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

原来我没有正确到达终点。我正在通过 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/token

Authorization: Basic Y2xpZW50X2lkOnNlY3JldA==

...

scope=read&grant_type=password&username=user&password=password


查看完整回答
反对 回复 2021-09-26
?
大话西游666

TA贡献1817条经验 获得超14个赞

尝试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;

        }

    };

}

希望它会起作用。


查看完整回答
反对 回复 2021-09-26
  • 2 回答
  • 0 关注
  • 287 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号