{"id":2478,"date":"2021-07-29T21:55:32","date_gmt":"2021-07-29T12:55:32","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2478"},"modified":"2021-07-30T13:49:58","modified_gmt":"2021-07-30T04:49:58","slug":"spring-boot-cache-for-auth-server","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2478","title":{"rendered":"Spring Boot Cache for Authorization Server"},"content":{"rendered":"<h1>Spring Boot Cache for Authorization Server<\/h1>\n<p><a href=\"https:\/\/jistol.github.io\/spring\/2017\/02\/09\/springboot-cache-key\/\">\ucc38\uc870<\/a><\/p>\n<h2>\ubaa9\ud45c<\/h2>\n<p>Authorization Server \uc5d0 Cache \ub97c \uc801\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n<h2>build.gradle \uc218\uc815<\/h2>\n<p>redis \ub294 \ubbf8\ub9ac \uc124\uce58\ub418\uc5b4 \uc788\uc5b4\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-gradle\">dependencies {\n    implementation &#039;org.springframework.boot:spring-boot-starter-data-redis&#039;\n    implementation &#039;org.springframework.boot:spring-boot-starter-cache&#039;\n    \/\/ ......\n}<\/code><\/pre>\n<h2>\ud30c\uc77c\ucd94\uac00<\/h2>\n<p>\uce90\uc2dc\ub97c \ud65c\uc131\ud654 \ud569\ub2c8\ub2e4.<\/p>\n<p>CacheConfig.java<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableCaching\npublic class CacheConfig {\n}<\/code><\/pre>\n<p>RedisConfig.java<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Configuration\npublic class RedisConfig {\n\n    private final RedisProperties redisProperties;\n\n    @Bean\n    public RedisConnectionFactory redisConnectionFactory() {\n        return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());\n    }\n\n    @Bean\n    public RedisTemplate&lt;?, ?&gt; redisTemplate() {\n        RedisTemplate&lt;byte[], byte[]&gt; redisTemplate = new RedisTemplate&lt;&gt;();\n        redisTemplate.setConnectionFactory(redisConnectionFactory());\n        return redisTemplate;\n    }\n}<\/code><\/pre>\n<h2>application.yml \uc218\uc815<\/h2>\n<pre><code class=\"language-yaml\">spring:\n  redis:\n    cluster:\n      nodes: 127.0.0.1:6379<\/code><\/pre>\n<p>\uce90\uc2dc\ub97c \ubd99\uc774\uae30 \uc704\ud574, <code>JdbcClientDetailsService<\/code> \ub97c \ud655\uc7a5\ud569\ub2c8\ub2e4.<\/p>\n<p>CustomJdbcClientDetailsService.java<\/p>\n<pre><code class=\"language-java\">@Service\npublic class CustomJdbcClientDetailsService extends JdbcClientDetailsService {\n\n    public CustomJdbcClientDetailsService(DataSource dataSource) {\n        super(dataSource);\n    }\n\n    @Override\n    @Transactional(readOnly = true)\n    @Cacheable(value=&quot;clientId&quot;)\n    public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {\n        \/\/ System.out.println(&quot;1111111111111111111&quot;);\n        return super.loadClientByClientId(clientId);\n    }\n\n    @Override\n    @Transactional\n    @CacheEvict(value=&quot;clientId&quot;, key=&quot;#clientDetails.getClientId()&quot;)\n    public void updateClientDetails(ClientDetails clientDetails) throws NoSuchClientException {\n        super.updateClientDetails(clientDetails);\n    }\n\n    @Override\n    @Transactional\n    @CacheEvict(value=&quot;clientId&quot;, key=&quot;#clientId&quot;)\n    public void updateClientSecret(String clientId, String secret) throws NoSuchClientException {\n        super.updateClientSecret(clientId, secret);\n    }\n\n    @Override\n    @Transactional\n    @CacheEvict(value=&quot;clientId&quot;)\n    public void removeClientDetails(String clientId) throws NoSuchClientException {\n        super.removeClientDetails(clientId);\n    }\n}<\/code><\/pre>\n<p><code>CustomJdbcClientDetailsService<\/code> \ub97c \ubd99\uc5ec\uc90d\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Configuration\n@EnableAuthorizationServer\npublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {\n\n    \/\/ ......\n\n    private final CustomJdbcClientDetailsService clientDetailsService;\n\n    @Override\n    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {\n        \/\/ clients.jdbc(dataSource).passwordEncoder(passwordEncoder);\n        clients.withClientDetails(clientDetailsService);\n    }\n\n    \/\/ ......\n}<\/code><\/pre>\n<p>\ud55c \ubc88\uc758 \uc561\uc138\uc2a4\ud1a0\ud070\uc744 \uc5bb\uae30\uc704\ud574 13\ubc88\uc758 <code>loadClientByClientId<\/code> \ud638\ucd9c\uc774 \ubaa8\ub450 \uc0ac\ub77c\uc9d1\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-bash\">C:\\Program Files\\Redis&gt;redis-cli\n127.0.0.1:6379&gt; keys *\n1) &quot;clientId::foo&quot;\n2) &quot;spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:110968452271843970018&quot;\n127.0.0.1:6379&gt;\n127.0.0.1:6379&gt; get clientId::foo\n&quot;\\xac\\xed\\x00\\x05sr\\x00Eorg.springframework.security.oauth2.provider.cl...\n.........................<\/code><\/pre>\n<p><code>clientId::foo<\/code> \uac00 <code>redis<\/code> \uc5d0 \uc800\uc7a5\ub418\uc5b4 \uc788\ub294 \uac83\uc744 \ud655\uc778\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot Cache for Authorization Server \ucc38\uc870 \ubaa9\ud45c Authorization Server \uc5d0 Cache \ub97c \uc801\uc6a9\ud569\ub2c8\ub2e4. build.gradle \uc218\uc815 redis \ub294 \ubbf8\ub9ac \uc124\uce58\ub418\uc5b4 \uc788\uc5b4\uc57c \ud569\ub2c8\ub2e4. dependencies { implementation &#039;org.springframework.boot:spring-boot-starter-data-redis&#039; implementation &#039;org.springframework.boot:spring-boot-starter-cache&#039; \/\/ &#8230;&#8230; } \ud30c\uc77c\ucd94\uac00 \uce90\uc2dc\ub97c \ud65c\uc131\ud654 \ud569\ub2c8\ub2e4. CacheConfig.java @Configuration @EnableCaching public class CacheConfig { } RedisConfig.java @RequiredArgsConstructor @Configuration public class RedisConfig { private final RedisProperties redisProperties; @Bean\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2478\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-2478","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-5"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2478"}],"version-history":[{"count":11,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2478\/revisions"}],"predecessor-version":[{"id":2493,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2478\/revisions\/2493"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}