mirror of https://github.com/halo-dev/halo
Restructure api interceptor
parent
4ec6604dcb
commit
e654e96db6
|
@ -30,6 +30,8 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cc.ryanc.halo.model.support.HaloConst.HALO_VERSION;
|
||||||
|
import static cc.ryanc.halo.model.support.HaloConst.TOKEN_HEADER;
|
||||||
import static springfox.documentation.schema.AlternateTypeRules.newRule;
|
import static springfox.documentation.schema.AlternateTypeRules.newRule;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,7 +103,7 @@ public class SwaggerConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiKey apiKeys() {
|
private ApiKey apiKeys() {
|
||||||
return new ApiKey("TOKEN ACCESS", HttpHeaders.AUTHORIZATION, In.HEADER.name());
|
return new ApiKey("TOKEN ACCESS", TOKEN_HEADER, In.HEADER.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
private SecurityContext securityContext() {
|
private SecurityContext securityContext() {
|
||||||
|
@ -121,7 +123,7 @@ public class SwaggerConfiguration {
|
||||||
return new ApiInfoBuilder()
|
return new ApiInfoBuilder()
|
||||||
.title("Halo API Documentation")
|
.title("Halo API Documentation")
|
||||||
.description("Documentation for Halo API")
|
.description("Documentation for Halo API")
|
||||||
.version("v0.4.2")
|
.version(HALO_VERSION)
|
||||||
.termsOfServiceUrl("https://ryanc.cc/")
|
.termsOfServiceUrl("https://ryanc.cc/")
|
||||||
.contact(new Contact("RYAN0UP", "https://ryanc.cc/", "i#ryanc.cc"))
|
.contact(new Contact("RYAN0UP", "https://ryanc.cc/", "i#ryanc.cc"))
|
||||||
.build();
|
.build();
|
||||||
|
|
|
@ -20,6 +20,11 @@ public class HaloConst {
|
||||||
*/
|
*/
|
||||||
public static final String HALO_VERSION = "0.4.2";
|
public static final String HALO_VERSION = "0.4.2";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token key
|
||||||
|
*/
|
||||||
|
public static final String TOKEN_HEADER = "token";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 所有设置选项(key,value)
|
* 所有设置选项(key,value)
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package cc.ryanc.halo.web.interceptor;
|
package cc.ryanc.halo.web.interceptor;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.support.JsonResult;
|
|
||||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
||||||
|
import cc.ryanc.halo.model.support.JsonResult;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
@ -16,6 +16,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
|
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
|
||||||
|
import static cc.ryanc.halo.model.support.HaloConst.TOKEN_HEADER;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
|
@ -28,8 +29,6 @@ import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
|
||||||
@Component
|
@Component
|
||||||
public class ApiInterceptor implements HandlerInterceptor {
|
public class ApiInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
private static final String TOKEN = "token";
|
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
public ApiInterceptor(ObjectMapper objectMapper) {
|
public ApiInterceptor(ObjectMapper objectMapper) {
|
||||||
|
@ -38,19 +37,20 @@ public class ApiInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
if (StrUtil.equals(TrueFalseEnum.TRUE.getDesc(), OPTIONS.get(BlogPropertiesEnum.API_STATUS.getProp()))) {
|
if (!StrUtil.equals(TrueFalseEnum.TRUE.getDesc(), OPTIONS.get(BlogPropertiesEnum.API_STATUS.getProp()))) {
|
||||||
if (StrUtil.equals(request.getHeader(TOKEN), OPTIONS.get(BlogPropertiesEnum.API_TOKEN.getProp()))) {
|
response.sendRedirect("/404");
|
||||||
return true;
|
return false;
|
||||||
} else {
|
|
||||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
||||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
||||||
JsonResult result = new JsonResult(HttpStatus.BAD_REQUEST.value(), "Invalid Token");
|
|
||||||
response.getWriter().write(objectMapper.writeValueAsString(result));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
response.sendRedirect("/404");
|
|
||||||
return false;
|
if (!StrUtil.equals(request.getHeader(TOKEN_HEADER), OPTIONS.get(BlogPropertiesEnum.API_TOKEN.getProp()))) {
|
||||||
|
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
|
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||||
|
JsonResult result = new JsonResult(HttpStatus.BAD_REQUEST.value(), "Invalid Token");
|
||||||
|
response.getWriter().write(objectMapper.writeValueAsString(result));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue