簡體   English   中英

Spring Security OAuth2更改JSON錯誤響應格式

[英]Spring Security OAuth2 Change JSON Error Response Format

我有一個基於Spring Security OAuth2的RESTful應用程序。 我一直在嘗試將默認的Spring Security消息格式從XML更改為JSON,並且已經部分成功。

例如 - 我想出了當請求不包含Bearer令牌時如何更改響應格式(以下行代碼)

<bean id="oauthAuthenticationEntryPoint" class ="c.s.m.security.CustomAuthenticationEntryPoint" />

但我無法弄清楚如何捕獲/更改以下兩項的格式。

  1. 當在安全URL中傳遞無效令牌時,Spring Security當前會返回。 我在哪里更改此格式?

     {"error": "invalid_token","error_description": "Invalid access token: 144285e3-9563-420e-8ce"} 
  2. 如何更改BadCredentialsException JSON格式? 目前,它返回類似於上面的JSON?

下面是我的applicationContext.xml

<sec:http pattern="/oauth/token" create-session="stateless"
    use-expressions="true" authentication-manager-ref="authenticationManager">
    <sec:csrf disabled="true" />
    <sec:anonymous enabled="false" />
    <sec:http-basic entry-point-ref="clientAuthenticationEntryPoint" />
    <sec:custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
</sec:http>
<sec:authentication-manager alias="authenticationManager"
    erase-credentials="false">
    <sec:authentication-provider user-service-ref="clientDetailsUserService" />
</sec:authentication-manager>

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

<!-- Entry point - Entry point Filter for token server -->

<bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <property name="realmName" value="Oauth 2 security" />
    <property name="typeName" value="Basic" />
</bean>

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

<!-- Oauth handler Access Denied Handler -->

<bean id="oauthAccessDeniedHandler" class="c.s.m.security.CustomAccessDeniedHandler" />
    <!-- class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> -->

<!-- Server resource -->

<sec:http pattern="/api/**" create-session="never"
    entry-point-ref="oauthAuthenticationEntryPoint" use-expressions="true" >
    <sec:csrf disabled="true" />
    <sec:anonymous enabled="false" />
    <sec:intercept-url pattern="/api/**" access="hasRole('ROLE_ADMIN')" />
    <sec:custom-filter ref="resourceServerFilter"
        before="PRE_AUTH_FILTER" />
    <sec:access-denied-handler ref="oauthAccessDeniedHandler" />
</sec:http>

<!-- Entry point resource -->

<bean id="oauthAuthenticationEntryPoint" class ="c.s.m.security.CustomAuthenticationEntryPoint" />          

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

<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="300000" />
    <property name="clientDetailsService" ref="clientDetails" />
</bean>    
<bean id="tokenStore"  class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore">
    <constructor-arg ref="dataSource" />
</bean>
<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 authentication-manager-ref="userAuthenticationManager" />
</oauth:authorization-server>

<sec:authentication-manager id="userAuthenticationManager">
    <sec:authentication-provider ref="customUserDetailsService" />
</sec:authentication-manager>

發送Accept: application/json請求標頭中的Accept: application/json將解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM