我们需要在OAuth2AuthorizationService.save方法中保存OAuth2AuthorizationRequest吗?

问题描述 投票:0回答:1

我正在使用 Spring Authorization Server 创建授权服务器。

为了保存和检索授权,我实现了一个自定义授权服务,该服务实现了

OAuth2AuthorizationService
接口。当调用
save
方法时,我保存客户端、授权类型、授权范围、主体和
authorization
对象中可用的令牌。我没有保存授权对象的任何属性,因为我没有定义任何自定义属性。

当我将请求发送到

/authorize
端点时,我收到授权代码并按预期将其保存到数据库中。但是,当我调用
/token
端点来获取访问令牌时,会引发异常。

java.lang.NullPointerException: Cannot invoke "org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.getAdditionalParameters()" because

"authorizationRequest" is null at org.springframework.security.oauth2.server.authorization.authentication.CodeVerifierAuthenticator.authenticate(CodeVerifierAuthenticator.java:97) at org.springframework.security.oauth2.server.authorization.authentication.CodeVerifierAuthenticator.authenticatelfAvailable(CodeVerifierAuthenticator.java:73) at org.springframework.security.oauth2.server.authorization.authentication.ClientSecretAuthentication Provider.authenticate(ClientSecretAuthenticationProvider.java:151) at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182) at org.springframework.security.authentication. ObservationAuthenticationManager.lambda$authenticate$1(ObservationAuthenticationManager.java:54)

我将

null
OAuth2AuthorizationRequest 对象追踪到
CodeVerifierAuthenticator.authenticate
方法:

OAuth2AuthorizationRequest authorizationRequest = (OAuth2AuthorizationRequest)authorization.getAttribute(OAuth2AuthorizationRequest.class.getName()); 

由于我没有将授权请求保存为授权对象中的属性,因此我无法将其取回。另外,我认为授权请求本身不需要存储。

因此,我想确认一下它是否确实需要保存,或者也许我做错了什么。

我尝试创建一个新的授权请求对象,并在从数据库获取它后将其分配给授权。但是,授权请求构建器仅适用于授权授予类型和所需的请求 URI,我不想对其进行硬编码。

java openid-connect spring-authorization-server
1个回答
0
投票

首先说一下你正在使用的流量/token。在这里Spring oauth2服务器,我们可以看到Spring支持这些grant-type

The supported authorization grant types are authorization_code, refresh_token, client_credentials, urn:ietf:params:oauth:grant-type:device_code, and urn:ietf:params:oauth:grant-type:token-exchange.

我相信您已经使用了authorization_code(在获取令牌之前首先需要授权),此grant_type用于正常流程(发送oauth2/authorize请求,然后发送/oauth2/token)。如果您只需要令牌而不需要 oauth2/授权请求,请使用 client_credentials grant_type,这将需要 client_id、base64 中的 client-secret。 你的问题是我们需要获得授权吗?是的,也不是,Spring 以某种方式支持许多 grant_type、可定制的方法,并且这些方法将默认寻求授权。改变这个逻辑将会导致你改变Spring的所有库。但另一方面,如果你不想太关心这个,就离开它,Spring 会自动将它存入内存中。

© www.soinside.com 2019 - 2024. All rights reserved.