在身份验证期间,ClientDetailsS ervice被调用6次

问题描述 投票:0回答:1

我们为OAuth2授权服务器编写了一个自定义ClientDetailsS​​ervice:

public class MyClientDetailsService implements ClientDetailsService {

 @Override
 public ClientDetails loadClientByClientId(String clientId) {
 log.info("Got called!");
 ...
 }
}

日志看起来像这样:

... Got called!
... Got called!
... Got called!
... Got called!
... Got called!
... Got called!

相关性:

    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.1.11.RELEASE</version>
    </dependency>

在官方的git-hub中已经讨论了这个话题,但到目前为止还没有修复过。 (https://github.com/spring-projects/spring-security-oauth/issues/141

我的问题是,有人知道这个问题的解决方法吗?我们每次调用都会访问我们的数据库,这非常耗费内存。

java spring-security-oauth2 spring-oauth2
1个回答
1
投票

你需要使用spring-boot2提供的缓存。

请通过@EnableCaching启用spring boot中的缓存

@SpringBootApplication
@EnableCaching
class Starter {
   public static void main(String[] args) {
      SpringApplication.run(Starter.class, args);
   }
}

然后使用@Cacheable缓存loadClientByClientId。

public class MyClientDetailsService implements ClientDetailsService {

  @Override
  @Cacheable("ClientDetails")
  public ClientDetails loadClientByClientId(String clientId) {
    log.info("Got called!");
    ...
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.