当前位置: 首页 > 知识库问答 >
问题:

spring security oauth2(2.0.8)正在获取在Memory tokenstore中使用的无效访问令牌

昝晗昱
2023-03-14

尝试在我的应用程序中实现spring security oauth2。

我可以使用以下方法获取访问令牌和刷新令牌:

{“access_token”:“798C7E71-983B-4137-A0CB-CEAE4E9B4190”“token_type”:“承载”“refresh_token”:“0752B8FF-5086-4457-918D-54376C7A2BEC”“expires_in”:299“scope”:“读信任写”}

当我试图使用下面的url访问受保护的资源时

http://localhost:8080/xapp/data/product/api/index/?access_token=798c7e71-983b-4137-a0cb-ceae4e9b4190

我得到的是:

{“error”:“invalid_token”“error_description”:“无效访问令牌:DB48214C-04D7-4D6B-AA34-6D16C9C2A438”}

ApplicationContext-Security.xml:

 <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans 
    xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    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.0.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.xsd">

      <http pattern="/login*" security="none" />


      <http pattern="/*.html" security="none" />
      <http pattern="/*.pdf" security="none" />
      <http pattern="/*.xls" security="none" />
      <http pattern="/cache-viewer.jnlp" security="none" />
      <!-- /old documentation URLs -->

      <!-- servlets -->
      <http pattern="/Index" security="none" />
    <http pattern="/ServletRedirector" security="none" />

    <!-- This is where we tells spring security what URL should be protected 
        and what roles have access to them -->
    <http pattern="/data/**" entry-point-ref="oauthAuthenticationEntryPoint"
        create-session="never" xmlns="http://www.springframework.org/schema/security"
        use-expressions="true">
        <anonymous enabled="false" />       
        <access-denied-handler ref="oauthAccessDeniedHandler" />
        <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    </http> 

    <http pattern="/oauth/token" create-session="stateless"
        use-expressions="true">
        <!--  authentication-manager-ref="clientAuthenticationManager"  -->
        <intercept-url pattern="/oauth/token" access="hasRole('Administrator')" />
        <anonymous enabled="false" />
        <custom-filter ref="clientCredentialsTokenEndpointFilter"
            after="BASIC_AUTH_FILTER" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
        <http-basic entry-point-ref="clientAuthenticationEntryPoint" />
        </http>

     <http use-expressions="true" disable-url-rewriting="true" entry-point-ref="authenticationChooser">
      <!-- /servlets -->
      <intercept-url pattern="/**" access="isAuthenticated()" />
      <intercept-url pattern="/" access="isAuthenticated()" />
      <form-login login-page="/login" authentication-failure-url="/login?login_error=1" authentication-success-handler-ref="authSuccessHandler"/>
      <logout logout-url="/logout" logout-success-url="/login" />
      <remember-me key="XappWebClient" services-ref="rememberMeServices" />
      <custom-filter ref="jbossSecurityFilter" after="REMEMBER_ME_FILTER" />
      </http>

    <beans:bean id="oauthAuthenticationEntryPoint"
        class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <beans:property name="realmName" value="Xapp" />
    </beans:bean>

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

    <beans:bean id="oauthAccessDeniedHandler"
        class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />

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


    <beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased"
          xmlns="http://www.springframework.org/schema/beans">
        <beans:constructor-arg>
            <beans:list>
                <beans:bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/>
                <beans:bean class="org.springframework.security.access.vote.RoleVoter"/>
                <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
            </beans:list>
        </beans:constructor-arg>
    </beans:bean>

    <authentication-manager id="clientAuthenticationManager"
        xmlns="http://www.springframework.org/schema/security">
        <authentication-provider user-service-ref="clientDetailsUserService" />
    </authentication-manager>

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

    <!-- This defined token store, we have used inmemory tokenstore for now 
        but this can be changed to a user defined one -->
    <beans:bean id="tokenStore"
        class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" />

    <!-- This is where we defined token based configurations, token validity 
        and other things -->  
    <beans:bean id="tokenServices"
        class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
        <beans:property name="tokenStore" ref="tokenStore" />
        <beans:property name="supportRefreshToken" value="true" />
        <beans:property name="accessTokenValiditySeconds" value="300000"/>
        <beans:property name="clientDetailsService" ref="clientDetails" />
    </beans:bean>

    <beans:bean id="userApprovalHandler"
        class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler">
        <beans:property name="tokenStore" ref="tokenStore" />
        <beans:property name="requestFactory" ref="oAuth2RequestFactory" />
    </beans:bean>

    <beans:bean id="oAuth2RequestFactory"
        class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory">
        <beans:constructor-arg ref="clientDetails" />
    </beans:bean>

    <beans:bean id="approvalStore" class="org.springframework.security.oauth2.provider.approval.TokenApprovalStore">
        <beans:property name="tokenStore" ref="tokenStore"/>
    </beans:bean>

       <!-- OAuth2 Authorization Server -->
    <oauth:authorization-server client-details-service-ref="clientDetails"
                                token-services-ref="tokenServices"
                                user-approval-handler-ref="userApprovalHandler">
        <oauth:authorization-code/>
        <oauth:implicit/>
        <oauth:refresh-token/>
        <oauth:client-credentials/>
        <oauth:password authentication-manager-ref="authenticationManager"/>
    </oauth:authorization-server>

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

    <oauth:client-details-service id="clientDetails">
        <!-- client -->     
        <oauth:client client-id="Xapp"
            authorized-grant-types="password,authorization_code,refresh_token,implicit"
            secret="Xapp" scope="read,write,trust" authorities="Administrator" access-token-validity="300" refresh-token-validity="600"/>

    </oauth:client-details-service>

   <authentication-manager alias="authenticationManager">
      <authentication-provider ref="jaasAuthenticationProvider"/>
   </authentication-manager>

   <beans:bean id="rememberMeServices"
      class="com.Xapp.Xapp.web.authentication.rememberme.RememberMeServices">
      <beans:property name="userDetailsService" ref="userDetailsService" />
      <beans:property name="key" value="XappWebClient" />
   </beans:bean>

   <beans:bean id="jaasNameCallBackHandler" 
      class="com.Xapp.Xapp.web.authentication.XappNameCallbackHandler">
      <beans:property name="userDetailsService" ref="userDetailsService" />
      <beans:property name="callbackHandler">
         <beans:bean class="org.springframework.security.authentication.jaas.JaasNameCallbackHandler"/>
      </beans:property>
   </beans:bean>

   <beans:bean id="jaasAuthenticationProvider" 
      class="org.springframework.security.authentication.jaas.JaasAuthenticationProvider">
      <beans:property name="refreshConfigurationOnStartup" value="false"/> 
      <beans:property name="loginConfig" value="/WEB-INF/login.conf" />
      <beans:property name="loginContextName" value="Xapp" />
      <beans:property name="callbackHandlers">
         <beans:list>
            <beans:ref bean="jaasNameCallBackHandler" />
            <beans:bean class="org.springframework.security.authentication.jaas.JaasPasswordCallbackHandler" />
         </beans:list>
      </beans:property>
      <beans:property name="authorityGranters">
         <beans:list>
            <beans:bean class="com.Xapp.Xapp.web.authentication.XappAuthorityGranter" />
         </beans:list>
      </beans:property>
   </beans:bean>

   <beans:bean id="userDetailsService" class="com.Xapp.Xapp.web.authentication.XappUserDetailsService">
   </beans:bean>

   <beans:bean id="jbossSecurityFilter" class="com.Xapp.Xapp.web.authentication.JBossSecurityFilter">
      <beans:property name="clientLoginDomain" value="client-login" />
      <beans:property name="callbackHandler">
         <beans:bean class="com.Xapp.Xapp.web.authentication.SecurityContextHolderAwareCallbackHandler" />
      </beans:property>
   </beans:bean>

   <beans:bean id="authSuccessHandler"
      class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
      <beans:property name="redirectStrategy" ref="XappRedirectStrategy"></beans:property>
   </beans:bean>

   <beans:bean id="XappRedirectStrategy"
      class="com.Xapp.Xapp.web.authentication.XappRedirectStrategy">
   </beans:bean>

   <beans:bean id="formAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
      <beans:property name="loginFormUrl" value="/login" />
   </beans:bean>

   <beans:bean id="authenticationChooser" class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint">
      <beans:constructor-arg>
         <beans:map>
             <beans:entry key="#{new com.Xapp.Xapp.web.authentication.DataRequestMatcher()}" value-ref="oauthAuthenticationEntryPoint" />
         </beans:map>
      </beans:constructor-arg>
      <beans:property name="defaultEntryPoint" ref="formAuthenticationEntryPoint" />
   </beans:bean>
