Refactor halo mode (#571)

pull/569/head
John Niang 2020-02-14 20:57:41 +08:00 committed by GitHub
parent baa2f1ecfd
commit df3601a751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 36 additions and 46 deletions

View File

@ -3,7 +3,6 @@ package run.halo.app.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
@ -20,7 +19,6 @@ import run.halo.app.cache.StringCacheStore;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.filter.CorsFilter;
import run.halo.app.filter.LogFilter;
import run.halo.app.model.enums.Mode;
import run.halo.app.security.filter.AdminAuthenticationFilter;
import run.halo.app.security.filter.ApiAuthenticationFilter;
import run.halo.app.security.filter.ContentFilter;
@ -120,9 +118,8 @@ public class HaloConfiguration {
public FilterRegistrationBean<ContentFilter> contentFilter(HaloProperties haloProperties,
OptionService optionService,
StringCacheStore cacheStore,
OneTimeTokenService oneTimeTokenService,
@Value("${spring.profiles.active:prod}") String activeProfile) {
ContentFilter contentFilter = new ContentFilter(haloProperties, optionService, cacheStore, oneTimeTokenService, Mode.valueFrom(activeProfile));
OneTimeTokenService oneTimeTokenService) {
ContentFilter contentFilter = new ContentFilter(haloProperties, optionService, cacheStore, oneTimeTokenService);
contentFilter.setFailureHandler(new ContentAuthenticationFailureHandler());
String adminPattern = HaloUtils.ensureBoth(haloProperties.getAdminPath(), "/") + "**";
@ -148,9 +145,8 @@ public class HaloConfiguration {
ObjectMapper objectMapper,
OptionService optionService,
StringCacheStore cacheStore,
OneTimeTokenService oneTimeTokenService,
@Value("${spring.profiles.active:prod}") String activeProfile) {
ApiAuthenticationFilter apiFilter = new ApiAuthenticationFilter(haloProperties, optionService, cacheStore, oneTimeTokenService, Mode.valueFrom(activeProfile));
OneTimeTokenService oneTimeTokenService) {
ApiAuthenticationFilter apiFilter = new ApiAuthenticationFilter(haloProperties, optionService, cacheStore, oneTimeTokenService);
apiFilter.addExcludeUrlPatterns(
"/api/content/*/comments",
"/api/content/**/comments/**",
@ -178,10 +174,9 @@ public class HaloConfiguration {
HaloProperties haloProperties,
ObjectMapper objectMapper,
OptionService optionService,
OneTimeTokenService oneTimeTokenService,
@Value("${spring.profiles.active:prod}") String activeProfile) {
OneTimeTokenService oneTimeTokenService) {
AdminAuthenticationFilter adminAuthenticationFilter = new AdminAuthenticationFilter(cacheStore, userService,
haloProperties, optionService, oneTimeTokenService, Mode.valueFrom(activeProfile));
haloProperties, optionService, oneTimeTokenService);
DefaultAuthenticationFailureHandler failureHandler = new DefaultAuthenticationFailureHandler();
failureHandler.setProductionEnv(haloProperties.isProductionEnv());

View File

@ -2,10 +2,8 @@ package run.halo.app.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import run.halo.app.model.enums.Mode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import static run.halo.app.model.support.HaloConst.*;
@ -36,6 +34,11 @@ public class HaloProperties {
*/
private boolean authEnabled = true;
/**
* Halo startup mode.
*/
private Mode mode = Mode.PRODUCTION;
/**
* Admin path.
*/

View File

@ -2,7 +2,6 @@ package run.halo.app.model.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
/**
@ -24,15 +23,17 @@ public enum Mode {
*/
@JsonCreator
public static Mode valueFrom(@Nullable String value) {
if (StringUtils.equalsIgnoreCase("dev", value)) {
return Mode.DEVELOPMENT;
Mode modeResult = null;
for (Mode mode : values()) {
if (mode.name().equalsIgnoreCase(value)) {
modeResult = mode;
break;
}
}
if (StringUtils.equalsIgnoreCase("test", value)) {
return Mode.TEST;
if (modeResult == null) {
modeResult = PRODUCTION;
}
return PRODUCTION;
return modeResult;
}
@JsonValue

View File

@ -53,8 +53,6 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
private OneTimeTokenService oneTimeTokenService;
private final Mode mode;
private volatile AuthenticationFailureHandler failureHandler;
/**
* Exclude url patterns.
@ -64,13 +62,11 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
AbstractAuthenticationFilter(HaloProperties haloProperties,
OptionService optionService,
StringCacheStore cacheStore,
OneTimeTokenService oneTimeTokenService,
Mode mode) {
OneTimeTokenService oneTimeTokenService) {
this.haloProperties = haloProperties;
this.optionService = optionService;
this.cacheStore = cacheStore;
this.oneTimeTokenService = oneTimeTokenService;
this.mode = mode;
antPathMatcher = new AntPathMatcher();
}
@ -162,7 +158,7 @@ public abstract class AbstractAuthenticationFilter extends OncePerRequestFilter
// Check whether the blog is installed or not
Boolean isInstalled = optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);
if (!isInstalled && mode != Mode.TEST) {
if (!isInstalled && Mode.TEST.equals(haloProperties.getMode())) {
// If not installed
getFailureHandler().onFailure(request, response, new NotInstallException("当前博客还没有初始化"));
return;

View File

@ -7,7 +7,6 @@ import run.halo.app.cache.StringCacheStore;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.exception.AuthenticationException;
import run.halo.app.model.entity.User;
import run.halo.app.model.enums.Mode;
import run.halo.app.security.authentication.AuthenticationImpl;
import run.halo.app.security.context.SecurityContextHolder;
import run.halo.app.security.context.SecurityContextImpl;
@ -43,9 +42,8 @@ public class AdminAuthenticationFilter extends AbstractAuthenticationFilter {
UserService userService,
HaloProperties haloProperties,
OptionService optionService,
OneTimeTokenService oneTimeTokenService,
Mode mode) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService, mode);
OneTimeTokenService oneTimeTokenService) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService);
this.userService = userService;
this.haloProperties = haloProperties;
}

View File

@ -7,7 +7,6 @@ import run.halo.app.cache.StringCacheStore;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.exception.AuthenticationException;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.enums.Mode;
import run.halo.app.model.properties.ApiProperties;
import run.halo.app.model.properties.CommentProperties;
import run.halo.app.security.service.OneTimeTokenService;
@ -36,9 +35,8 @@ public class ApiAuthenticationFilter extends AbstractAuthenticationFilter {
public ApiAuthenticationFilter(HaloProperties haloProperties,
OptionService optionService,
StringCacheStore cacheStore,
OneTimeTokenService oneTimeTokenService,
Mode mode) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService, mode);
OneTimeTokenService oneTimeTokenService) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService);
this.optionService = optionService;
}

View File

@ -2,7 +2,6 @@ package run.halo.app.security.filter;
import run.halo.app.cache.StringCacheStore;
import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.enums.Mode;
import run.halo.app.security.service.OneTimeTokenService;
import run.halo.app.service.OptionService;
@ -23,9 +22,8 @@ public class ContentFilter extends AbstractAuthenticationFilter {
public ContentFilter(HaloProperties haloProperties,
OptionService optionService,
StringCacheStore cacheStore,
OneTimeTokenService oneTimeTokenService,
Mode mode) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService, mode);
OneTimeTokenService oneTimeTokenService) {
super(haloProperties, optionService, cacheStore, oneTimeTokenService);
}
@Override

View File

@ -23,7 +23,6 @@ import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.entity.User;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.Mode;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.LoginParam;
import run.halo.app.model.params.ResetPasswordParam;
@ -300,7 +299,7 @@ public class AdminServiceImpl implements AdminService {
environmentDTO.setVersion(HaloConst.HALO_VERSION);
environmentDTO.setMode(Mode.valueFrom(this.mode));
environmentDTO.setMode(haloProperties.getMode());
return environmentDTO;
}

View File

@ -19,10 +19,10 @@ spring:
password: 123456
# MySQL database configuration.
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/halo_dev?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
# username: root
# password: 123456
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://127.0.0.1:3306/halo_dev?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
# username: root
# password: 123456
h2:
console:
@ -62,5 +62,6 @@ halo:
doc-disabled: false
production-env: false
auth-enabled: true
mode: development
workDir: ${user.home}/halo-dev/
cache: level

View File

@ -60,4 +60,5 @@ logging:
halo:
doc-disabled: false
auth-enable: false
mode: test
workDir: ${user.home}/halo-test/