初始化
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package tech.easyflow.auth.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
|
||||
@AutoConfiguration
|
||||
public class AuthModuleConfig {
|
||||
|
||||
public AuthModuleConfig() {
|
||||
System.out.println("启用模块 >>>>>>>>>> module-auth");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package tech.easyflow.auth.config;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import tech.easyflow.common.annotation.UsePermission;
|
||||
|
||||
public class CurdInterceptor implements HandlerInterceptor {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CurdInterceptor.class);
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
String requestURI = request.getRequestURI();
|
||||
|
||||
log.info("进入 CurdInterceptor requestURI:{}", requestURI);
|
||||
|
||||
String groupName = "";
|
||||
// 检查handler是否是HandlerMethod类型
|
||||
if (handler instanceof HandlerMethod) {
|
||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||
|
||||
|
||||
SaIgnore saIgnoreAnnotation = handlerMethod.getBeanType().getAnnotation(SaIgnore.class);
|
||||
SaIgnore saIgnoreAnnotationMethod = handlerMethod.getMethodAnnotation(SaIgnore.class);
|
||||
if (saIgnoreAnnotation != null || saIgnoreAnnotationMethod != null) {
|
||||
log.info("{}-----------------> 放行: saIgnoreAnnotation:{}", requestURI,saIgnoreAnnotation);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 获取类上的特定注解
|
||||
UsePermission classAnnotation = handlerMethod.getBeanType().getAnnotation(UsePermission.class);
|
||||
if (classAnnotation != null) {
|
||||
// 处理注解逻辑
|
||||
groupName = classAnnotation.moduleName();
|
||||
}
|
||||
// 有此注解,交给 sa token 自行判断
|
||||
SaCheckPermission saCheckPermission = handlerMethod.getMethodAnnotation(SaCheckPermission.class);
|
||||
if (saCheckPermission != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
String requestUri = request.getRequestURI();
|
||||
// 查询
|
||||
String finalGroupName = groupName;
|
||||
SaRouter.match("/**/list",
|
||||
"/**/page",
|
||||
"/**/detail",
|
||||
"/**/intelligentFilling"
|
||||
).check(r -> {
|
||||
checkBaseCurd(requestUri, finalGroupName, "query");
|
||||
});
|
||||
// 保存
|
||||
SaRouter.match("/**/save",
|
||||
"/**/update"
|
||||
).check(r -> {
|
||||
checkBaseCurd(requestUri, finalGroupName, "save");
|
||||
});
|
||||
// 删除
|
||||
SaRouter.match("/**/remove",
|
||||
"/**/removeBatch"
|
||||
).check(r -> {
|
||||
checkBaseCurd(requestUri, finalGroupName, "remove");
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void checkBaseCurd(String uri, String groupName, String permission) {
|
||||
int idx = uri.lastIndexOf("/");
|
||||
String per = uri.substring(0,idx + 1) + permission;
|
||||
if (StrUtil.isNotEmpty(groupName)) {
|
||||
// 如果指定了继承的模块,就改为该模块的权限校验
|
||||
per = groupName + "/" + permission;
|
||||
}
|
||||
StpUtil.checkPermission(per);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package tech.easyflow.auth.config;
|
||||
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Configuration
|
||||
public class LoginAutoConfig implements WebMvcConfigurer {
|
||||
|
||||
@Resource
|
||||
private LoginProperties properties;
|
||||
|
||||
@Resource
|
||||
private NeedApiKeyInterceptor needApiKeyInterceptor;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
SaInterceptor saInterceptor = new SaInterceptor(handle -> {
|
||||
StpUtil.checkLogin();
|
||||
});
|
||||
registry.addInterceptor(new CurdInterceptor())
|
||||
.order(101)
|
||||
.addPathPatterns("/**");
|
||||
registry.addInterceptor(saInterceptor)
|
||||
.order(100)
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns("/")
|
||||
.excludePathPatterns("/error")
|
||||
.excludePathPatterns("/attachment/**")
|
||||
.excludePathPatterns("/api/v1/public/*")
|
||||
.excludePathPatterns("/api/v1/account/login")
|
||||
.excludePathPatterns("/api/v1/account/register")
|
||||
.excludePathPatterns("/thirdAuth/**")
|
||||
.excludePathPatterns("/public-api/**")
|
||||
.excludePathPatterns(properties.getExcludesOrEmpty());
|
||||
registry.addInterceptor(needApiKeyInterceptor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package tech.easyflow.auth.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "easyflow.login")
|
||||
public class LoginProperties {
|
||||
|
||||
private String[] excludes;
|
||||
|
||||
public String[] getExcludes() {
|
||||
return excludes;
|
||||
}
|
||||
|
||||
public String[] getExcludesOrEmpty() {
|
||||
return excludes != null ? excludes : new String[0];
|
||||
}
|
||||
|
||||
public void setExcludes(String[] excludes) {
|
||||
this.excludes = excludes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package tech.easyflow.auth.config;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import tech.easyflow.common.annotation.NeedApiKeyAccess;
|
||||
import tech.easyflow.common.util.StringUtil;
|
||||
import tech.easyflow.common.web.exceptions.BusinessException;
|
||||
import tech.easyflow.system.service.SysApiKeyService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Component
|
||||
public class NeedApiKeyInterceptor implements HandlerInterceptor {
|
||||
@Resource
|
||||
private SysApiKeyService sysApiKeyService;
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String requestURI = request.getRequestURI();
|
||||
if ( handler instanceof HandlerMethod) {
|
||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||
NeedApiKeyAccess needApiKeyAccess = handlerMethod.getMethodAnnotation(NeedApiKeyAccess.class);
|
||||
if (needApiKeyAccess != null) {
|
||||
String apiKey = request.getHeader("Authorization");
|
||||
if (StringUtil.noText(apiKey)) {
|
||||
throw new BusinessException("请传入apiKey");
|
||||
}
|
||||
sysApiKeyService.checkApikeyPermission(apiKey, requestURI);
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user