【发布时间】:2015-10-08 20:54:03
【问题描述】:
我们有一个使用spring-security-oauth2:1.0 的应用程序。我试图将其更改为较新的版本,spring-security-oauth2:2.0.7.RELEASE。删除了一些类,更改了一些包结构,我设法解决了所有这些问题,并且能够毫无问题地启动服务器。但我在这里遇到了一个奇怪的问题。
使用OAuth2 - 1.0 version,当用户登录时,我们曾经对/oauth/token进行GET请求,例如:
它曾经工作得很好。
当我尝试同样的事情时,首先由于TokenEndPoint.java 中的逻辑,我无法发出GET 请求
private Set<HttpMethod> allowedRequestMethods = new HashSet<HttpMethod>(Arrays.asList(HttpMethod.POST));
@RequestMapping(value = "/oauth/token", method=RequestMethod.GET)
public ResponseEntity<OAuth2AccessToken> getAccessToken(Principal principal, @RequestParam
Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
if (!allowedRequestMethods.contains(HttpMethod.GET)) {
throw new HttpRequestMethodNotSupportedException("GET");
}
return postAccessToken(principal, parameters);
}
我尝试发出与上述 URL 相同的 POST 请求,但我收到带有错误消息的 InsufficientAuthenticationException
没有客户端身份验证。尝试添加适当的身份验证过滤器
这是因为TokenEndpoint.java 中的以下POST 请求控制器。当我调试时,我看到principal 为空。
@RequestMapping(value = "/oauth/token", method=RequestMethod.POST)
public ResponseEntity<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam
Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
//principal is null here
if (!(principal instanceof Authentication)) {
throw new InsufficientAuthenticationException(
"There is no client authentication. Try adding an appropriate authentication filter.");
}
.............
}
我有一个身份验证过滤器,当我使用version 1.0 时它运行良好。这是我配置的相关部分:
<authentication-manager xmlns="http://www.springframework.org/schema/security">
<authentication-provider user-service-ref="userDetailsService"/>
</authentication-manager>
<bean id="userDetailsService" class="com.hcl.nc.service.UserDetailsService">
<constructor-arg><ref bean="sessionFactory" /></constructor-arg>
</bean>
我一直认为请求将由authentication-provider 进行身份验证并转到token-endpoint 但这似乎不是正确的流程。用version 2.0.7调试应用程序后,现在我真的怀疑我对流程的理解了。
有人能解释一下为什么它在以前的版本中可以工作,为什么现在不能工作吗?
我是否需要做一些不同的事情才能获得 OAuth 令牌?
【问题讨论】:
-
你找到答案了吗?我遇到了同样的问题,在这里找不到答案。
标签: spring-security access-token spring-security-oauth2