【问题标题】:Use an external Bearer Token in order to execute a REST API使用外部不记名令牌以执行 REST API
【发布时间】:2021-05-15 08:13:33
【问题描述】:

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

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

你能帮帮我吗?

【问题讨论】:

    标签: spring-boot bearer-token findall


    【解决方案1】:

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

    Maven

    <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)
    

    【讨论】:

    • 好的,但是我有一个像这样的令牌:(eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJpTXN5NjVaTGpRTFQ1OS1Zb ....) 我如何在你的修改中大摇大摆地使用它???因为在招摇我看到不同的部分:issuerUri、jwkSetUri、jwsAlgorithm、publicKeyLocation.URI.absolute ecc...
    • 我不知道 Swagger。您拥有的是编码令牌。只需在 Authorization 标头中传递编码的令牌,@AuthenticationPrincipal 就会为您解码。您将获得一个包含所有令牌信息的 Jwt 对象。
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多