Spring OAuth2 - 没有客户端身份验证.尝试添加适当的身份验证筛选器

Kar*_*hik 13 spring-security access-token spring-security-oauth2

我们有一个正在使用的应用程序spring-security-oauth2:1.0.我试图将其更改为更新版本spring-security-oauth2:2.0.7.RELEASE.一些类被删除,一些包结构被更改,我设法整理所有这些东西,我能够没有任何问题启动服务器.但我在这里遇到一个奇怪的问题.

随着OAuth2 - 1.0 version,当我们在用户登录使用做了GET要求/oauth/token,例如:

HTTP://本地主机:8080 /回声/的OAuth /令牌grant_type =密码&CLIENT_ID = WS&client_secret =秘密及范围=读,写和用户名=约翰@ abc.com和密码= password123

它以前工作得很好.

当我尝试同样的事情时,首先GET由于TokenEndPoint.java中的逻辑,我无法提出请求

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);
}
Run Code Online (Sandbox Code Playgroud)

我试图POST提出与上述URL相同的请求,但我收到InsufficientAuthenticationException了错误消息

没有客户端身份验证.尝试添加适当的身份验证筛选器

这是因为下面的POST请求控制器TokenEndpoint.java.当我调试时,我看到它principal是null.

@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.");
        }
     .............
 }
Run Code Online (Sandbox Code Playgroud)

我有一个身份验证过滤器,它在我使用时运行良好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>
Run Code Online (Sandbox Code Playgroud)

我一直认为请求将通过身份验证authentication-provider进行,token-endpoint但这似乎不是正确的流程.在调试应用程序后version 2.0.7,现在我真的怀疑我对流程的理解.

有人可以解释为什么它在以前的版本中有效,为什么它现在不起作用?

我是否有不同的东西来获得OAuth令牌?

注意:我已经检查了这些问题:这里,这里,这里.但我无法找到正确的解决方案.

Mic*_*urt 5

我不知道以前的版本,但我对2.0.7有所了解.

我怀疑您的问题是您的TokenEndpoint安全性尝试根据您的用户服务验证您的客户端.

TokenEndpoint受a保护BasicAuthenticationFilter.默认情况下,此过滤器将使用一个AuthenticationManager实例,该实例本身包含一个实例,该实例AuthenticationProvider本身依赖于实例UserDetailsService.诀窍是,这种特殊的情况下UserDetailsService一定要客户基础,没有用户基础:这就是为什么有一个ClientDetailsUserDetailsService,它适应ClientDetailsServiceUserDetailsService.

通常,所有这些东西已经是自动进行的,当你使用框架的配置类AuthorizationServerConfigurerAdapter,@EnableAuthorizationServer等等.

  • 那么当我使用xml配置时,如何完成所有这些工作呢? (2认同)

归档时间:

查看次数:

15427 次

最近记录:

6 年,10 月 前