BIToken认证修改
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.mhd.bi.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* BI 开放接口访问控制(请求头 {@code X-BI-Access-Token})。
|
||||
* <p>配置了 {@code accessToken} 时必须与请求头完全一致;未配置时仅校验请求头非空。</p>
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "mhd.bi")
|
||||
public class BiAccessProperties {
|
||||
|
||||
/**
|
||||
* 与 X-BI-Access-Token 比对的密钥;为空则只要请求头非空即可
|
||||
*/
|
||||
private String accessToken = "";
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.mhd.bi.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* 注册 /biReport 路径的访问令牌校验
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(BiAccessProperties.class)
|
||||
public class BiReportAccessConfiguration {
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<BiReportAccessTokenFilter> biReportAccessTokenFilter(BiAccessProperties properties) {
|
||||
FilterRegistrationBean<BiReportAccessTokenFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(new BiReportAccessTokenFilter(properties));
|
||||
registration.addUrlPatterns("/biReport/*");
|
||||
registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 100);
|
||||
registration.setName("biReportAccessTokenFilter");
|
||||
return registration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.mhd.bi.config;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 限制 /biReport 下接口必须携带 {@code X-BI-Access-Token},否则 401。
|
||||
*/
|
||||
public class BiReportAccessTokenFilter extends OncePerRequestFilter {
|
||||
|
||||
private static final String UNAUTHORIZED_MSG = "未认证,请提供有效的访问Token";
|
||||
|
||||
public static final String HEADER_X_BI_ACCESS_TOKEN = "X-BI-Access-Token";
|
||||
|
||||
private final BiAccessProperties accessProperties;
|
||||
|
||||
public BiReportAccessTokenFilter(BiAccessProperties accessProperties) {
|
||||
this.accessProperties = accessProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(@NonNull HttpServletRequest request,
|
||||
@NonNull HttpServletResponse response,
|
||||
@NonNull FilterChain filterChain) throws ServletException, IOException {
|
||||
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
String presented = request.getHeader(HEADER_X_BI_ACCESS_TOKEN);
|
||||
if (!StringUtils.hasText(presented)) {
|
||||
writeUnauthorized(response);
|
||||
return;
|
||||
}
|
||||
|
||||
String expected = accessProperties.getAccessToken();
|
||||
if (StringUtils.hasText(expected) && !expected.equals(presented)) {
|
||||
writeUnauthorized(response);
|
||||
return;
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
private static void writeUnauthorized(HttpServletResponse response) throws IOException {
|
||||
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
String body = "{\"code\":401,\"msg\":\"" + UNAUTHORIZED_MSG.replace("\"", "\\\"") + "\"}";
|
||||
response.getOutputStream().write(body.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.mhd.bi.domain.biReport.support;
|
||||
|
||||
/**
|
||||
* BI 快照表 {@code DEL_FLAG} 取值(三张 *\_SNAPSHOT 表一致)。
|
||||
* <p>查询接口仅返回 {@link #NORMAL}。</p>
|
||||
*/
|
||||
public final class BiSnapshotDelFlag {
|
||||
|
||||
/** 无状态 */
|
||||
public static final int NONE = 0;
|
||||
/** 正常(接口查询可见) */
|
||||
public static final int NORMAL = 1;
|
||||
/** 已删除(逻辑删除,接口不返回) */
|
||||
public static final int DELETED = 2;
|
||||
|
||||
private BiSnapshotDelFlag() {
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
/**
|
||||
* BI 报表接口
|
||||
*/
|
||||
@Api(tags = "BI报表")
|
||||
@Api(tags = "BI报表", description = "须在请求头携带 X-BI-Access-Token,与配置 mhd.bi.access-token 一致")
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/biReport")
|
||||
|
||||
Reference in New Issue
Block a user