缺少授权类型错误

2024-03-03

我只是想学习 OAuth。我写了一些代码来测试它。当我提交请求时我得到 { “错误”:“无效请求”, "error_description": "缺少授权类型" }

邮递员的错误。

import java.util.Optional;

//import static org.assertj.core.api.Assertions.tuple;

import java.util.stream.Stream;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.springframework.security.core.userdetails.User;

//import org.omg.PortableInterceptor.ACTIVE;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
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.stereotype.Service;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@SpringBootApplication
public class SpringAuthServiceApplication {

    @Bean
    CommandLineRunner clr(AccountRepository accountRepository){
        return args -> {
            Stream.of("name1, password1", "name2, password2", "name3, password3", "name4, password4")
            .map(tpl -> tpl.split(",") )
            .forEach(tpl -> accountRepository.save(new Account(tpl[0], tpl[1], true)));
        };


    }

    public static void main(String[] args) {
        SpringApplication.run(SpringAuthServiceApplication.class, args);
    }
}


@Configuration
@EnableAuthorizationServer
class AuthServiceConfiguration extends AuthorizationServerConfigurerAdapter{
    private final AuthenticationManager authenticationManager;

    public AuthServiceConfiguration(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
        .inMemory()
        .withClient("html5")
        .secret("password")
        .authorizedGrantTypes("password")
        .scopes("openid");

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(this.authenticationManager);
    }



}


@Service
class AccountUserDetailService implements UserDetailsService{

    private final AccountRepository accountRepository;

    public AccountUserDetailService(AccountRepository accountRepository) {
//      super();
        this.accountRepository = accountRepository;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // TODO Auto-generated method stub
        return accountRepository.findByUsername (username)
                .map(account -> new User(account.getUsername(),
                        account.getPassword(), account.isActive(), account.isActive(), account.isActive(), account.isActive(),
                        AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER") )
                        )
                .orElseThrow(() -> new UsernameNotFoundException("Couldn't fine user name " + username + "!") ) ;
    }

    /*@Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return accountRepository.findByUsername(username)
                .map(account -> {
                    boolean active = account.isActive();
                    return new User(
                            account.getUsername(),
                            account.getPassword(),
                            active, active, active, active,
                            AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER"));
                })
                .orElseThrow(() -> new UsernameNotFoundException(String.format("username %s not found!", username)));
    }*/

}



interface AccountRepository extends JpaRepository<Account, Long>{
    Optional<Account> findByUsername(String username); 
}



@Data 
@NoArgsConstructor
@AllArgsConstructor
@Entity
class Account{

    public Account(String username, String password, boolean active) {
        //super();
        this.username = username;
        this.password = password;
        this.active = active;
    }

    @GeneratedValue @Id
    private long id; 
    private String username, password;
    private boolean active;


}

这是我在邮递员中发送的内容:

在标题选项卡中: 内容类型:application/json 授权:基本aHRtbDU6cGFzc3dvcmQ=

在授权选项卡中: 类型是基本身份验证 用户名:html5 密码: 密码

正文选项卡,选择表单数据并发送以下内容:

用户名: 用户名 密码: 密码1 grant_type:密码 范围:openid 客户端 ID:html5 client_secret: 密码


OAuth2 正在请求正文中查找查询字符串形式的参数,也称为application/x-www-form-urlencoded.

改变你的Content-Type to application/x-www-form-urlencoded并检查x-www-form-urlencoded代替form-data.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

缺少授权类型错误 的相关文章

随机推荐

  • React - 错误:尝试运行 npm start 时找不到模块 React

    我在尝试为 React 应用程序运行 npm start 时遇到了一个错误 我的应用程序在开发过程中基本上按照我的预期运行 但是我遇到了一个错误 需要我更新我的节点版本作为潜在的修复 我将 node 更新到 v16 13 2 并且还决定将
  • JQuery 自动完成。如果找不到项目,显示“按 Enter 键插入自动完成”?

    我正在做一种标签自动完成组合 因此 基本上 当用户输入没有自动完成选项的查询 即 aaa 时 我希望自动完成下拉并显示 按 Enter 为 aaa 创建标签 我在文档中找不到任何内容 我想这需要我进行一些黑客攻击 但在这样做之前 我想看看是
  • C# 相当于 Java 的 Exception.printStackTrace()?

    C 是否有与 Java 等效的方法Exception printStackTrace 或者我必须自己写一些东西 通过 InnerExceptions 来解决 尝试这个 Console WriteLine ex ToString From h
  • css api 的 jquery if else 条件

