【发布时间】:2023-04-02 11:53:02
【问题描述】:
我目前正在开发一个简单的 Spring Boot 应用程序,我在其中传递 client_id 和 secret 来获取访问令牌,从而获得刷新和访问令牌。
但是当我尝试使用该令牌访问资源(我的 REST API)时(使用此 URL:curl -H "Authorization: Bearer ead8ba5d-88ad-4531-a821-db08bf25e888" localhost:8081/my-端点 ),它对我不起作用并给我以下错误-
{"error":"invalid_token","error_description":"无效的访问令牌:ead8ba5d-4531-db08bf2fe888"}
这就是我的端点的样子 -
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
@RequestMapping(path = "/my-end-point")
public class PrincipalResource {
@RequestMapping(method = RequestMethod.POST)
public Principal oauth(Principal principal) {
/*
* Translate the incoming request, which has an access token
* Spring security takes the incoming request and injects the Java Security Principal
* The converter inside Spring Security will handle the to json method which the Spring Security
* Oauth client will know how to read
*
* The @EnableResourceServer on the application entry point is what makes all this magic happen.
* If there is an incoming request token it will check the token validity and handle it accordingly
*
*
*/
return principal;
}
} `
【问题讨论】:
-
由于身份验证问题,可能对您有帮助stackoverflow.com/questions/29596036/…
-
我正在关注这篇关于oauth2实现的文章-dazito.com/java/spring-boot-and-oauth2-with-jdbc
标签: spring spring-boot spring-security oauth-2.0 spring-security-oauth2