Skip to content

Commit d2151bc

Browse files
authored
Merge pull request #24 from opensabre/0.1.1
0.1.1
2 parents a794b91 + 6fdca42 commit d2151bc

9 files changed

Lines changed: 300 additions & 15 deletions

File tree

opensabre-base-dependencies/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<hutool.version>5.8.35</hutool.version>
4545
<guava.version>33.4.0-jre</guava.version>
4646
<jasypt-springboot.version>3.0.5</jasypt-springboot.version>
47+
<jib-maven-plugin.version>3.4.6</jib-maven-plugin.version>
4748
<!-- 测试 -->
4849
<junit-jupiter.version>5.11.4</junit-jupiter.version>
4950
<httpclient5.version>5.3.1</httpclient5.version>
@@ -276,6 +277,32 @@
276277
<dockerfile>src/main/docker/Dockerfile</dockerfile>
277278
</configuration>
278279
</plugin>
280+
<!-- Jib Maven插件 -->
281+
<plugin>
282+
<groupId>com.google.cloud.tools</groupId>
283+
<artifactId>jib-maven-plugin</artifactId>
284+
<version>${jib-maven-plugin.version}</version>
285+
<configuration>
286+
<!-- 基础镜像 -->
287+
<from>
288+
<image>docker.1ms.run/eclipse-temurin:21-jre-alpine</image>
289+
</from>
290+
<!-- 目标镜像 -->
291+
<to>
292+
<image>opensabre/${project.artifactId}:${project.version}</image>
293+
</to>
294+
<!-- 容器配置 -->
295+
<container>
296+
<user>1001</user>
297+
<workingDirectory>/app</workingDirectory>
298+
<ports>
299+
<port>8000</port>
300+
</ports>
301+
<jvmFlags>-XX:+UseContainerSupport</jvmFlags>
302+
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
303+
</container>
304+
</configuration>
305+
</plugin>
279306
<!--测试案例-->
280307
<plugin>
281308
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->

opensabre-starter-boot/src/main/java/io/github/opensabre/boot/config/OpensabreSwaggerConfig.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.github.opensabre.boot.config;
22

33
import io.github.opensabre.boot.entity.SwaggerInfo;
4+
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
5+
import io.swagger.v3.oas.annotations.security.*;
46
import io.swagger.v3.oas.models.ExternalDocumentation;
57
import io.swagger.v3.oas.models.OpenAPI;
68
import io.swagger.v3.oas.models.info.Info;
79
import io.swagger.v3.oas.models.info.License;
810
import org.springframework.boot.autoconfigure.AutoConfiguration;
9-
import org.springframework.boot.context.properties.ConfigurationProperties;
1011
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1112
import org.springframework.context.annotation.Bean;
1213

