[*] 日常优化

This commit is contained in:
acgist
2023-06-13 08:10:37 +08:00
parent c35869df0c
commit ebda8b0bbd
11 changed files with 159 additions and 114 deletions

View File

@@ -40,7 +40,10 @@ public class TaoyaoAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "taoyao.security", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingBean
public SecurityInterceptor securityInterceptor(SecurityService securityService, SecurityProperties securityProperties) {
public SecurityInterceptor securityInterceptor(
SecurityService securityService,
SecurityProperties securityProperties
) {
return new SecurityInterceptor(securityService, securityProperties);
}

View File

@@ -27,16 +27,22 @@ public class SecurityInterceptor extends InterceptorAdapter {
private final SecurityService securityService;
private final SecurityProperties securityProperties;
public SecurityInterceptor(SecurityService securityService, SecurityProperties securityProperties) {
this.securityService = securityService;
this.securityProperties = securityProperties;
log.info("安全拦截密码:{}", securityProperties.getPassword());
}
/**
/**
* 鉴权信息
*/
private final String authenticate;
/**
* 地址匹配
*/
private final AntPathMatcher matcher = new AntPathMatcher();
private final AntPathMatcher matcher;
public SecurityInterceptor(SecurityService securityService, SecurityProperties securityProperties) {
this.securityService = securityService;
this.securityProperties = securityProperties;
this.authenticate = "Basic Realm=\"" + this.securityProperties.getRealm() + "\"";
this.matcher = new AntPathMatcher();
log.info("安全拦截密码:{}", securityProperties.getPassword());
}
@Override
public String name() {
@@ -62,7 +68,7 @@ public class SecurityInterceptor extends InterceptorAdapter {
log.debug("授权失败:{}", request.getRequestURL());
}
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic Realm=\"" + this.securityProperties.getRealm() + "\"");
response.setHeader(HttpHeaders.WWW_AUTHENTICATE, this.authenticate);
return false;
}
@@ -72,7 +78,7 @@ public class SecurityInterceptor extends InterceptorAdapter {
* @return 是否许可请求
*/
private boolean permit(HttpServletRequest request) {
final String uri = request.getRequestURI();
final String uri = request.getRequestURI();
final String[] permit = this.securityProperties.getPermit();
if(ArrayUtils.isEmpty(permit)) {
return false;

View File

@@ -17,14 +17,15 @@ public class SlowInterceptor extends InterceptorAdapter {
private final TaoyaoProperties taoyaoProperties;
public SlowInterceptor(TaoyaoProperties taoyaoProperties) {
this.taoyaoProperties = taoyaoProperties;
}
/**
/**
* 请求开始时间
*/
private final ThreadLocal<Long> local = new ThreadLocal<>();
private final ThreadLocal<Long> local;
public SlowInterceptor(TaoyaoProperties taoyaoProperties) {
this.taoyaoProperties = taoyaoProperties;
this.local = new ThreadLocal<>();
}
@Override
public String name() {
@@ -52,7 +53,7 @@ public class SlowInterceptor extends InterceptorAdapter {
final long duration;
final Long last = this.local.get();
if(last != null && (duration = System.currentTimeMillis() - last) > this.taoyaoProperties.getTimeout()) {
log.info("请求执行时间过慢:{}-{}", request.getRequestURI(), duration);
log.info("请求执行时间过慢:{} - {}", request.getRequestURI(), duration);
}
this.local.remove();
}