Announcement Announcement Module
Collapse
No announcement yet.
Spring OAuth and Jersey Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring OAuth and Jersey

    Hi all i am trying to integrate oauth into a jersey application.

    Obviously jersey is the service provider and i am attempting to adapt the sparklr / tonr from the tutorial to work. I have a spring mvc client trying to access a jersey service.


    Anyone know if this is actually possible?

  • #2
    Sorry should of provided more details

    The application is a small Library app. This is implemented in Jersey. Below is the security config file for the service side.

    Code:
        <beans:import resource="applicationContext.xml"/>
        <http auto-config="true">
            <!-- Restrict URLs based on role -->
            <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
            <intercept-url pattern="/logoutSuccess*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
            
            <intercept-url pattern="/css/*.css" access="IS_AUTHENTICATED_ANONYMOUSLY" />
            <intercept-url pattern="/request_token_authorized.jsp" access="ROLE_BORROWER" />
        	<intercept-url pattern="/oauth/**" access="ROLE_BORROWER" />
            <intercept-url pattern="/**" access="ROLE_BORROWER" />
    
            <!-- Override default login and logout pages -->
            <form-login login-page="/login.jsp"  
                                 default-target-url="/index.jsp" 
                                 authentication-failure-url="/login.jsp?login_error=1" />
            <logout logout-url="/logout" logout-success-url="/logoutSuccess.html" />
        </http>
        
      <authentication-manager>
        <authentication-provider>
        	<password-encoder hash="md5" />
            <jdbc-user-service data-source-ref="master_dataSource" />
            
            <!-- 
                david:newyork
                alex:newjersey
                tim:illinois
            --> 
            <!-- 
            <security:password-encoder hash="md5" />
            <security:user-service>
                <security:user name="david" password="369389d19e24204b4927e30dd7c39efc" authorities="ROLE_BORROWER,ROLE_ADMIN" />
                <security:user name="alex" password="847c6f184197dc1545d9891d42814a7d" authorities="ROLE_USER" />
                <security:user name="tim" password="0513111ff330e25c631b5d3e9c0a4aae" authorities="ROLE_USER" />
            </security:user-service>
            -->
            
        </authentication-provider>
      </authentication-manager>
      
      <beans:bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices">
        <beans:property name="supportRefreshToken" value="true"/>
      </beans:bean>
    
      
      <oauth:provider client-details-service-ref="clientDetails" token-services-ref="tokenServices" >
        <oauth:verification-code user-approval-page="/oauth/confirm_access"/>
      </oauth:provider>
    
      <oauth:client-details-service id="clientDetails">
        <oauth:client clientId="my-trusted-client" authorizedGrantTypes="password,authorization_code,refresh_token"/>
        <oauth:client clientId="my-less-trusted-client" authorizedGrantTypes="authorization_code"/>
        <oauth:client clientId="library" authorizedGrantTypes="authorization_code"/>
      </oauth:client-details-service>
        
    </beans:beans>
    The i have written a small spring mvc app to access the library services. Below is the security config for the client application
    Code:
      <!--Basic application beans.-->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
    
      <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
            <prop key="/hello_World.htm">homeController</prop>
            <prop key="/moreContent.htm">moreContent</prop>
            <prop key="/library/libraryBooks.htm">libraryBooks</prop>
          </props>
        </property>
        <property name="alwaysUseFullPath" value="true"/>
      </bean>
      
      <bean id="homeController" class="com.atech.library.client.controller.HomeController">
      
      </bean>
      
      <bean id="moreContent" class="com.atech.library.client.controller.MoreContentConroller">
      
      </bean>
      
      <bean id="libraryBooks" class="com.atech.library.client.controller.LibraryController">
      	<property name="ls" ref="libraryService"/>
      </bean>
      
      <bean id="libraryService" class="com.atech.library.client.service.LibraryServiceImpl">
        <property name="booksURL" value="http://localhost:9082/library/webresources/book/allBooks"/>
        <property name="libraryURLPattern" value="http://localhost:9082/library/webresources/book/%s"/>
        <property name="lrt">
          <bean class="org.springframework.security.oauth2.consumer.OAuth2RestTemplate">
            <constructor-arg ref="library"/>
          </bean>
        </property>
      </bean>
           
    </beans>
    and the security file is as below
    Code:
      <http auto-config='true' access-denied-page="/login.jsp">
        <intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <intercept-url pattern="/**" access="ROLE_USER" />
        
    	<intercept-url pattern="/library/**" access="ROLE_USER" />
        <form-login authentication-failure-url="/login.jsp" 
        			default-target-url="/index.jsp" 
        			login-page="/login.jsp"/>
        <logout logout-success-url="/index.jsp" />
      </http>
    
      <authentication-manager>
        <authentication-provider>
          <user-service>
            <user name="marissa" password="kangaroo" authorities="ROLE_USER" />
            <user name="sam" password="wombat" authorities="ROLE_USER" />
          </user-service>
        </authentication-provider>
      </authentication-manager>
    
      <!--apply the oauth client context-->
      <oauth:client/>
    
      <!--define an oauth 2 resource for sparklr-->
      <oauth:resource id="library" type="authorization_code" clientId="library"
                      accessTokenUri="http://localhost:9082/library/oauth/authorize"
                      userAuthorizationUri="http://localhost:9082/library/oauth/user/authorize"/>
    
    </beans:beans>
    can anyone spot why i might be getting the following exception

    Code:
    DEBUG 01-02-11 15:18 [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver] Resolving exception from handler [com.atech.library.client.controller.LibraryController@46ac97cc]: org.springframework.security.oauth2.consumer.OAuth2AccessTokenRequiredException: No OAuth 2 security context has been established. Unable to access resource 'library'.
    DEBUG 01-02-11 15:18 [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver] Resolving exception from handler
    thanks in advance for any help

    Comment


    • #3
      Did you ever get this resolved. Im using a very similar configuration and am running into the same issue.

      Comment

      Working...
      X