更改Spring Security Oauth2错误响应

时间:2015-10-11 07:40:17

标签: spring spring-security oauth-2.0

当我输入错误的凭据时,我收到400 Bad请求输出。

INFO:org.springframework.security.oauth2.provider.endpoint.TokenEndpoint - 处理错误:InvalidGrantException,凭据错误

我想更改此回复。

我在这里添加了一些配置:

 <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">  
    <intercept-url pattern="/oauth/token" access="isFullyAuthenticated()" />  
    <anonymous enabled="false" />  
    <http-basic entry-point-ref="clientAuthenticationEntryPoint" />  
    <custom-filter ref="clientCredentialsTokenEndpointFilter"  after="BASIC_AUTH_FILTER" />  
    <access-denied-handler ref="oauthAccessDeniedHandler" />  
 </http>  


 <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">  
    <property name="realmName" value="test" />  
 </bean>  

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

 <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler">
    <property name="exceptionTranslator" ref="customExceptionTranslator" />
    <property name="exceptionRenderer" ref="headerOnlyExceptionRender" />
</bean>

<bean id="headerOnlyExceptionRender" class="com.spring.myapp.Exception.HeaderOnlyOAuth2ExceptionRenderer"/>

<bean id="customExceptionTranslator" class="com.spring.myapp.Exception.CustomExceptionTranslator"/>

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

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

 <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">  
    <authentication-provider user-service-ref="notifyUserDetailsService">  
        <password-encoder hash="bcrypt" /> 
    </authentication-provider>  
 </authentication-manager>  

  <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" />

 <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">  
    <property name="tokenStore" ref="tokenStore" />  
    <property name="supportRefreshToken" value="true" />  
    <property name="accessTokenValiditySeconds" value="12000000" />  
    <property name="clientDetailsService" ref="clientDetails" />  
    <property name="tokenEnhancer" ref="tokenEnhancer"/>
 </bean>  

 <bean id="tokenEnhancer" class="com.spring.myapp.security.MyTokenEnhancer" />

<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices">  
  <oauth:authorization-code />  
  <oauth:implicit />  
  <oauth:refresh-token />  
  <oauth:client-credentials />  
  <oauth:password />  
 </oauth:authorization-server>  

 <oauth:resource-server id="resourceServerFilter" resource-id="test" token-services-ref="tokenServices" />  

 <oauth:client-details-service id="clientDetails">  
  <!-- client -->  
    <oauth:client client-id="restapp" authorized-grant-types="authorization_code,client_credentials" authorities="ROLE_APP" scope="read,write,trust" secret="secret" />  

    <oauth:client client-id="restapp" authorized-grant-types="password,authorization_code,refresh_token,implicit" secret="restapp" authorities="ROLE_APP" />  

 </oauth:client-details-service>  

 <sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true">  
  <!--you could also wire in the expression handler up at the layer of the   
   http filters. See https://jira.springsource.org/browse/SEC-1452 -->  
    <sec:expression-handler ref="oauthExpressionHandler" />  
 </sec:global-method-security>  

 <oauth:expression-handler id="oauthExpressionHandler" />  
 <oauth:web-expression-handler id="oauthWebExpressionHandler" />  

0 个答案:

没有答案