sk-matrix-service 项目 模块调整

This commit is contained in:
Shikong 2023-05-07 22:50:25 +08:00
parent 0b0da1c130
commit 01433dea8f
23 changed files with 267 additions and 2 deletions

View File

@ -19,10 +19,12 @@
<entry name="D:/Repository/maven/org/springframework/boot/spring-boot-configuration-processor/3.0.1/spring-boot-configuration-processor-3.0.1.jar" />
</processorPath>
<module name="annotation" />
<module name="orm" />
<module name="api" />
<module name="common" />
<module name="starter" />
<module name="auth" />
<module name="orm" />
<module name="services" />
<module name="api" />
<module name="casbin" />
</profile>
</annotationProcessing>
@ -31,9 +33,11 @@
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="annotation" options="-parameters" />
<module name="api" options="-parameters" />
<module name="auth" options="-parameters" />
<module name="casbin" options="-parameters" />
<module name="common" options="-parameters" />
<module name="orm" options="-parameters" />
<module name="services" options="-parameters" />
<module name="sk-matrix-service" options="-parameters" />
<module name="starter" options="-parameters" />
</option>

View File

@ -4,10 +4,12 @@
<file url="file://$PROJECT_DIR$/annotation/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/annotation/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/api/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/auth/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/casbin/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/common/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/orm/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/services/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/starter/src/main/java" charset="UTF-8" />

View File

@ -24,6 +24,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>auth</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>common</artifactId>

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>sk-matrix-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>auth</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>annotation</artifactId>
<version>${project.version}</version>
</dependency>
<!--编译时创建候选对象的静态列表来提高大型应用程序的启动性能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package cn.skcks.matrix.v2.autoconfigure;
import cn.skcks.matrix.v2.interceptor.AuthorizationInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({
AuthorizationInterceptor.class,
})
public class AuthAutoConfiguration {
}

View File

@ -0,0 +1,8 @@
package cn.skcks.matrix.v2.handler;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public interface AuthHandler {
boolean auth(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
}

View File

@ -0,0 +1,79 @@
package cn.skcks.matrix.v2.interceptor;
import cn.skcks.matrix.annotation.web.auth.Auth;
import cn.skcks.matrix.annotation.web.auth.UnAuth;
import cn.skcks.matrix.handler.AuthHandler;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
@SuppressWarnings({"unused"})
@ConditionalOnClass({Auth.class, UnAuth.class})
public class AuthorizationInterceptor implements HandlerInterceptor {
private final List<AuthHandler> authHandlers = new ArrayList<>();
public List<AuthHandler> getAuthHandlers() {
return authHandlers;
}
public void addAuthHandler(AuthHandler authHandler) {
this.authHandlers.add(authHandler);
}
@Override
public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) throws Exception {
log.info("[请求检测] 请求类型 => {}, 请求地址 => {}, {}", request.getMethod(), request.getRequestURI(), handler);
if (!(handler instanceof HandlerMethod handlerMethod)) {
return true;
} else if (response.getStatus() != HttpServletResponse.SC_OK) {
return true;
}
Method method = handlerMethod.getMethod();
Class<?> clz = handlerMethod.getBeanType();
if (method.isAnnotationPresent(Auth.class) || method.isAnnotationPresent(UnAuth.class) ||
clz.isAnnotationPresent(Auth.class) || clz.isAnnotationPresent(UnAuth.class)) {
if (method.isAnnotationPresent(UnAuth.class) || clz.isAnnotationPresent(UnAuth.class)) {
UnAuth unAuth = method.isAnnotationPresent(UnAuth.class) ?
method.getAnnotation(UnAuth.class) :
handlerMethod.getBeanType().getAnnotation(UnAuth.class);
if (!unAuth.required()) {
return executeAuthHandlers(request, response, handler);
}
} else if (method.isAnnotationPresent(Auth.class) || clz.isAnnotationPresent(Auth.class)) {
Auth auth = method.isAnnotationPresent(Auth.class) ?
method.getAnnotation(Auth.class) :
handlerMethod.getBeanType().getAnnotation(Auth.class);
if (auth.required()) {
return executeAuthHandlers(request, response, handler);
}
}
}
log.info("[权限检测] 无需权限认证 直接放行 {}", handler);
return true;
}
private boolean executeAuthHandlers(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
for (AuthHandler authHandler : authHandlers) {
if(!authHandler.auth(request,response,handler)){
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.skcks.matrix.v2.autoconfigure.AuthAutoConfiguration

View File

@ -0,0 +1 @@
cn.skcks.matrix.v2.autoconfigure.AuthAutoConfiguration

View File

@ -23,6 +23,8 @@
<module>orm</module>
<module>casbin</module>
<module>api</module>
<module>auth</module>
<module>services</module>
</modules>
<properties>

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>sk-matrix-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>services</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>orm</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cn.skcks.matrix.v2</groupId>
<artifactId>auth</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!--编译时创建候选对象的静态列表来提高大型应用程序的启动性能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,7 @@
package cn.skcks.matrix.v2;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}