2012-10-05 24 views
5

Tôi muốn triển khai OAuth 2.0 cho dự án Spring 3.1 và RESTEasy của mình. Dự án là một dịch vụ REST dựa trên JSON. Tôi sử dụng Spring Security 3.1 và spring-security-oauth2 phiên bản 1.0.0.RC2 (nên là phiên bản mới nhất). Cho đến nay tôi có thiết lập bảo mật mùa xuân với cài đặt mặc định. Tôi cũng có cấu hình cơ bản (mặc định) cho OAuth 2.0.Không có bộ chuyển đổi nào cho lỗi trình xử lý đối với điểm cuối của nhà cung cấp oauth2

Tôi đã sử dụng dịch vụ REST trước đó, nó hoạt động hoàn hảo. An ninh mùa xuân cũng có vẻ hoạt động tốt. Tôi được chuyển hướng đến trang đăng nhập nếu tôi mở một liên kết đến dịch vụ REST của mình. Sau khi đăng nhập, tôi có thể thực hiện các cuộc gọi REST cung cấp kết quả mong đợi.

Khi tôi mở het url localhost:8080/tools-service/oauth/token hoặc localhost:8080/tools-service/oauth/error, để kiểm tra OAuth, tôi nhận được một lỗi 500. Các lỗi sau đây là hiển thị khi tôi truy cập /oauth/token. Lỗi cho /oauth/error là mô phỏng.

HTTP Status 500 - No adapter for handler [public org.springframework.http.ResponseEntity org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.getAccessToken(java.security.Principal,java.lang.String,java.util.Map)]: Does your handler implement a supported interface like Controller?

Nếu tôi đúng này ngụ ý rằng có một lỗi trong TokenEndpoint.getAccessToken chức năng? Vì lớp đó là một phần của khung công tác Spring (và tôi đã tra cứu mã, điều này có vẻ ổn) Tôi không nghĩ vấn đề thực sự liên quan đến các lớp đó. Điều đó khiến tôi không biết gì.

Bây giờ tôi muốn biết tại sao điều này xảy ra và cách tôi có thể giải quyết vấn đề này. Tôi coi thực tế là tôi có thể không được phép truy cập vào các URL đó trong trình duyệt. Tuy nhiên, nếu tôi thử cùng với Sparklr2 (the Spring OAuth 2.0 sample application), tôi nhận được một thông báo XML (cho/oauth2/token) và một trang lỗi (cho/oauth2/error), như mong đợi.

Bất kỳ trợ giúp hoặc mẹo nào sẽ được đánh giá cao.


an ninh liên quan đến đoạn từ 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> 

bối cảnh ứng dụng của tôi tải các tập tin an ninh-config.xml sau tôi đã tạo:

<?xml version="1.0" encoding="UTF-8"?> 

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:sec="http://www.springframework.org/schema/security" 
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
    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.1.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.1.xsd 
         http://www.springframework.org/schema/security/oauth2 
         http://www.springframework.org/schema/security/spring-security-oauth2.xsd"> 

    <sec:http auto-config="true"> 
     <sec:intercept-url pattern="/**" access="ROLE_USER" /> 
    </sec:http> 

    <sec:authentication-manager> 
     <sec:authentication-provider> 
      <sec:user-service> 
       <sec:user name="user1" password="test123" authorities="ROLE_USER" /> 
       <sec:user name="user2" password="hello123" authorities="ROLE_USER" /> 
      </sec:user-service> 
     </sec:authentication-provider> 
    </sec:authentication-manager> 

    <sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"> 
     <sec:expression-handler ref="oauthExpressionHandler" /> 
    </sec:global-method-security> 


    <bean id="clientDetailsService" class="be.collectortools.rest.service.security.CollectorDetailsServiceImpl" /> 

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

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

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

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

</beans> 

Việc thực hiện CollectorClientDetails chỉ giả mã:

@Service 
public class CollectorDetailsServiceImpl implements ClientDetailsService { 

    @Resource 
    private CollectorClientDetailsRepository collectorClientDetailsRepository; 

    @Override 
    public ClientDetails loadClientByClientId(final String clientId) throws OAuth2Exception { 
     CollectorClientDetails dummyClient = new CollectorClientDetails(); 
     dummyClient.setClientId(clientId); 

     return dummyClient; 
    } 

} 

Trả lời

11

Sau khi cho phép vấn đề này nguội đi trong vài ngày, tôi đã thực hiện tìm kiếm mới trên Google. Điều này dẫn tôi đến diễn đàn Spring Source: http://forum.springsource.org/showthread.php?130684-OAuth2-No-adapter-for-handler-exception.

Ở đây tôi thấy rằng banifou có cùng vấn đề. Dave Syer đã trả lời câu hỏi như thế này:

Có vẻ như bạn đã xóa <mvc:annnotation-driven/> khỏi vanilla sparklr. Tôi nghĩ rằng nếu bạn đặt lại trong bộ điều hợp bộ xử lý sẽ được xác định cho bạn.

Bảo mật mùa xuân dựa trên khung công tác Spring MVC để xử lý các yêu cầu và phản hồi. Cần có cả khung MVC và được thiết lập đúng để OAuth bảo mật Spring hoạt động.

Giải pháp là tôi thêm 2 tag vào bối cảnh ứng dụng của tôi:

  • <mvc:annotation-driven />

Tận dụng tối framwork MVC sẵn sàng để xử lý các chú thích. Điều này làm cho @FrameworkEndpoint hoạt động đúng cách. Người sau được sử dụng trong public class TokenEndpoint, đã mang đến cho lỗi 500.

  • <mvc:default-servlet-handler />

xử lý này sẽ chuyển tất cả các yêu cầu đến Servlet mặc định. Do đó điều quan trọng là nó vẫn tồn tại theo thứ tự của tất cả các URL HandlerMappings khác. Đó sẽ là trường hợp nếu bạn sử dụng <mvc:annotation-driven>. (Trích dẫn từ tài liệu Spring.)

Thông tin thêm có thể tìm thấy tại đây: annotation-driven, default-servlet-handler.

<?xml version='1.0' encoding='UTF-8'?> 

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

    <context:component-scan base-package="be.collectortools.rest"/> 
    <context:annotation-config/> 

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

    <import resource="classpath:springmvc-resteasy.xml"/> 
    <import resource="mongo-config.xml"/> 
    <import resource="security-config.xml"/> 

</beans> 
+4

Tôi rất vui vì có những người khác trả lời câu hỏi của riêng họ như thế này. Đây là một ví dụ tốt cho dù vấn đề của bạn có mơ hồ bao giờ cũng có ít nhất một người dùng khác gặp phải điều tương tự. Nếu tôi có thể mua cho bạn một ly bia tôi muốn, nhưng có một upvote ít nhất :-) – Joe

+0

Vertongen, tôi đã có cùng một vấn đề, tất cả các kết quả google liên quan đã được đọc, nhưng chỉ có bạn giải quyết vấn đề của tôi, bị thiếu trong mã của tôi. Cảm ơn bạn đã trả lời câu hỏi của riêng mình và giúp tôi! – szpetip

+0

Chào mừng bạn. Tôi hạnh phúc điều này vẫn hữu ích sau tất cả thời gian này. – Vertongen

Các vấn đề liên quan