</beans:beans>
@Controller
@RequestMapping("/data/product")
public final class AppController extends AbstractDataController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody public List<Data> getProducts() throws ServerException  {
  final List<DataTO> dataTOs = productLogic.findDataTOsForCurrentUser();
  Collections.sort(dataTOs, HasName.COMPARATOR);
  return ListConverter.convert(dataTOs, fromDataTO);
 }
}
2016-02-02 11:11:16,268 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] (default task-3) HttpSession returned null object for SPRING_SECURITY_CONTEXT
2016-02-02 11:11:16,269 DEBUG [org.springframework.security.web.context.HttpSessionSecurityContextRepository] (default task-3) No SecurityContext was available from the HttpSession: io.undertow.servlet.spec.HttpSessionImpl@4439d585. A new one will be created.
2016-02-02 11:11:16,315 DEBUG [org.springframework.security.web.FilterChainProxy] (default task-3) /data/product/index at position 2 of 10 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2016-02-02 11:11:16,315 DEBUG [org.springframework.security.web.FilterChainProxy] (default task-3) /data/product/index at position 3 of 10 in additional filter chain; firing Filter: 'OAuth2AuthenticationProcessingFilter'
2016-02-02 11:11:16,315 DEBUG [org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter] (default task-3) Entering Do filter>>>>>>>>>>>>>>>>>>>>
2016-02-02 11:11:16,316 DEBUG [org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter] (default task-3) !!!!!!!!!!request>>>>>>>>> org.springframework.security.web.context.HttpSessionSecurityContextRepository$Servlet3SaveToSessionRequestWrapper@590ca42d
2016-02-02 11:11:16,317 DEBUG [org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter] (default task-3) !!!!!!!!!!authentication>>>>>>>>> org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken@763c08a: Principal: 34a81f49-528d-4087-b192-414b6e2224b6; Credentials: [PROTECTED]; Authenticated: false; Details: remoteAddress=127.0.0.1, sessionId=<SESSION>, tokenType=BearertokenValue=<TOKEN>; Not granted any authorities
2016-02-02 11:11:16,317 DEBUG [org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager] (default task-3) >>>Call authenticate>>>> token 34a81f49-528d-4087-b192-414b6e2224b6
2016-02-02 11:11:16,317 DEBUG [org.springframework.security.oauth2.provider.token.DefaultTokenServices] (default task-3) >>>>>>accessTokenValue>>>>>>>>>>>>>>> 34a81f49-528d-4087-b192-414b6e2224b6
2016-02-02 11:11:16,317 DEBUG [org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore] (default task-3) >>>>MAP>>>>>>>{}
2016-02-02 11:11:16,317 DEBUG [org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter] (default task-3) <<<<<<<<<<<Trace Error>>>>>>>>>>>>>>>>>>
2016-02-02 11:11:16,339 ERROR [stderr] (default task-3) error="invalid_token", error_description="Invalid access token: 34a81f49-528d-4087-b192-414b6e2224b6"

