Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions opensabre-base-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<hutool.version>5.8.35</hutool.version>
<guava.version>33.4.0-jre</guava.version>
<jasypt-springboot.version>3.0.5</jasypt-springboot.version>
<jib-maven-plugin.version>3.4.6</jib-maven-plugin.version>
<!-- 测试 -->
<junit-jupiter.version>5.11.4</junit-jupiter.version>
<httpclient5.version>5.3.1</httpclient5.version>
Expand Down Expand Up @@ -276,6 +277,32 @@
<dockerfile>src/main/docker/Dockerfile</dockerfile>
</configuration>
</plugin>
<!-- Jib Maven插件 -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<!-- 基础镜像 -->
<from>
<image>docker.1ms.run/eclipse-temurin:21-jre-alpine</image>
</from>
<!-- 目标镜像 -->
<to>
<image>opensabre/${project.artifactId}:${project.version}</image>
</to>
<!-- 容器配置 -->
<container>
<user>1001</user>
<workingDirectory>/app</workingDirectory>
<ports>
<port>8000</port>
</ports>
<jvmFlags>-XX:+UseContainerSupport</jvmFlags>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
</plugin>
<!--测试案例-->
<plugin>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.github.opensabre.boot.config;

import io.github.opensabre.boot.entity.SwaggerInfo;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.*;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

