【问题标题】:Browser REST Client ask for credential and it fails for RESTEasy Service浏览器 REST 客户端要求提供凭据,但 RESTEasy 服务失败
【发布时间】:2014-03-03 11:24:25
【问题描述】:

我使用 Spring security Oauth2 在 java 中创建了一个 RESTEasy 服务,用于身份验证和令牌生成。一切对我来说都很好,但是当我尝试访问我的服务以从浏览器 REST 客户端生成令牌时,它会要求提供凭据,但它会失败,但同时如果我通过 Java 中的 HTTPClient 访问相同的服务,它对我有用,

HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:80/my-rest-application/oauth/token");

          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
          nameValuePairs.add(new BasicNameValuePair("client_id","myclientid"));
          nameValuePairs.add(new BasicNameValuePair("client_secret","myclientsecret"));
          nameValuePairs.add(new BasicNameValuePair("username","someuser"));
          nameValuePairs.add(new BasicNameValuePair("password","somepassword"));
          nameValuePairs.add(new BasicNameValuePair("grant_type","password"));

          post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

          HttpResponse response = client.execute(post);

Java 中的 Http 客户端为我工作,但在浏览器休息客户端中它要求凭据并返回错误请求

知道为什么会这样吗?

我的配置,

<http pattern="/oauth/token" create-session="stateless"
    authentication-manager-ref="clientAuthenticationManager"
    xmlns="http://www.springframework.org/schema/security" > 
    <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
    <anonymous enabled="false" />
    <http-basic entry-point-ref="clientAuthenticationEntryPoint" />
    <!-- include this only if you need to authenticate clients via request parameters -->
    <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" /> 
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>


<authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
    <authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager>

<bean id="clientDetailsUserService"
    class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
    <constructor-arg ref="clientDetails" />
</bean>

<bean id="clientDetails" class="my own client details implementation"/>



<bean id="clientAuthenticationEntryPoint"
    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <!-- <property name="realmName" value="springsec/client" /> -->
    <property name="realmName" value="test/client" />
    <property name="typeName" value="Basic" />
</bean>

<bean id="clientCredentialsTokenEndpointFilter"
    class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <property name="authenticationManager" ref="clientAuthenticationManager" />
</bean>

<authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
    <authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager>

【问题讨论】:

  • 日志?您响应的内容长度为 1061 字节。它包含什么?使用您的休息客户端时,您的 request 的内容类型是什么?
  • 我的内容类型是 application/json
  • 听起来不对,应该是application/x-www-form-urlencoded
  • 非常感谢。它现在适用于内容类型“application/x-www-form-urlencoded”。
  • 我将创建评论作为回复,以便您将问题标记为已解决(这对您的 Karma 有好处)

标签: java resteasy spring-security-oauth2


【解决方案1】:

将 REST 客户端上的 content-type 更改为 application/x-www-form-urlencoded

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2022-10-17
    • 1970-01-01
    • 2011-10-02
    相关资源
    最近更新 更多