如何将缓存应用于spring oauth ClientDetailsS​​ervice

时间:2015-01-30 09:27:36

标签: spring oauth-2.0 ehcache

这是一个答案,而不是一个问题 - 将其记录在可能寻找此事的其他人身上。

我们正在使用Spring JdbcClientDetailsS​​ervice,并且在当前版本2.0.5中,它为每个资源请求/令牌请求生成多个查询,并且可能会导致一些性能问题。 所以我们按照Spring团队的建议(在2.0.3版本中)将缓存放在这个服务上

我们已经有了一个ehcache设置,所以我需要做的就是几个aop声明。

更新 现在我正在考虑在令牌和身份验证对象上放置一个缓存,因为它们也被查询了很多。但事实证明TokenStore.readAuthentication存在问题 - 我猜方法重载存在问题,导致一个签名是spring而另一个是token对象。

我尝试过类似的东西,但它不起作用

<aop:advisor advice-ref="authByTokenIdAdvice" pointcut="execution(* org.springframework.security.oauth2.provider.token.TokenStore.readAuthentication(String))"/>

有人有想法吗?

1 个答案:

答案 0 :(得分:2)

这是用于缓存客户端详细信息。仅在非群集环境中,在群集环境中相关 - 我建议覆盖客户端服务并向其他节点发送消息以清除缓存的项目。

<cache:advice id="cacheClientsAdvice" cache-manager="cacheManager">
            <cache:caching cache="OauthClientDetailsServiceCache">
                <cache:cacheable method="loadClientByClientId" key="#clientId"/>
            </cache:caching>
        </cache:advice>
        <cache:advice id="cacheEvictClient" cache-manager="cacheManager">
            <cache:caching cache="OauthClientDetailsServiceCache">

                <cache:cache-evict method="removeClientDetails" key="#clientId" before-invocation="false"/>
            </cache:caching>
        </cache:advice>

        <!-- apply the cacheable behavior to all ClientDetailsService interface methods -->
        <aop:config>
            <aop:advisor advice-ref="cacheClientsAdvice" pointcut="execution(* org.springframework.security.oauth2.provider.ClientDetailsService.*(..))"/>
        </aop:config>
        <aop:config>
            <aop:advisor advice-ref="cacheEvictClient" pointcut="execution(* org.springframework.security.oauth2.provider.ClientRegistrationService.*(..))"/>
        </aop:config>