【问题标题】:How to validate tokens received from authorization server如何验证从授权服务器收到的令牌
【发布时间】:2021-07-28 20:40:22
【问题描述】:

我的项目中有一个 oauth 流程。

我在前端检索一个 jwt 令牌并将其添加到授权标头中的每个请求中。

现在我需要验证所述令牌并在我的后端验证签名,这是一个 kotlin spring boot 应用程序。

我知道如何使用 jjwt 库验证令牌,但我不明白验证在哪里完成。

我有一个证书来验证令牌,只是想让带有有效令牌的请求得到处理。

我在网上看到有些人使用他们添加到 SecurityConfiguration 中的 OncePerRequestFilter 来执行此操作,但我不明白发生了什么以及它是如何工作的。

我尝试在线搜索教程,但其中许多都创建了一个既是授权服务器又是资源服务器的后端。我只想让后端成为资源服务器,如果令牌有效,则检查证书并处理请求。我该怎么做?

现在这是我的 SecurityConfiguration :

package com.renaulttrucks.transfertprotocolbackend.security.config

import org.springframework.beans.factory.annotation.Value
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter

@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    @Value("\${security.enabled}")
    val securityEnabled : Boolean? = false

    @Throws(Exception::class)
    override fun configure(http: HttpSecurity) {

        if(!securityEnabled!!) {
            http.httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .and()
                .csrf().disable()
                .formLogin().disable()
        } else {
            http.httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .and()
                .csrf().disable()
                .formLogin().disable()
        }
    }

}

【问题讨论】:

标签: spring spring-boot spring-security


【解决方案1】:

当包含正确的依赖项和配置时,Spring Security 支持开箱即用的资源服务器。

正如@sdoxsee 提到的,有一个Spring Security 示例概述了如何create a resource server with a public key;不过,我将在这里简要总结一下,尽管您可以在Spring Security reference 中找到更多详细信息。

首先,您需要添加适当的依赖项。如果你是 Spring Boot 应用,那么你可以添加:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>

其次,您可以将密钥指定为引导属性:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          public-key-location: classpath:my-key.pub

或者,您直接使用您的公钥配置JwtDecoder

@Configuration
class SecurityConfig {
    @Value("${public.key.property}") val key : RSAPublicKey;

    @Bean
    fun jwtDecoder() : JwtDecoder {
        return NimbusJwtDecoder.withPublicKey(this.key).build();
    }
}

Boot 属性或JwtDecoder @Bean 都会将过滤器自动引入名为BearerTokenAuthenticationFilter 的过滤器链中,因此您无需创建自己的过滤器。

【讨论】:

  • 我只是像你和@sdoxsee 提到的那样尝试过,但它不起作用。我没有验证令牌的公钥,而是证书。使用证书的密钥路径出现以下错误: 原因:org.springframework.beans.ConversionNotSupportedException:无法将类型“java.lang.String”的值转换为所需类型“java.security.interfaces.RSAPublicKey”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“java.security.interfaces.RSAPublicKey”:找不到匹配的编辑器或转换策略
  • 我认为您可以将证书转换为公钥。 OpenSSL stackoverflow.com/questions/17143606/… 和 Java stackoverflow.com/questions/6358555/… 都具有此功能。
猜你喜欢
  • 2022-08-21
  • 1970-01-01
  • 2018-05-06
  • 2013-04-22
  • 2020-11-25
  • 2019-08-13
  • 2022-10-07
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多