2013-05-17 32 views
3

我試圖建立一個Spring OAuth2客戶端,對我自己的提供者和資源服務器進行身份驗證。提供者和資源服務器在我的情況下是一個應用程序OAuth2春天客戶端錯誤:沒有重定向URI已建立

我在使用Spring 3.2設置OAuth客戶端時遇到問題。當我把在控制器中OAuth2RestTemplate(通過獲取網頁)我得到這個錯誤:

java.lang.IllegalStateException: No redirect URI has been established for the current request. at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getRedirectForAuthorization(AuthorizationCodeAccessTokenProvider.java:283) at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:159) at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142) at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118) at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:216) ...

據我瞭解重定向自動從當前請求的URI設置。無論如何,我試圖設置oauth:資源屬性pre-established-redirect-uri =「http:// localhost:8080/myresourcesercer /」。然後我得到這個錯誤:

org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval ...

如何正確設置重定向uri或我錯過了什麼?

謝謝!

這裏是我的配置:

的web.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/mvc-dispatcher-servlet.xml, 
       /WEB-INF/spring-security.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

彈簧security.xml文件

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

    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-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/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<oauth:client id="my-client" /> 

<oauth:resource id="security" type="authorization_code" client-id="my-client" client-secret="secret" access-token-uri="http://localhost:8080/provider/oauth/token" 
scope="read,write" user-authorization-uri="http://localhost:8080/provider/oauth/authorize" authentication-scheme="query"/> 

<bean id="fooService" class="com.mypackage.serviceImpl.FooServiceImpl"> 
    <property name="secureRestTemplate"> 
     <oauth:rest-template resource="security" /> 
    </property> 
</bean> 

</beans> 

MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
    xmlns:sec="http://www.springframework.org/schema/security" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util-3.2.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-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/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

<mvc:default-servlet-handler /> 
<mvc:annotation-driven> 

     <mvc:message-converters register-defaults="true"> 
    ... 
    </mvc:message-converters> 
    </mvc:annotation-driven> 

<context:component-scan base-package="com.mypackage.controller" /> 
<context:component-scan base-package="com.mypackage.serviceImpl" /> 

<mvc:resources mapping="/css/**" location="/css/"/> 


<bean id="viewResolver" 
    class=" org.springframework.web.servlet.view.InternalResourceViewResolver" > 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

… 

</beans> 
+0

你有沒有想過這個?我遇到了同樣的問題 – acvcu

+0

我發現第二個錯誤信息是誤導性的。不知道實際的問題是什麼,但你無法從錯誤信息中猜出它。我現在的(和工作)客戶端配置是這樣的:' \t \t \t'請問這幫你? – Mirko

+0

您實際使用哪種授權類型?您是否收到「您想授予訪問權限......」的請求嗎?我在這裏有同樣的問題,但仍然沒有解決方案。不幸的是,我的OAuth提供商只允許授權授權。 –

回答

0

OAuth2ClientContextFilter將設置爲您重定向URI。您可以手動配置它,或讓Spring Security自動爲您設置。

如果你想Spring Security的設置它,那麼你就需要在你的web.xml

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

變化而你也需要一個<http/> block

-1

造成的照片的問題。此行的代碼(型號模型)

ObjectNode result = facebookRestTemplate 
       .getForObject("https://graph.facebook.com/me/friends", ObjectNode.class); 

它成功登錄後將其重定向到Facebook頁面,並返回了像t這樣的URL他,但似乎沒有工作之後......

Check Link

我覺得出事了導致問題。

相關問題