2015-05-20 204 views
2

我試圖實現一個Spring的Oauth2設置與授權服務器在自己的應用程序和資源服務器在單獨的應用程序。春季OAuth2訪問令牌在HTTP頭

我會說,從一開始,由於現行制度的限制,我不得不使用舊版本的春/春季安全的,直到我們可以在系統升級計劃時間:

  • 春天3.1.1春天
  • 安全:3.2.7
  • 春天的OAuth:1.0.5

我所擁有的一切工作,然而,當我從種源服務器請求一個受限制的資源,我有提供access_token作爲查詢參數。

我寧願將其作爲HTTP標頭提供。 Spring Security Oauth中是否有內置的功能?

資源服務器配置

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
    xmlns:sec="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd 
    http://www.springframework.org/schema/security/oauth2 
    http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <!-- Resource Server Filter --> 
    <oauth:resource-server id="resourceServerFilter" resource-id="someResourceId" token-services-ref="tokenServices"/> 
    <!-- END Resource Server Filter --> 

    <!-- Protected resources --> 
    <http pattern="/rest/BasicService/**" create-session="stateless" xmlns="http://www.springframework.org/schema/security" entry-point-ref="oauthAuthenticationEntryPoint"> 
     <anonymous enabled="false"/> 
     <intercept-url pattern="/rest/BasicService/**" access="ROLE_USER"/> 
     <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/> 
     <access-denied-handler ref="oauthAccessDeniedHandler"/> 
    </http> 
    <!-- END Protected resources --> 

    <!-- Authentication --> 
    <sec:authentication-manager xmlns="http://www.springframework.org/schema/security" /> 
    <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/beans"/> 
    <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/> 
    <!-- END Authentication -->   

    <!-- Token Store -->  
    <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore"> 
     <constructor-arg ref="myJdbcTokenStore" /> 
    </bean> 

    <bean id="myJdbcTokenStore" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="java:comp/env/jdbc/someJNDIDatabaseName"/> 
    </bean> 

    <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"> 
     <property name="tokenStore" ref="tokenStore"/> 
    </bean> 
    <!-- END Token Store --> 

</beans> 
+1

添加這樣的授權標頭::::授權:承載<您的訪問令牌在這裏> –

+0

啊,我試過一個授權標頭。但是我忘記了令牌類型。 – dooffas

回答

2

如由prtk_shah註釋中,令牌類型需要被添加到授權報頭。

授權:承載

2

如果您使用的彈簧引導法術,也許通過以下their example,你只需要設置authenticationSchemeheader。任務完成。

security: 
    oauth2: 
    client: 
     ... 
     authenticationScheme: header