【问题标题】:Basic Auth and Grant Type for OAuth 2 in Spring BootSpring Boot 中 OAuth 2 的基本身份验证和授权类型
【发布时间】:2022-08-09 15:30:08
【问题描述】:

我有一个使用 OAuth2 和 JWT 进行身份验证的 Spring Boot 应用程序。在我的application-development.yml 中,我有以下配置:

security:
  jwt:
    key-store: classpath:keystore.jks
    key-store-password: mySecretPassword1
    key-pair-alias: clientId
    key-pair-password: mySecretPassword1

在我的AuthorizationServerConfiguration.java 中,我有:

@Override
public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
    authorizationServerEndpointsConfigurer
            .authenticationManager(authenticationManager)
            .accessTokenConverter(jwtAccessTokenConverter())
            .reuseRefreshTokens(false)
            .userDetailsService(this.userDetailsService)
            .exceptionTranslator(webResponseExceptionTranslator)
            .tokenStore(tokenStore())
            .pathMapping(\"/oauth/token\", \"/login\");
}

使用它,我可以使用 Postman 登录系统。但是,每次我需要登录时,在 Postman 中,我都需要为基本身份验证和授权类型配置客户端 ID 和客户端密码,如下面的屏幕截图所示:

Basic Auth

Grant Type

我需要能够在每次不提供 clientId、clientSecret 和 grant_type 的情况下登录。

我已经看到在使用云配置的微服务的情况下做了类似的事情,如下所示:

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

      routes:
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/auth/**
          filters:
            - RewritePath=/auth/(?<path>.*), /$\\{path}
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/login/**
          filters:
            - RewritePath=/login(?<path>.*), /oauth/token$\\{path}
            - AddRequestHeader=Authorization,Basic BaS!CAuthT0ken
            - AddRequestParameter=grant_type,password

在这里,过滤器被添加到网关中,它工作得非常好。在不使用网关的情况下,我需要一种在 Monolith 的情况下复制相同内容的方法。我需要一种方法来替换以下代码:

- AddRequestHeader=Authorization,Basic BaS!CAuthT0ken
- AddRequestParameter=grant_type,password

我尝试在我的 AuthorizationServerConfiguration 中使用以下内容:

@Override
public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
    clientDetailsServiceConfigurer.jdbc(this.dataSource)
            .withClient(\"clientId\")
            .authorizedGrantTypes(\"password\")
            .secret(encoder().encode(\"mySecretPassword1\"))
            .scopes(\"read\",\"write\");

但是我没有发现成功,因为它会导致以下错误:

{
  \"error\": \"unauthorized\",
  \"error_description\": \"Full authentication is required to access this resource\"
}

任何帮助,将不胜感激。谢谢。

  • Ehrm,所以您想要安全,但您不想要安全,因为您不想提供凭据?
  • 我不希望客户端需要从客户端输入客户端 ID 和密码。我想在后端本身处理这个。对此有什么想法吗?

标签: spring-boot oauth-2.0 jwt basic-authentication


【解决方案1】:

恐怕我不确定我是否了解您的需求,您可能会想到两个主要想法:

  • 仅在后端处理的凭据并不是真正的凭据。
  • Postman 具有一个功能(选项卡),可以为集合中的任何文件夹存储与安全相关的客户端信息,并且可以将其继承到所有子文件夹及以下请求。所以可能不需要在后端站点上设置任何“便利”。

【讨论】:

    猜你喜欢
    • 2014-12-27
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2019-04-25
    • 2018-03-12
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多