From 6e8ef4b35a106cb407e8e48dee75a036122639b9 Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 29 Apr 2019 01:34:00 +0800 Subject: [PATCH] Refactor string cache store usage --- .../app/security/filter/AdminAuthenticationFilter.java | 8 +++----- .../run/halo/app/service/impl/ThemeServiceImpl.java | 10 +++++----- .../run/halo/app/service/impl/UserServiceImpl.java | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java b/src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java index 26972ff2b..c49cb06f4 100644 --- a/src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java +++ b/src/main/java/run/halo/app/security/filter/AdminAuthenticationFilter.java @@ -15,7 +15,6 @@ import run.halo.app.security.context.SecurityContextHolder; import run.halo.app.security.context.SecurityContextImpl; import run.halo.app.security.support.UserDetail; import run.halo.app.service.UserService; -import run.halo.app.utils.JsonUtils; import javax.servlet.FilterChain; import javax.servlet.ServletException; @@ -84,15 +83,14 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter { if (StringUtils.isNotBlank(token)) { // Valid the token - // TODO Add key prefix - Optional userDetailOptional = cacheStore.get(token); + Optional optionalUserDetail = cacheStore.getAny(token, UserDetail.class); - if (!userDetailOptional.isPresent()) { + if (!optionalUserDetail.isPresent()) { getFailureHandler().onFailure(request, response, new AuthenticationException("The token has been expired or not exist").setErrorData(token)); return; } - UserDetail userDetail = JsonUtils.jsonToObject(userDetailOptional.get(), UserDetail.class); + UserDetail userDetail = optionalUserDetail.get(); // Set security SecurityContextHolder.setContext(new SecurityContextImpl(new AuthenticationImpl(userDetail))); diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index e8ae703e1..8eb6422ab 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -36,7 +36,6 @@ import run.halo.app.service.support.HaloMediaType; import run.halo.app.utils.FileUtils; import run.halo.app.utils.FilenameUtils; import run.halo.app.utils.HaloUtils; -import run.halo.app.utils.JsonUtils; import java.io.ByteArrayInputStream; import java.io.File; @@ -122,12 +121,13 @@ public class ThemeServiceImpl implements ThemeService { @Override public Set getThemes() { - Optional themeCacheString = cacheStore.get(THEMES_CACHE_KEY); + + Optional themePropertiesOptional = cacheStore.getAny(THEMES_CACHE_KEY, ThemeProperty[].class); try { - if (themeCacheString.isPresent()) { + if (themePropertiesOptional.isPresent()) { // Convert to theme properties - ThemeProperty[] themeProperties = JsonUtils.jsonToObject(themeCacheString.get(), ThemeProperty[].class); + ThemeProperty[] themeProperties = themePropertiesOptional.get(); return new HashSet<>(Arrays.asList(themeProperties)); } @@ -142,7 +142,7 @@ public class ThemeServiceImpl implements ThemeService { Set themes = themePaths.stream().map(this::getProperty).collect(Collectors.toSet()); // Cache the themes - cacheStore.put(THEMES_CACHE_KEY, JsonUtils.objectToJson(themes)); + cacheStore.putAny(THEMES_CACHE_KEY, themes); return themes; } catch (IOException e) { diff --git a/src/main/java/run/halo/app/service/impl/UserServiceImpl.java b/src/main/java/run/halo/app/service/impl/UserServiceImpl.java index cd694fad2..201a228d2 100644 --- a/src/main/java/run/halo/app/service/impl/UserServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/UserServiceImpl.java @@ -128,7 +128,7 @@ public class UserServiceImpl extends AbstractCrudService implemen if (!BCrypt.checkpw(password, user.getPassword())) { // If the password is mismatch // Add login failure count - Integer loginFailureCount = stringCacheStore.get(LOGIN_FAILURE_COUNT_KEY).map(Integer::valueOf).orElse(0); + Integer loginFailureCount = stringCacheStore.getAny(LOGIN_FAILURE_COUNT_KEY, Integer.class).orElse(0); if (loginFailureCount >= MAX_LOGIN_TRY - 1) { // Set expiration @@ -139,7 +139,7 @@ public class UserServiceImpl extends AbstractCrudService implemen loginFailureCount++; - stringCacheStore.put(LOGIN_FAILURE_COUNT_KEY, loginFailureCount.toString(), LOCK_MINUTES, TimeUnit.MINUTES); + stringCacheStore.putAny(LOGIN_FAILURE_COUNT_KEY, loginFailureCount, LOCK_MINUTES, TimeUnit.MINUTES); int remainder = MAX_LOGIN_TRY - loginFailureCount;