Expand All @@ -17,6 +18,28 @@
*/
@AutoConfiguration
@EnableConfigurationProperties(SwaggerInfo.class)
@SecuritySchemes({@SecurityScheme(
// 指定 SecurityScheme 的名称(OpenAPIDefinition注解中的security属性中会引用该名称)
name = "${custom.security.name}",
// 指定认证类型为oauth2
type = SecuritySchemeType.OAUTH2,
// 设置认证流程
flows = @OAuthFlows(
// 设置授权码模式
authorizationCode = @OAuthFlow(
// 获取token地址
tokenUrl = "${custom.security.token-url}",
// 授权申请地址
authorizationUrl = "${custom.security.authorization-url}",
// oauth2的申请的scope(需要在OAuth2客户端中存在)
scopes = {
@OAuthScope(name = "openid", description = "OpenId登录"),
@OAuthScope(name = "profile", description = "获取用户信息"),
@OAuthScope(name = "message.read", description = "读"),
@OAuthScope(name = "message.write", description = "写")
})
)
)})
public class OpensabreSwaggerConfig {

@Resource
Expand All @@ -30,10 +53,12 @@ public class OpensabreSwaggerConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info().title(swaggerInfo.getTitle())
.info(new Info()
.title(swaggerInfo.getTitle())
.description(swaggerInfo.getDescription())
.version(swaggerInfo.getVersion())
.license(new License().name(swaggerInfo.getLicenseName()).url(swaggerInfo.getLicenseUrl())))
// 外部文档
.externalDocs(new ExternalDocumentation()
.description(swaggerInfo.getWikiDocumentation())
.url(swaggerInfo.getWikiUrl()));
Expand Down
105 changes: 105 additions & 0 deletions opensabre-starter-webmvc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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>
<artifactId>opensabre-framework</artifactId>
<groupId>io.github.opensabre</groupId>
<version>${revision}</version>
</parent>

<artifactId>opensabre-starter-webmvc</artifactId>
<packaging>jar</packaging>

<name>opensabre-starter-webmvc</name>
<url>https://github.com/opensabre/</url>
<description>OpenSabre Web project for Spring Boot</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译 -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- 测试 -->
<!-- 依赖 -->
</properties>

<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<developers>
<developer>
<name>zhoutaoo</name>
<email>zhoutaoo@foxmail.com</email>
<roles>
<role>developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>

<scm>
<tag>opensabre-web</tag>
<url>https://github.com/opensabre/opensabre-framework/opensabre-web</url>
<connection>scm:git:https://github.com/opensabre/opensabre-framework.git</connection>
<developerConnection>scm:git:https://github.com/opensabre/opensabre-framework.git</developerConnection>
</scm>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.github.opensabre</groupId>
<artifactId>opensabre-base-dependencies</artifactId>
<version>${revision}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.github.opensabre</groupId>
<artifactId>opensabre-web</artifactId>
</dependency>
<!--spring web相关包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--常用工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--常用json包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!--servlet开发相关-->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--测试框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--junit单元测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
28 changes: 28 additions & 0 deletions opensabre-starter-webmvc/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
WEBMVC公共包
----------

## 简介

主要封装spring-webmvc WEB开发用到的通用公共类、工具类,如公共web拦截器、web统一异常定义等。

## 使用

进入应用目录

安装命令:`mvn install`

## 使用指南

### 应用引入

需要将编译生成的jar包安装到本地maven类进入引用使用。

pom.xml

```
<dependency>
<groupId>io.github.opensabre</groupId>
<artifactId>opensabre-starter-webmvc</artifactId>
<version>0.1.1</version>
</dependency>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.github.opensabre.webmvc.exception;

import io.github.opensabre.common.core.entity.vo.Result;
import io.github.opensabre.common.core.exception.SystemErrorType;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
* 默认全局异常处理类
*/
@Slf4j
@Order
@RestControllerAdvice
public class DefaultWebMvcExceptionHandlerAdvice {

@ExceptionHandler(value = {MissingServletRequestParameterException.class})
public Result<?> missingServletRequestParameterException(MissingServletRequestParameterException ex) {
log.warn("missing servlet request parameter exception:{}", ex.getMessage());
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID);
}

@ExceptionHandler(value = {MethodArgumentNotValidException.class})
public Result<?> argumentInvalidException(MethodArgumentNotValidException ex) {
log.warn("service exception:{}", ex.getMessage());
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, ex.getBindingResult().getFieldError().getDefaultMessage());
}

@ExceptionHandler(value = {HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class})
public Result<?> httpMessageConvertException(HttpMessageNotReadableException ex) {
log.warn("http message convert exception:{}", ex.getMessage());
return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID, "数据解析错误:" + ex.getMessage());
}

@ExceptionHandler(value = {MultipartException.class})
public Result<?> uploadFileLimitException(MultipartException ex) {
log.warn("upload file size limit:{}", ex.getMessage());
return Result.fail(SystemErrorType.UPLOAD_FILE_SIZE_LIMIT);
}

@ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public Result<?> notSupportedMethodException(HttpRequestMethodNotSupportedException ex) {
log.warn("http request method not supported exception {}", ex.getMessage());
return Result.fail(SystemErrorType.METHOD_NOT_SUPPORTED);
}

@ExceptionHandler(value = {NoHandlerFoundException.class, NoResourceFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public Result<?> noHandlerFoundException(ServletException ex) {
log.warn("No static resource exception:{}", ex.getMessage());
return Result.fail(SystemErrorType.RESOURCE_NOT_FOUND, ex.getMessage());
}

@ExceptionHandler(value = {HttpMediaTypeNotSupportedException.class})
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
public Result<?> notSupportedMethodException(HttpMediaTypeNotSupportedException ex) {
log.warn("http request media not supported exception {}", ex.getMessage());
return Result.fail(SystemErrorType.METHOD_NOT_SUPPORTED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.opensabre.webmvc.interceptor;

import io.github.opensabre.common.core.util.UserContextHolder;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class UserInterceptorTest {
@Test
public void preHandle_当未设置token_user_那么正常处理下一个handle() throws Exception {
UserInterceptor userInterceptor = new UserInterceptor();
assertTrue(userInterceptor.preHandle(new MockHttpServletRequest(), new MockHttpServletResponse(), new Object()));
}

@Test
public void preHandle_当设置token的username_那么username可以在线程中拿出来用() throws Exception {
UserInterceptor userInterceptor = new UserInterceptor();
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("x-client-token-user", "{\"user_name\":\"zhangsan\"}");
userInterceptor.preHandle(request, new MockHttpServletResponse(), new Object());
assertEquals(UserContextHolder.getInstance().getUsername(), "zhangsan");
}
}
4 changes: 2 additions & 2 deletions opensabre-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- 测试 -->
<!-- 依赖 -->
</properties>
Expand Down
Loading