2016-02-02 11:11:16,339 ERROR [stderr] (default task-3)     at org.springframework.security.oauth2.provider.token.DefaultTokenServices.loadAuthentication(DefaultTokenServices.java:237)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager.authenticate(OAuth2AuthenticationManager.java:88)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:152)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

2016-02-02 11:11:16,340 ERROR [stderr] (default task-3)     at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)

可能与OAuth2重复:无效的访问令牌,但未应答。对堆栈中的其他链接进行了尝试,但无法解决我的问题。任何关于配置OAuth2 Spring Security2.0.8的帮助或建议都是非常好的。

我们可以使用JDBCtokenstore和jwttokenstore进行配置,但仍然不能使用InMemorystore,任何关于这方面的帮助都将非常棒!!!

共有1个答案

燕照
2023-03-14

从资源API来看,您似乎在请求参数中传递access_token。您必须在请求头中传递access_token,如下所示:

授权:承载

卷曲示例:

curl-x get-h“Authorization:Bearer 89AF6541-F87F-4C63-BE6D-6012426BB745”-h“Cache-Control:no-cache”“http://localhost:8080/xapp/data/product/api/index”

 类似资料:
  • 在我的web应用程序中,我使用Firebase进行身份验证,要访问任何API,我必须从Firebase进行身份验证。 问题:如何在Postman中获取firebase的访问令牌? 我有两种解决此问题的方法: 1) 在postman中从firebase获取访问令牌,并将该访问令牌存储在postman global env中。变量,然后我可以执行其他API请求。(这里我不知道如何在邮递员那里获得访问令

  • 我有一个由 AAD B2C 访问令牌保护的 REST API。我现在想要添加可用于 API 负载测试的 Web 测试。 我跌跌撞撞地从AADB2C为我的测试用户获取了一个有效的访问令牌。我想以某种方式从对AADB2C的REST API调用中获取一个令牌作为响应。 在网上朝这个方向看,我已经找到了许多指向这个没有记录的endpoint的页面 其中我需要提供一些参数。到目前为止,我发现 < li >客

  • 我正在尝试使用spotipy向特定用户验证我的python应用程序。我以前和其他一些用户做过这件事,没有任何问题。然而,每当我尝试使用这个用户时,我总是得到“{”错误“:“无效的授权”,“错误描述“:“无效的授权码”}” 我还尝试了几种手动方式,包括卷曲: curl-H“授权:基本OTQxNDliNjI3…WRhMzk5YTY2ZTliZWY=“-d授权类型=授权代码-d代码=AQByvdq4Vr

  • 目前访问类型处于联机状态。当我需要访问驱动器上的文件/文件夹时,我将浏览器重定向到Google URL并获得访问代码: 一切运转良好!但我只需要第一次重定向。 当我谷歌时,在google Drive API文档中,我发现我可以通过浏览器重定向获得刷新令牌,并将其保存在数据库中。(换句话说,我可以使用脱机访问)。 而且每次当我需要从google drive读取数据时,我使用刷新令牌获得访问令牌,而无

  • 我已经实现了FacebookSDK,借助它可以通过以下代码获得facebook身份验证和用户信息。 但是要发布提要(https://graph.facebook.com/me/feed/access_token=)从这个登录用户我需要访问令牌。如果有人知道如何在iOS 6.0中获取访问令牌,请帮助我。 感谢所有人。

  • 我试图使用从Spring应用程序中的公共客户端获取访问令牌。 谁能帮我弄清楚我做错了什么吗?