submitToken 注解处理

This commit is contained in:
2024-03-10 10:20:49 +08:00
parent 5b77a5828d
commit 7d87742aec
3 changed files with 29 additions and 7 deletions

View File

@@ -0,0 +1,13 @@
package cn.lihongjie.coal.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface SubmitToken {
boolean value() default true;
}

View File

@@ -81,6 +81,10 @@ public class ResourceEntity extends CommonEntity {
private Boolean rateLimit;
@Comment("是否校验提交token")
private Boolean submitToken;
private static String getParent(String path) {
log.debug("getParent {}", path);

View File

@@ -1,7 +1,9 @@
package cn.lihongjie.coal.resource.service;
import cn.lihongjie.coal.annotation.Anonymous;
import cn.lihongjie.coal.annotation.RateLimit;
import cn.lihongjie.coal.annotation.SignCheck;
import cn.lihongjie.coal.annotation.SubmitToken;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
@@ -202,13 +204,22 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
entity.setType("3");
entity.setName("");
entity.setUrl(allUrl._1);
entity.setAnonymous(StringUtils.equalsAny(allUrl._1, "/login", "/logout", "/genCaptcha"));
Anonymous anonymous = allUrl._2.getMethodAnnotation(Anonymous.class);
entity.setAnonymous(
StringUtils.equalsAny(allUrl._1, "/login", "/logout", "/genCaptcha")
|| (anonymous != null && anonymous.value()));
RateLimit rateLimit = allUrl._2.getMethodAnnotation(RateLimit.class);
entity.setRateLimit(rateLimit == null || rateLimit.value());
SignCheck signCheck = allUrl._2.getMethodAnnotation(SignCheck.class);
entity.setSignCheck(signCheck == null || signCheck.value());
SubmitToken submitToken = allUrl._2.getMethodAnnotation(SubmitToken.class);
entity.setSubmitToken(submitToken == null || submitToken.value());
root.addChildren(entity);
}
@@ -249,18 +260,12 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
.findAny();
}
@Cacheable(cacheNames = Constants.CACHE_RESOURCE_BY_URL_2, key = "#url")
public Optional<ResourceDto> findUrlFromCache2(String url) {
return Optional.ofNullable(repository.findByCodeAndType(url, "3"));
}
public void clearCache() {
cacheManager.getCache(Constants.CACHE_RESOURCE).clear();