java oauth2 springsecurity在请求令牌或代码之前是否应该登录
我正在尝试在SpringMVC应用程序中使用SpringSecurity(2.0.3.RELEASE)设置oauth2,并使用授权类型的密码
执行以下请求以访问令牌时:
curl -X POST "http://localhost:8080/oauth/authorize?client_id=testclient&grant_type=password&username=user&password=test123&response_type=token"
我得到一个:
org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed.
在跟踪此异常时,我注意到它起源于AuthorizationEndpoint。java第137行代码检查用户是否经过身份验证:
if (!(principal instanceof Authentication) || !((Authentication) principal).isAuthenticated()) {
我觉得这很奇怪,在尝试访问令牌之前,我应该首先登录并发布到/oauth/authorize资源吗? 如果是,为什么?我已经将密码和用户名添加到/oauth/authorize请求中。如果您必须再次提供密码和用户名,我看不到在尝试获取令牌之前登录的好处
# 1 楼答案
您使用了错误的端点。如果您正在使用
password
授权,那么应该将请求发送到令牌端点。有关详细信息,请参见OAuth2 spec