@@ -17,6 +18,28 @@
1718
*/
1819
@AutoConfiguration
1920
@EnableConfigurationProperties(SwaggerInfo.class)
21+
@SecuritySchemes({@SecurityScheme(
22+
// 指定 SecurityScheme 的名称(OpenAPIDefinition注解中的security属性中会引用该名称)
23+
name = "${custom.security.name}",
24+
// 指定认证类型为oauth2
25+
type = SecuritySchemeType.OAUTH2,
26+
// 设置认证流程
27+
flows = @OAuthFlows(
28+
// 设置授权码模式
29+
authorizationCode = @OAuthFlow(
30+
// 获取token地址
31+
tokenUrl = "${custom.security.token-url}",
32+
// 授权申请地址
33+
authorizationUrl = "${custom.security.authorization-url}",
34+
// oauth2的申请的scope(需要在OAuth2客户端中存在)
35+
scopes = {
36+
@OAuthScope(name = "openid", description = "OpenId登录"),
37+
@OAuthScope(name = "profile", description = "获取用户信息"),
38+
@OAuthScope(name = "message.read", description = "读"),
39+
@OAuthScope(name = "message.write", description = "写")
40+
})
41+
)
42+
)})
2043
public class OpensabreSwaggerConfig {
2144

2245
@Resource
@@ -30,10 +53,12 @@ public class OpensabreSwaggerConfig {
3053
@Bean
3154
public OpenAPI openAPI() {
3255
return new OpenAPI()
33-
.info(new Info().title(swaggerInfo.getTitle())
56+
.info(new Info()
57+
.title(swaggerInfo.getTitle())
3458
.description(swaggerInfo.getDescription())
3559
.version(swaggerInfo.getVersion())
3660
.license(new License().name(swaggerInfo.getLicenseName()).url(swaggerInfo.getLicenseUrl())))
61+
// 外部文档
3762
.externalDocs(new ExternalDocumentation()
3863
.description(swaggerInfo.getWikiDocumentation())
3964
.url(swaggerInfo.getWikiUrl()));

opensabre-starter-webmvc/pom.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<artifactId>opensabre-framework</artifactId>
8+
<groupId>io.github.opensabre</groupId>
9+
<version>${revision}</version>
10+
</parent>
11+
12+
<artifactId>opensabre-starter-webmvc</artifactId>
13+
<packaging>jar</packaging>
14+
15+
<name>opensabre-starter-webmvc</name>
16+
<url>https://github.com/opensabre/</url>
17+
<description>OpenSabre Web project for Spring Boot</description>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<!-- 编译 -->
23+
<maven.compiler.source>17</maven.compiler.source>
24+
<maven.compiler.target>17</maven.compiler.target>
25+
<!-- 测试 -->
26+
<!-- 依赖 -->
27+
</properties>
28+
29+
<licenses>
30+
<license>
31+
<name>The Apache License, Version 2.0</name>
32+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
33+
</license>
34+
</licenses>
35+
36+
<developers>
37+
<developer>
38+
<name>zhoutaoo</name>
39+
<email>zhoutaoo@foxmail.com</email>
40+
<roles>
41+
<role>developer</role>
42+
</roles>
43+
<timezone>+8</timezone>
44+
</developer>
45+
</developers>
46+
47+
<scm>
48+
<tag>opensabre-web</tag>
49+
<url>https://github.com/opensabre/opensabre-framework/opensabre-web</url>
50+
<connection>scm:git:https://github.com/opensabre/opensabre-framework.git</connection>
51+
<developerConnection>scm:git:https://github.com/opensabre/opensabre-framework.git</developerConnection>
52+
</scm>
53+
54+
<dependencyManagement>
55+
<dependencies>
56+
<dependency>
57+
<groupId>io.github.opensabre</groupId>
58+
<artifactId>opensabre-base-dependencies</artifactId>
59+
<version>${revision}</version>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
</dependency>
63+
</dependencies>
64+
</dependencyManagement>
65+
66+
<dependencies>
67+
<dependency>
68+
<groupId>io.github.opensabre</groupId>
69+
<artifactId>opensabre-web</artifactId>
70+
</dependency>
71+
<!--spring web相关包-->
72+
<dependency>
73+
<groupId>org.springframework</groupId>
74+
<artifactId>spring-webmvc</artifactId>
75+
</dependency>
76+
<!--常用工具包-->
77+
<dependency>
78+
<groupId>org.apache.commons</groupId>
79+
<artifactId>commons-lang3</artifactId>
80+
</dependency>
81+
<!--常用json包-->
82+
<dependency>
83+
<groupId>com.fasterxml.jackson.core</groupId>
84+
<artifactId>jackson-annotations</artifactId>
85+
</dependency>
86+
<!--servlet开发相关-->
87+
<dependency>
88+
<groupId>jakarta.servlet</groupId>
89+
<artifactId>jakarta.servlet-api</artifactId>
90+
<scope>provided</scope>
91+
</dependency>
92+
<!--测试框架-->
93+
<dependency>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-starter-test</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
<!--junit单元测试-->
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
</dependencies>
105+
</project>

opensabre-starter-webmvc/readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
WEBMVC公共包
2+
----------
3+
4+
## 简介
5+
6+
主要封装spring-webmvc WEB开发用到的通用公共类、工具类,如公共web拦截器、web统一异常定义等。
7+
8+
## 使用
9+
10+
进入应用目录
11+
12+
安装命令:`mvn install`
13+
14+
## 使用指南
15+
16+
### 应用引入
17+
18+
需要将编译生成的jar包安装到本地maven类进入引用使用。
19+
20+
pom.xml
21+
22+
```
23+
<dependency>
24+
<groupId>io.github.opensabre</groupId>
25+
<artifactId>opensabre-starter-webmvc</artifactId>
26+
<version>0.1.1</version>
27+
</dependency>
28+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.opensabre.webmvc.exception;
2+
3+
import io.github.opensabre.common.core.entity.vo.Result;
4+
import io.github.opensabre.common.core.exception.SystemErrorType;
5+
import jakarta.servlet.ServletException;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.converter.HttpMessageNotReadableException;
10+
import org.springframework.web.HttpMediaTypeNotSupportedException;
11+
import org.springframework.web.HttpRequestMethodNotSupportedException;
12+
import org.springframework.web.bind.MethodArgumentNotValidException;
13+
import org.springframework.web.bind.MissingServletRequestParameterException;
14+
import org.springframework.web.bind.annotation.ExceptionHandler;
15+
import org.springframework.web.bind.annotation.ResponseStatus;
16+
import org.springframework.web.bind.annotation.RestControllerAdvice;
17+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
18+
import org.springframework.web.multipart.MultipartException;
19+
import org.springframework.web.servlet.NoHandlerFoundException;
20+
import org.springframework.web.servlet.resource.NoResourceFoundException;
21+
22+
/**
23+
* 默认全局异常处理类
24+
*/
25+
@Slf4j
26+
@Order
27+
@RestControllerAdvice
28+
public class DefaultWebMvcExceptionHandlerAdvice {
29+
30+
@ExceptionHandler(value = {MissingServletRequestParameterException.class})
31+
public Result<?> missingServletRequestParameterException(MissingServletRequestParameterException ex) {
32+
log.warn("missing servlet request parameter exception:{}", ex.getMessage());
33+
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID);
34+
}
35+
36+
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
37+
public Result<?> argumentInvalidException(MethodArgumentNotValidException ex) {
38+
log.warn("service exception:{}", ex.getMessage());
39+
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, ex.getBindingResult().getFieldError().getDefaultMessage());
40+
}
41+
42+
@ExceptionHandler(value = {HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class})
43+
public Result<?> httpMessageConvertException(HttpMessageNotReadableException ex) {
44+
log.warn("http message convert exception:{}", ex.getMessage());
45+
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, "数据解析错误:" + ex.getMessage());
46+
}
47+
48+
@ExceptionHandler(value = {MultipartException.class})
49+
public Result<?> uploadFileLimitException(MultipartException ex) {
50+
log.warn("upload file size limit:{}", ex.getMessage());
51+
return Result.fail(SystemErrorType.UPLOAD_FILE_SIZE_LIMIT);
52+
}
53+
54+
@ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
55+
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
56+
public Result<?> notSupportedMethodException(HttpRequestMethodNotSupportedException ex) {
57+
log.warn("http request method not supported exception {}", ex.getMessage());
58+
return Result.fail(SystemErrorType.METHOD_NOT_SUPPORTED);
59+
}
60+
61+
@ExceptionHandler(value = {NoHandlerFoundException.class, NoResourceFoundException.class})
62+
@ResponseStatus(HttpStatus.NOT_FOUND)
63+
public Result<?> noHandlerFoundException(ServletException ex) {
64+
log.warn("No static resource exception:{}", ex.getMessage());
65+
return Result.fail(SystemErrorType.RESOURCE_NOT_FOUND, ex.getMessage());
66+
}
67+
68+
@ExceptionHandler(value = {HttpMediaTypeNotSupportedException.class})
69+
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
70+
public Result<?> notSupportedMethodException(HttpMediaTypeNotSupportedException ex) {
71+
log.warn("http request media not supported exception {}", ex.getMessage());
72+
return Result.fail(SystemErrorType.METHOD_NOT_SUPPORTED);
73+
}
74+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.opensabre.webmvc.interceptor;
2+
3+
import io.github.opensabre.common.core.util.UserContextHolder;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.mock.web.MockHttpServletRequest;
6+
import org.springframework.mock.web.MockHttpServletResponse;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
public class UserInterceptorTest {
12+
@Test
13+
public void preHandle_当未设置token_user_那么正常处理下一个handle() throws Exception {
14+
UserInterceptor userInterceptor = new UserInterceptor();
15+
assertTrue(userInterceptor.preHandle(new MockHttpServletRequest(), new MockHttpServletResponse(), new Object()));
16+
}
17+
18+
@Test
19+
public void preHandle_当设置token的username_那么username可以在线程中拿出来用() throws Exception {
20+
UserInterceptor userInterceptor = new UserInterceptor();
21+
MockHttpServletRequest request = new MockHttpServletRequest();
22+
request.addHeader("x-client-token-user", "{\"user_name\":\"zhangsan\"}");
23+
userInterceptor.preHandle(request, new MockHttpServletResponse(), new Object());
24+
assertEquals(UserContextHolder.getInstance().getUsername(), "zhangsan");
25+
}
26+
}

opensabre-web/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2222
<!-- 编译 -->
23-
<maven.compiler.source>11</maven.compiler.source>
24-
<maven.compiler.target>11</maven.compiler.target>
23+
<maven.compiler.source>17</maven.compiler.source>
24+
<maven.compiler.target>17</maven.compiler.target>
2525
<!-- 测试 -->
2626
<!-- 依赖 -->
2727
</properties>

0 commit comments

Comments
 (0)