使用外部不记名令牌来执行 REST API

问题描述

我有一个不记名令牌,我必须做什么才能在 Spring Boot 的 findAll 类型的 REST API 中使用它。
我试过了,但它不起作用:

@GetMapping ("/ all")
public List <MMModel> findAll (@RequestHeader (name = "Authorization") String token); 

你能帮我吗?

解决方法

假设您的令牌是 JWT 令牌,这很可能是这种情况,您可以导入 spring-boot-starter-security,其中包含所有必要的身份验证工具。

马文

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

摇篮

  implementation 'org.springframework.boot:spring-boot-starter-security'

那么您的 GetMapping 会喜欢

 @GetMapping ("/ all")
 public List <MMModel> findAll (@AuthenticationPrincipal Jwt token)