    我有下面的 jquery 语句 this span section1 css background url images accordion closed left png no repeat scroll 0 0 transparent
  • WPF 从 DataTrigger 调用方法

    是否可以使用通配符或调用方法来确定是否应应用 DataTrigger 我目前将 DataList 绑定到包含文件名的 IEnumerable 并且如果文件扩展名以 old 开头 我希望文件名显示为灰色 我的非工作梦想 xaml 标记看起来像
  • 返回内置类型的常量值[重复]

    这个问题在这里已经有答案了 为内置类型返回 const 值是一个好的习惯吗 原因是 const int F int y F x y 如果返回值是const 上面的代码将无法编译 然而 如果不是的话F x y 是一个非常隐蔽的错误 由于为函数
  • UserType / Hibernate / JodaTime - 在哪里设置 UserType 全局属性?

    我正在使用org jadira usertype dateandtime joda PersistentDateTimeUserType 3 0 0 RC1 中的类来映射 JodaTimeDateTime进入休眠状态 Java文档 http
  • 为什么 Collections.Frequency 在转换后的列表上无法按预期工作?

    我过去使用过 Collections Frequency 它工作得很好 但现在我使用 int 时遇到了问题 基本上 Collections Frequency 需要一个数组 但我的数据采用 int 的形式 所以我转换了我的列表 但没有得到结
  • 如何将 Bootstrap CDN 添加到我的 WordPress

    我想在我的 Wordpress 中使用 Bootstrap 框架 如何在functions php 中编辑 我找到一个地方告诉这样的代码 function enqueue my scripts wp enqueue script jquer
  • 带有 Spring-boot 后端的 Flutter websocket

    好吧 Flutter 在食谱中有 WebSocket 配方 here https flutter dev docs cookbook networking web sockets 这对于 websocket org 测试服务器非常有效 问题
  • WPF - 更改隐藏代码中的样式

    我有一个显示 TFS 查询结果的列表框 我想更改后面代码中 ListBoxItem 的样式 以使查询结果中包含列 ListBox 项的样式在我的 Windows Resources 部分中定义 我已经尝试过这个 public T GetQu
  • Nginx 不区分大小写 proxy_pass

    我有一个网站叫http example com 正在运行一个可以通过以下位置访问的应用程序http example com app1 app1 位于 nginx 反向代理后面 如下所示 location app1 proxy pass ht
  • BeautifulSoup 返回意外的额外空格

    我正在尝试使用 BeautifulSoup 从 html 文档中获取一些文本 在一个对我来说非常相关的案例中 它产生了一个奇怪而有趣的结果 在某一点之后 汤在文本中充满了额外的空格 空格将每个字母与下一个字母分开 我试图在网络上搜索以找到原
  • 识别何时使用模运算符

    我知道modulus http en wikipedia org wiki Modulo operation 运算符计算除法的余数 如何确定需要使用模运算符的情况 我知道我可以使用模运算符来查看数字是偶数还是奇数 素数还是合数 但仅此而已
  • 使用 Pandas 时明显缺少 dateutil.tz 包?

    我的python 2 7代码如下 import pandas as pd from pandas import DataFrame DF rando DataFrame 1 2 3 然后当我执行时 我收到一个奇怪的错误dateutil tz
  • 如何将通用 JavaScript 对象序列化为 XML

    主流 JavaScript 库 YUI jQuery Dojo 之一是否提供了将 JavaScript 对象序列化为 XML 作为文本 的方法 有no用于本机对象到 XML 序列化的本机 API 然而 有一些 3rd 方库 比如这个 它会输
  • 从外部程序集中动态加载类型

    在托管代码中 假设调用代码没有对该程序集的静态引用 如何在运行时从另一个程序集加载托管类型 为了澄清起见 假设我将 Lib cs 中的类 Lib 编译为 Lib dll 我想在一个名为 Foo dll 的单独程序集中编写一个类 Foo 它没
  • Django:如何从时间帖子中获取时差?

    假设我有一个模型课程 class Post models Model time posted models DateTimeField auto now add True blank True def get time diff self
  • 如何声明和使用 NSString 全局常量[重复]

    这个问题在这里已经有答案了 可能的重复 Objective C 中的常量 https stackoverflow com questions 538996 constants in objective c 我将一些应用程序设置存储在 NSU
  • 缺少授权类型错误

    我只是想学习 OAuth 我写了一些代码来测试它 当我提交请求时我得到 错误 无效请求 error description 缺少授权类型 邮递员的错误 import java util Optional import static org