This commit is contained in:
六如
2024-09-06 23:17:43 +08:00
parent 5dd9c597a9
commit 9a213743d7
132 changed files with 2491 additions and 2516 deletions

17
pom.xml
View File

@@ -17,15 +17,16 @@
<modules> <modules>
<module>doc</module> <module>doc</module>
<module>sop-common</module> <!-- <module>sop-common</module>-->
<module>sop-auth</module> <!-- <module>sop-auth</module>-->
<module>sop-example</module> <module>sop-example</module>
<module>sop-admin</module> <module>sop-admin</module>
<module>sop-gateway</module> <!-- <module>sop-gateway</module>-->
<module>sop-test</module> <module>sop-test</module>
<module>sop-sdk</module> <module>sop-sdk</module>
<module>sop-website</module> <!-- <module>sop-website</module>-->
<module>sop-index</module> <module>sop-index</module>
<module>sop-spring-boot-starter</module>
</modules> </modules>
<properties> <properties>
@@ -57,7 +58,7 @@
<commons-logging.version>1.2</commons-logging.version> <commons-logging.version>1.2</commons-logging.version>
<validation-api.version>2.0.1.Final</validation-api.version> <validation-api.version>2.0.1.Final</validation-api.version>
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version> <hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
<fastmybatis.version>2.4.8</fastmybatis.version> <fastmybatis.version>3.0.10</fastmybatis.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version> <mybatis-plus.version>3.5.3.1</mybatis-plus.version>
<guava.version>29.0-jre</guava.version> <guava.version>29.0-jre</guava.version>
<knife4j.version>3.0.2</knife4j.version> <knife4j.version>3.0.2</knife4j.version>
@@ -99,6 +100,12 @@
<version>${dubbo.version}</version> <version>${dubbo.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>

View File

@@ -25,7 +25,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>${project.version}</version> <version>5.0.0-SNAPSHOT</version>
</dependency> </dependency>
<!-- sop相关配置 end--> <!-- sop相关配置 end-->

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>4.4.2-SNAPSHOT</version> <version>5.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -15,7 +15,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>${project.version}</version> <version>5.0.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -15,6 +15,5 @@
<modules> <modules>
<module>sop-story</module> <module>sop-story</module>
<module>sop-springmvc</module>
</modules> </modules>
</project> </project>

View File

@@ -1,25 +0,0 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

View File

@@ -1,181 +0,0 @@
<?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/maven-v4_0_0.xsd">
<parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-parent</artifactId>
<version>5.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sop-springmvc</artifactId>
<name>sop-springmvc</name>
<packaging>war</packaging>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.7.25</org.slf4j-version>
</properties>
<dependencies>
<!-- sop接入依赖 -->
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>${project.version}</version>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.3.3</version>
</dependency>
<!-- sop接入依赖 end -->
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,43 +0,0 @@
package com.gitee.app.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.web.context.support.StandardServletEnvironment;
import java.util.Map;
import java.util.Properties;
/**
* @author tanghc
*/
public class MyPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
super.postProcessBeanFactory(beanFactory);
if (environment instanceof StandardServletEnvironment) {
PropertySources appliedPropertySources = this.getAppliedPropertySources();
for (PropertySource<?> propertySource : appliedPropertySources) {
Object source = propertySource.getSource();
if (source instanceof Map) {
Map map = (Map)source;
map.forEach((key, value)-> {
System.setProperty(key.toString(), value.toString());
});
}
}
}
}
}

View File

@@ -1,68 +0,0 @@
package com.gitee.app.config;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.utils.NetUtils;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.gitee.sop.servercommon.configuration.SpringmvcConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
/**
* 使用支付宝开放平台功能
*
* @author tanghc
*/
@Slf4j
@EnableNacosDiscovery
public class OpenServiceConfig extends SpringmvcConfiguration {
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String SERVER_CONTEXT_PATH = "server.servlet.context-path";
public static final String SERVER_IP = "server.ip";
public static final String SERVER_PORT = "server.port";
public static final String METADATA_TIME_STARTUP = "server.startup-time";
@Value("${spring.application.name}")
private String serviceId;
@Value("${server.port}")
private int port;
@Value("${server.servlet.context-path}")
private String contextPath;
@NacosInjected
private NamingService namingService;
@Override
protected void doAfter() {
super.doAfter();
try {
String ip = NetUtils.localIP();
System.setProperty(SPRING_APPLICATION_NAME, serviceId);
System.setProperty(SERVER_IP, ip);
System.setProperty(SERVER_PORT, String.valueOf(port));
System.setProperty(SERVER_CONTEXT_PATH, contextPath);
Instance instance = this.getInstance(serviceId, ip, port, contextPath);
namingService.registerInstance(serviceId, instance);
log.info("注册到nacos, serviceId: {}, ip: {}, port: {}, contextPath: {}", serviceId, ip, port, contextPath);
} catch (NacosException e) {
log.error("注册nacos失败", e);
throw new RuntimeException("注册nacos失败", e);
}
}
private Instance getInstance(String serviceId, String ip, int port, String contextPath) {
Instance instance = new Instance();
instance.setServiceName(serviceId);
instance.setIp(ip);
instance.setPort(port);
instance.getMetadata().put(METADATA_SERVER_CONTEXT_PATH, contextPath);
instance.getMetadata().put(METADATA_TIME_STARTUP, String.valueOf(System.currentTimeMillis()));
return instance;
}
}

View File

@@ -1,41 +0,0 @@
package com.gitee.app.config;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 容器销毁注销nacos配置见web.xml
*/
@Slf4j
public class OpenServletContextListener implements ServletContextListener {
private static WebApplicationContext webApplicationContext;
@Override
public void contextDestroyed(ServletContextEvent sce) {
String serviceId = System.getProperty(OpenServiceConfig.SPRING_APPLICATION_NAME);
String ip = System.getProperty(OpenServiceConfig.SERVER_IP);
String port = System.getProperty(OpenServiceConfig.SERVER_PORT);
log.info("注销nacosserviceId:{}, ip:{}, port:{}", serviceId, ip, port);
NamingService namingService = webApplicationContext.getBean(NamingService.class);
try {
namingService.deregisterInstance(serviceId, ip, Integer.parseInt(port));
} catch (NacosException e) {
log.error("注销nacos服务失败serviceId:{}, ip:{}, port:{}", serviceId, ip, port);
}
}
@Override
public void contextInitialized(ServletContextEvent sce) {
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
}
}

View File

@@ -1,21 +0,0 @@
package com.gitee.app.config;
import com.gitee.sop.servercommon.swagger.SwaggerSupport;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 开启文档本地微服务文档地址http://localhost:2222/doc.html
* http://ip:port/v2/api-docs
*/
@EnableSwagger2
public class Swagger2 extends SwaggerSupport {
@Override
protected String getDocTitle() {
return "MVC_API";
}
@Override
protected boolean swaggerAccessProtected() {
return false;
}
}

View File

@@ -1,68 +0,0 @@
package com.gitee.app.controller;
import com.gitee.app.model.Goods;
import com.gitee.app.model.StoryParam;
import com.gitee.sop.servercommon.annotation.Open;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Handles requests for the application home page.
*/
@Api(tags = "MVC接口")
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
@ApiOperation(value="获取商品", notes = "获取商品说明")
@Open("springmvc.goods.get")
@RequestMapping("/goods/get")
@ResponseBody
public Goods getGoods(Goods param) {
Goods goods = new Goods();
goods.setId(1L);
goods.setGoods_name(param.getGoods_name() + " 1");
goods.setPrice(new BigDecimal(5000));
return goods;
}
@Open("springmvc.path.same")
@RequestMapping("iam_same_path")
@ResponseBody
public Object iam_same_path(StoryParam param) {
param.setName(param.getName() + " mvc..");
return param;
}
}

View File

@@ -1,17 +0,0 @@
package com.gitee.app.model;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
@Data
public class Goods {
private Long id;
@NotBlank(message = "goods_name不能为空")
private String goods_name;
private BigDecimal price;
}

View File

@@ -1,23 +0,0 @@
package com.gitee.app.model;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
@Data
public class StoryParam {
@ApiModelProperty(value = "故事ID", example = "111")
private int id;
@NotBlank(message = "name不能为空")
@Length(max = 20, message = "name长度不能超过20")
@ApiModelProperty(value = "故事名称", required = true, example = "白雪公主", position = 3)
private String name;
@ApiModelProperty(value = "备注 (第二)", example = "xx", position = 2)
@Length(max = 64, message = "长度不能超过64")
private String remark;
}

View File

@@ -1,5 +0,0 @@
spring.application.name=sop-springmvc
server.port=2223
server.servlet.context-path=/sop-springmvc
nacos.server-addr=127.0.0.1:8848

View File

@@ -1,41 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.gitee">
<level value="debug" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -1,44 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<bean class="com.gitee.app.config.MyPropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- 只扫描Controller类 -->
<context:component-scan base-package="com.gitee.app" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven />
<bean class="com.gitee.app.config.OpenServiceConfig"/>
<!-- swagger2 配置 -->
<mvc:resources location="classpath:/META-INF/resources/" mapping="doc.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
<bean class="com.gitee.app.config.Swagger2"/>
<!-- 接入SOP配置 end -->
</beans>

View File

@@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.gitee.app.config.MyPropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<!-- 排除Controller类 -->
<context:component-scan base-package="com.gitee.app">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
</beans>

View File

@@ -1,15 +0,0 @@
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" pageEncoding="utf-8" %>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>

View File

@@ -1,61 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SOP监听 -->
<listener>
<listener-class>com.gitee.app.config.OpenServletContextListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置swagger-bootstrap-ui的url请求路径-->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/swagger-resources</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/swagger-resources/configuration/ui</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/swagger-resources/configuration/security</url-pattern>
</servlet-mapping>
<!--此接口地址为SwaggerBootstrapUi提供的增强地址,如果不使用增强功能,可排除此配置-->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/v2/api-docs-ext</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@@ -1,41 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.gitee.app">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>sop-story</artifactId> <artifactId>sop-story</artifactId>
<name>sop-story</name>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
@@ -18,8 +19,8 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-spring-boot-starter</artifactId>
<version>${project.version}</version> <version>5.0.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
@@ -27,32 +28,25 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<!-- 使用nacos注册中心 -->
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>org.apache.dubbo</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- 使用eureka注册中心 -->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.alibaba.boot</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId> <artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
<dependency> <dependency>
@@ -63,7 +57,7 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.4</version> <version>1.18.34</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -1,11 +1,11 @@
package com.gitee.sop.storyweb; package com.gitee.sop.storyweb;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication @SpringBootApplication
@EnableDubbo
public class SopStoryApplication { public class SopStoryApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -1,48 +0,0 @@
package com.gitee.sop.storyweb;
import com.gitee.sop.servercommon.exception.ExceptionHolder;
import com.gitee.sop.servercommon.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 全局异常处理
*
* @author tanghc
*/
@ControllerAdvice
@Slf4j
public class StoryGlobalExceptionHandler {
/**
* 捕获手动抛出的异常
*
* @param request request
* @param response response
* @param exception 异常信息
* @return 返回提示信息
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
// 在返回前加这一句
ExceptionHolder.hold(request, response, exception);
// 下面可以实现自己的全局异常处理
return new ErrorResult(500, exception.getMessage());
}
@Data
@AllArgsConstructor
public static class ErrorResult {
private int code;
private String msg;
}
}

View File

@@ -1,40 +0,0 @@
package com.gitee.sop.storyweb.config;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.configuration.AlipayServiceConfiguration;
import com.gitee.sop.servercommon.swagger.SwaggerSupport;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 开放平台功能
* @author tanghc
*/
@Configuration
public class OpenServiceConfig extends AlipayServiceConfiguration {
static {
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/goods_error");
}
/**
* 开启文档本地微服务文档地址http://localhost:2222/doc.html
* http://ip:port/v2/api-docs
*/
@Configuration
@EnableSwagger2
public static class Swagger2 extends SwaggerSupport {
@Override
protected String getDocTitle() {
return "故事API";
}
@Override
protected boolean swaggerAccessProtected() {
return false;
}
}
}

View File

@@ -1,329 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.servercommon.annotation.BizCode;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.servercommon.bean.ServiceContext;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.storyweb.controller.param.ArrayElementParam;
import com.gitee.sop.storyweb.controller.param.CategoryParam;
import com.gitee.sop.storyweb.controller.param.GoodsParam;
import com.gitee.sop.storyweb.controller.param.LargeTextParam;
import com.gitee.sop.storyweb.controller.param.MemberInfoGetParam;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import com.gitee.sop.storyweb.controller.param.TypeEnum;
import com.gitee.sop.storyweb.controller.result.CategoryResult;
import com.gitee.sop.storyweb.controller.result.MemberInfoGetResult;
import com.gitee.sop.storyweb.controller.result.MemberInfoGetResultMemberInfo;
import com.gitee.sop.storyweb.controller.result.StoryResult;
import com.gitee.sop.storyweb.controller.result.TestResult;
import com.gitee.sop.storyweb.controller.result.TreeResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 签名验证通过后,到达这里进行具体的业务处理。
*
* @author tanghc
*/
@RestController
@RequestMapping("story")
@Slf4j
@Api(tags = "故事接口")
public class Example1001_BaseController {
@Value("${server.port}")
private int port;
// http://localhost:2222/stroy/get
// 原生的接口,可正常调用
@RequestMapping("/get")
public StoryResult get() {
StoryResult result = new StoryResult();
result.setId(1L);
result.setName("海底小纵队(原生)");
return result;
}
// 基础用法
@ApiOperation(value = "获取故事信息(首位)", notes = "获取故事信息的详细信息", position = -100/* position默认0值越小越靠前 */)
@Open(value = "story.get", bizCode = {
// 定义业务错误码,用于文档显示
@BizCode(code = "100001", msg = "姓名错误", solution = "填写正确的姓名"),
@BizCode(code = "100002", msg = "备注错误", solution = "填写正确备注"),
})
@RequestMapping("/get/v1")
public StoryResult get_v1(StoryParam param) {
StoryResult story = new StoryResult();
story.setId(1L);
story.setName("海底小纵队(story.get1.0), " + "param:" + param + ", port:" + port);
return story;
}
// 指定版本号
@ApiOperation(value = "获取故事信息", notes = "获取故事信息的详细信息")
@Open(value = "story.get", version = "2.0", bizCode = {
// 定义业务错误码,用于文档显示
@BizCode(code = "100001", msg = "姓名错误", solution = "填写正确的姓名"),
@BizCode(code = "100002", msg = "备注错误", solution = "填写正确备注"),
})
@RequestMapping("/get/v2")
public StoryResult get_v2(StoryParam param) {
StoryResult story = new StoryResult();
story.setId(1L);
story.setName("海底小纵队(story.get2.0), " + "param:" + param);
return story;
}
@Open(value = "story.system.param.get")
@GetMapping("/get/system/param/v1")
public StoryResult systemParam(StoryParam param) {
StoryResult result = new StoryResult();
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
System.out.println(param == openContext.getBizObject());
System.out.println("app_id:" + openContext.getAppId());
System.out.println("token:" + openContext.getAppAuthToken());
result.setName("系统参数:" + openContext);
return result;
}
@Open(value = "story.system.param.get2")
@GetMapping("/get/system/param/v2")
public StoryResult systemParam2() {
StoryResult result = new StoryResult();
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
String token = openContext.getAppAuthToken();
result.setName(token);
return result;
}
@Open(value = "story.system.param.get3")
@GetMapping("/get/system/param/v3")
public StoryResult systemParam3(HttpServletRequest request) {
System.out.println(request.getParameter("app_id"));
StoryResult result = new StoryResult();
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
String token = openContext.getAppAuthToken();
result.setName(token);
return result;
}
@Open("story.system.param.get4")
@RequestMapping("/get/system/param/v4")
public Object addGoods3(HttpServletRequest request, StoryParam param) {
param.setRemark(request.getRequestURI());
return param;
}
@Open("story.system.param.get5")
@RequestMapping("/get/system/param/v5")
public Object get5(HttpServletRequest request, HttpServletResponse response, StoryParam param) {
param.setRemark(request.getRequestURI() + ", getCharacterEncoding:" + response.getCharacterEncoding());
return param;
}
// 参数绑定,少量参数可以这样写,参数多了建议放进类里面
@Open(value = "story.oneparam")
@GetMapping("/oneParam/v1")
public StoryResult oneParam(@NotBlank(message = "id不能为空") String id, @NotBlank(message = "name不能为空") String name) {
StoryResult result = new StoryResult();
result.setName("id" + id + ", name:" + name);
return result;
}
// 参数绑定
@Open(value = "story.oneparam", version = "1.1")
@GetMapping("/oneParam/v2")
public StoryResult oneParamV2(Integer id) {
StoryResult result = new StoryResult();
result.setName("id" + id);
return result;
}
// 参数绑定,枚举
@Open(value = "story.oneparam", version = "1.2")
@GetMapping("/oneParam/v3")
public StoryResult oneParamV2(@NotNull(message = "typeEnum不能为空") TypeEnum typeEnum) {
StoryResult result = new StoryResult();
result.setName("typeEnum" + typeEnum.name());
return result;
}
// 参数绑定
@Open(value = "story.param.bind", mergeResult = false)
@RequestMapping("/get/param/v1")
public StoryResult param(String id, String name) {
StoryResult result = new StoryResult();
result.setName("参数绑定id:" + id + ", name:" + name);
return result;
}
// 忽略验证
@ApiOperation(value = "忽略签名验证", notes = "忽略签名验证")
@Open(value = "story.get.ignore", ignoreValidate = true)
@RequestMapping(value = "/get/ignore/v1")
public StoryResult getStory21(@RequestBody StoryParam story) {
StoryResult result = new StoryResult();
result.setId((long) story.getId());
result.setName(story.getName() + ", ignoreValidate = true");
return result;
}
@ApiOperation(value = "接收数组", notes = "接收数组")
@Open(value = "story.listparam")
@PostMapping(value = "/get/listparam/v1")
public StoryResult listparam(@RequestBody ArrayElementParam story) {
StoryResult result = new StoryResult();
result.setId(11L);
result.setName(JSON.toJSONString(story));
return result;
}
@Open(value = "story.get.large")
@RequestMapping("/get/large/v1")
public StoryResult getStoryLarge(LargeTextParam param) {
StoryResult result = new StoryResult();
int length = param.getContent().getBytes(StandardCharsets.UTF_8).length;
result.setName("length:" + length);
return result;
}
// 绑定复杂对象
@Open(value = "sdt.get",version = "4.0")
@RequestMapping("/get/v4")
public TestResult getV4(@RequestBody TestResult testResult) {
if(StringUtils.isEmpty(testResult.getType())) {
throw new ServiceException("testResult.getType() 不能为null");
}
return testResult;
}
// 获取header
@Open(value = "test.head",version = "1.0")
@GetMapping("/get/header/v1")
public StoryResult header(@RequestBody StoryParam story, HttpServletRequest request) {
HttpServletRequest servletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
StoryResult storyResult = new StoryResult();
storyResult.setId(1L);
storyResult.setName(story.getName()
+ ", token1:" + request.getHeader("token")
+ ", token2:" + servletRequest.getHeader("token"));
return storyResult;
}
// 返回数组结果
@ApiOperation(value = "返回数组结果(第二)", notes = "返回数组结果", position = -99)
@Open("story.list")
@RequestMapping("/list/v1")
public List<StoryResult> getStory3(StoryParam story) {
int index = 0;
StoryResult storyVO = new StoryResult();
storyVO.setId(1L);
storyVO.setName("白雪公主, index:" + index++);
storyVO.setGmt_create(new Date());
StoryResult storyVO2 = new StoryResult();
storyVO2.setId(1L);
storyVO2.setName("白雪公主, index:" + index++);
storyVO2.setGmt_create(new Date());
return Arrays.asList(storyVO, storyVO2);
}
// 演示文档表格树
@ApiOperation(value = "获取分类信息", notes = "演示表格树")
@Open("category.get")
@PostMapping("/category/get/v1")
public CategoryResult getCategory(CategoryParam param) {
System.out.println(param);
StoryResult result = new StoryResult();
result.setId(1L);
result.setName("白雪公主");
result.setGmt_create(new Date());
CategoryResult categoryResult = new CategoryResult();
categoryResult.setCategoryName("娱乐");
categoryResult.setStoryResult(result);
return categoryResult;
}
// 演示文档页树状返回
@ApiOperation(value = "树状返回", notes = "树状返回")
@Open("story.tree.get")
@PostMapping("/tree/v1")
public TreeResult tree(StoryParam param) {
int id = 0;
TreeResult parent = new TreeResult();
parent.setId(++id);
parent.setName("父节点");
parent.setPid(0);
TreeResult child1 = new TreeResult();
child1.setId(++id);
child1.setName("子节点1");
child1.setPid(1);
TreeResult child2 = new TreeResult();
child2.setId(++id);
child2.setName("子节点2");
child2.setPid(1);
parent.setChildren(Arrays.asList(child1, child2));
return parent;
}
private static String json = "{\"flightDate\":\"2020-09-01\",\"flightNo\":\"HO1705\",\"departureAirport\":\"ZSCN\",\"arrivalAirport\":\"ZPLJ\",\"ycy\":\"11521\",\"lcy\":\"4354\",\"cr\":\"145\",\"et\":\"1\",\"ye\":\"0\",\"td\":\"0\",\"gw\":\"0\",\"ew\":\"146\",\"xl\":\"1018\",\"yj\":\"0\",\"hw\":\"635\"}";
// 返回大数据
@Open(value = "bigdata.get")
@RequestMapping("/bigdata/v1")
public Map<String, Object> bigData(StoryParam param) {
int len = 2000;
List<JSONObject> list = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
list.add(JSON.parseObject(json));
}
Map<String, Object> map = new HashMap<>();
map.put("data", list);
return map;
}
@Open(value = "member.info.get")
@RequestMapping("/member/info/get")
public MemberInfoGetResult memberInfoGet(MemberInfoGetParam param) {
MemberInfoGetResult result = new MemberInfoGetResult();
MemberInfoGetResultMemberInfo memberInfo = new MemberInfoGetResultMemberInfo();
memberInfo.setIsVip((byte)1);
memberInfo.setVipEndtime(new Date());
result.setName(JSON.toJSONString(param));
result.setId(11);
result.setMemberInfo(memberInfo);
return result;
}
}

View File

@@ -1,116 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.util.UploadUtil;
import com.gitee.sop.storyweb.controller.param.FileUploadParam;
import com.gitee.sop.storyweb.controller.param.FileUploadParam2;
import com.gitee.sop.storyweb.controller.result.FileUploadResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Optional;
/**
* 演示文件上传
*
* @author tanghc
*/
@RestController
@RequestMapping("upload")
@Api(tags = "文件上传", position = 2)
public class Example1002_FileUploadController {
/**
* 方式1将文件写在参数中可直接获取。好处是可以校验是否上传
* @param param
* @return
*/
@ApiOperation(value = "文件上传例1", notes = "上传文件demo")
@Open("file.upload")
@PostMapping(value = "file1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public FileUploadResult file1(FileUploadParam param) {
// 获取上传的文件
MultipartFile file1 = param.getFile1();
MultipartFile file2 = param.getFile2();
FileUploadResult result = new FileUploadResult();
FileUploadResult.FileMeta fileMeta1 = buildFileMeta(file1);
FileUploadResult.FileMeta fileMeta2 = buildFileMeta(file2);
result.setRemark(param.getRemark());
result.getFiles().add(fileMeta1);
result.getFiles().add(fileMeta2);
return result;
}
/**
* 方式2从request中获取上传文件
*
* @param param
* @return
*/
@ApiOperation(value = "文件上传例2", notes = "可上传多个文件"
// 多文件上传、不确定文件数量上传,必须申明下面这句,否则沙盒界面不会出现上传控件
, extensions = @Extension(properties = @ExtensionProperty(name = "multiple", value = "multiple")))
@Open("file.upload2")
@PostMapping(value = "file2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public FileUploadResult file2(FileUploadParam2 param, HttpServletRequest request) {
System.out.println(param.getRemark());
FileUploadResult result = new FileUploadResult();
// 获取上传的文件
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request);
for (MultipartFile multipartFile : uploadFiles) {
FileUploadResult.FileMeta fileMeta = buildFileMeta(multipartFile);
result.getFiles().add(fileMeta);
}
return result;
}
@Open("file.upload3")
@PostMapping(value = "file3", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public FileUploadResult file3(FileUploadParam2 param, HttpServletRequest request) {
System.out.println(param.getRemark());
FileUploadResult result = new FileUploadResult();
// 获取上传的文件
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request);
Optional<MultipartFile> first = uploadFiles.stream().findFirst();
if (first.isPresent()) {
MultipartFile multipartFile = first.get();
try {
String path = System.getProperty("user.dir");
multipartFile.transferTo(new File(path + "/img_"+System.currentTimeMillis()+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
private FileUploadResult.FileMeta buildFileMeta(MultipartFile multipartFile) {
// 文件名
String fileName = multipartFile.getOriginalFilename();
// 文件大小
long size = multipartFile.getSize();
// 文件内容
String fileContent = null;
try {
fileContent = IOUtils.toString(multipartFile.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return new FileUploadResult.FileMeta(fileName, size, fileContent);
}
}

View File

@@ -1,44 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
/**
* 演示文件下载
*
* @author tanghc
*/
@Api(tags = "文件下载", position = 3)
@Controller
@RequestMapping("download")
public class Example1003_DownloadController {
@ApiOperation(value = "文件下载", notes = "演示文件下载")
@Open("file.download")
@RequestMapping(value = "file1", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE/* 这个一定要加,不然沙箱文档不起作用 */)
public ResponseEntity<byte[]> download(StoryParam param) throws IOException {
HttpHeaders headers = new HttpHeaders();
// 假设下载classpath下的application.properties文件
ClassPathResource resource = new ClassPathResource("/application.properties");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", resource.getFilename());
return ResponseEntity
.ok()
.headers(headers)
.body(IOUtils.toByteArray(resource.getInputStream()));
}
}

View File

@@ -1,23 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.storyweb.controller.param.GoodsParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* 演示参数验证
* @author tanghc
*/
@RestController
public class Example1004_JSR303Controller {
@Open("goods.add")
@RequestMapping("jsr303")
public Object addGoods(GoodsParam param, HttpServletRequest request) {
System.out.println(request.getParameter("method"));
return param;
}
}

View File

@@ -1,25 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.storyweb.controller.result.StoryResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 支付宝服务端,假设签名验证通过后,到达这里进行具体的业务处理。
* 这里演示如何接受业务参数。
* @author tanghc
*/
@RestController
public class Example1005_PermissionController {
@Open(value = "story.get.permission", permission = true)
@RequestMapping("perm/get")
public StoryResult getStory() {
StoryResult story = new StoryResult();
story.setId(1L);
story.setName("海底小纵队(story.get.permission)");
return story;
}
}

View File

@@ -1,42 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.storyweb.controller.param.GoodsUpdateParam;
import com.gitee.sop.storyweb.message.GoodsErrorEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示如何抛出异常
* @author tanghc
*/
@RestController
public class Example1005_ThrowExceptionController {
@Open("goods.update")
@RequestMapping("ex")
public Object updateGoods(GoodsUpdateParam param) {
// 方式1
if ("iphone6".equals(param.getGoods_name())) {
throw new ServiceException("不能更新iphone6");
}
// 方式2国际化
if (StringUtils.isEmpty(param.getGoods_name())) {
throw GoodsErrorEnum.NO_GOODS_NAME.getErrorMeta().getException();
}
// 方式3国际化传参
if (param.getGoods_name().length() <= 3) {
throw GoodsErrorEnum.LESS_GOODS_NAME_LEN.getErrorMeta().getException(3);
}
return param;
}
@Open("goods.update2")
@RequestMapping("ex2")
public Object updateGoods2(GoodsUpdateParam param) {
int i = 1/0;
return param;
}
}

View File

@@ -1,34 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.servercommon.bean.ParamNames;
import com.gitee.sop.servercommon.bean.ServiceContext;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import com.gitee.sop.storyweb.controller.result.StoryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author tanghc
*/
@RestController
@Slf4j
@Api(tags = "故事接口")
public class Example1007_TokenController {
@ApiOperation(value="传递token", notes = "传递token")
@Open(value = "story.get.token", needToken = true/* 设置true网关会校验token是否存在 */)
@RequestMapping("token")
public StoryResult token(StoryParam story, HttpServletRequest request) {
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
StoryResult result = new StoryResult();
result.setName("appAuthToken:" + openContext.getAppAuthToken());
return result;
}
}

View File

@@ -1,103 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collection;
/**
* 传统web开发实例
*
* @author tanghc
*/
@RestController
@RequestMapping("food")
@Api(tags = "食物接口")
public class Example1008_RestfulController {
@Autowired
private Environment environment;
// http://localhost:8081/rest/story-service/food/getFoodById?id=1 网关入口
// http://localhost:2222/food/getFoodById/?id=12 本地入口
@ApiOperation(value="获取食物", notes = "根据id获取食物")
@RequestMapping(value = "getFoodById", method = RequestMethod.GET)
public Food getFoodById(Integer id) {
Food food = new Food();
food.setId(id);
food.setName("香蕉, " + environment.getProperty("server.port"));
food.setPrice(new BigDecimal(20.00));
return food;
}
// http://localhost:8081/rest/story-service/food/getFoodByObj?id=2
@RequestMapping(value = "getFoodByObj", method = RequestMethod.GET)
public Food getFoodByObj(Food food) {
return food;
}
// http://localhost:8081/rest/story-service/food/saveFood
@RequestMapping(value = "saveFood", method = RequestMethod.POST)
public Food saveFood(@RequestBody Food food) {
food.setId(3);
return food;
}
@RequestMapping(value = "foodUpload", method = RequestMethod.POST)
public Food upload(Food food, HttpServletRequest request) {
// 获取上传的文件
Collection<MultipartFile> uploadFiles = UploadUtil.getUploadFiles(request);
StringBuilder sb = new StringBuilder();
for (MultipartFile multipartFile : uploadFiles) {
sb.append(multipartFile.getOriginalFilename());
}
food.setId(4);
food.setName("文件名称+" + sb.toString());
return food;
}
@RequestMapping(value = "foodUpload3", method = RequestMethod.POST)
public Food upload3(Food food, MultipartFile file) {
food.setId(5);
food.setName("文件名称+" + file.getOriginalFilename());
return food;
}
@RequestMapping(value = "foodUpload2", method = RequestMethod.POST)
public Food upload2(Food food, MultipartFile file) {
food.setId(4);
food.setName("文件名称2+" + file.getOriginalFilename());
return food;
}
// http://localhost:2222/food/get/3 本地
// http://localhost:8081/rest/story-service/food/get/3 网关访问
@RequestMapping("/get/{id}")
public Food getById(@PathVariable("id") Integer id) {
Food food = new Food();
food.setId(id);
food.setName("香蕉");
food.setPrice(BigDecimal.valueOf(100));
return food;
}
@Data
public static class Food {
private Integer id;
private String name;
private BigDecimal price;
}
}

View File

@@ -1,29 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* 模拟超时设置
*
* @author tanghc
*/
@RestController
public class Example1009_TimeoutController {
@Open("goods.timeout")
@RequestMapping("timeoutTest")
public Object timeout(StoryParam param) {
// 模拟耗时操作耗时10秒
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return param;
}
}

View File

@@ -1,22 +0,0 @@
package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.storyweb.controller.param.StoryParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 模拟超时设置
*
* @author tanghc
*/
@RestController
public class Example1010_SamePathController {
@Open("story.path.same")
@RequestMapping("iam_same_path")
public Object iam_same_path(StoryParam param) {
param.setName(param.getName() + " story..");
return param;
}
}

View File

@@ -1,20 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @author tanghc
*/
@Data
public class ArrayElementParam {
@ApiModelProperty(value = "名字", required = true, example = "白雪公主", position = 1)
private String name;
@ApiModelProperty(value = "数组", required = true, dataType = "List", example = "白雪公主", position = 2)
private List<StoryParam> list;
}

View File

@@ -1,19 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class CategoryParam {
@ApiModelProperty(value = "分类名称", example = "娱乐")
private String categoryName;
@ApiModelProperty(value = "创建时间", example = "2019-09-25 17:12:52")
private Date createTime;
@ApiModelProperty(value = "分类故事")
private StoryParam storyParam;
}

View File

@@ -1,24 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull;
/**
* @author tanghc
*/
@Data
public class FileUploadParam {
private String remark;
// 上传文件字段名称对应表单中的name属性值
@NotNull(message = "请上传文件1")
@ApiModelProperty(value = "文件1", required = true)
private MultipartFile file1;
@NotNull(message = "请上传文件2")
@ApiModelProperty(value = "文件2", required = true)
private MultipartFile file2;
}

View File

@@ -1,12 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import lombok.Data;
/**
* @author tanghc
*/
@Data
public class FileUploadParam2 {
private String remark;
}

View File

@@ -1,19 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
@Data
public class GoodsParam {
@NotBlank(message = "商品名称不能为空")
private String goods_name;
@NotBlank(message = "{goods.remark.notNull}")
private String goods_remark;
// 传参的格式:{xxx}=value1,value2...
@Length(min = 3, max = 20, message = "{goods.comment.length}=3,20")
private String goods_comment;
}

View File

@@ -1,9 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import lombok.Data;
@Data
public class GoodsUpdateParam {
private String goods_name;
}

View File

@@ -1,11 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import lombok.Data;
/**
* @author tanghc
*/
@Data
public class LargeTextParam {
private String content;
}

View File

@@ -1,34 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
/**
* 请求参数
*/
public class MemberInfoGetParam {
private String name;
private Integer age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@@ -1,23 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
@Data
public class StoryParam {
@ApiModelProperty(value = "故事ID", example = "111")
private int id;
@NotBlank(message = "name不能为空")
@Length(max = 20, message = "name长度不能超过20")
@ApiModelProperty(value = "故事名称", required = true, example = "白雪公主", position = 3)
private String name;
@ApiModelProperty(value = "备注 (第二)", example = "xx", position = 2)
@Length(max = 64, message = "长度不能超过64")
private String remark;
}

View File

@@ -1,8 +0,0 @@
package com.gitee.sop.storyweb.controller.param;
/**
* @author tanghc
*/
public enum TypeEnum {
MOBILE, COMPUTER
}

View File

@@ -1,13 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class CategoryResult {
@ApiModelProperty(value = "分类名称", example = "娱乐")
private String categoryName;
@ApiModelProperty(value = "分类故事")
private StoryResult storyResult;
}

View File

@@ -1,37 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* @author tanghc
*/
@Data
public class FileUploadResult {
private List<FileMeta> files = new ArrayList();
private String remark;
@Data
public static class FileMeta {
public FileMeta(String filename, long size, String content) {
this.filename = filename;
this.size = size;
this.content = content;
}
public FileMeta() {
}
@ApiModelProperty(value = "文件名称", example = "1.txt")
private String filename;
@ApiModelProperty(value = "文件大小", example = "109")
private long size;
@ApiModelProperty(value = "文件内容", example = "啊啊啊")
private String content;
}
}

View File

@@ -1,39 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* 接口返回结果
*/
public class MemberInfoGetResult {
private Integer id;
private String name;
@JsonProperty("member_info")
private MemberInfoGetResultMemberInfo memberInfo;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MemberInfoGetResultMemberInfo getMemberInfo() {
return memberInfo;
}
public void setMemberInfo(MemberInfoGetResultMemberInfo memberInfo) {
this.memberInfo = memberInfo;
}
}

View File

@@ -1,29 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
public class MemberInfoGetResultMemberInfo {
@JsonProperty("is_vip")
private Byte isVip;
@JsonProperty("vip_endtime")
private Date vipEndtime;
public Byte getIsVip() {
return isVip;
}
public void setIsVip(Byte isVip) {
this.isVip = isVip;
}
public Date getVipEndtime() {
return vipEndtime;
}
public void setVipEndtime(Date vipEndtime) {
this.vipEndtime = vipEndtime;
}
}

View File

@@ -1,24 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import java.util.Date;
/**
* @author tanghc
*/
@Data
public class StoryResult {
@ApiModelProperty(value = "故事ID", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "故事名称", required = true, example = "海底小纵队")
@Length(max = 20)
private String name;
@ApiModelProperty(value = "创建时间", example = "2019-04-14 19:02:12")
private Date gmt_create;
}

View File

@@ -1,20 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class TestResult {
@ApiModelProperty(value = "标签", example = "学习")
private String label;
@ApiModelProperty(value = "类型", example = "1 超管 2 普管")
private String type;
@ApiModelProperty(value = "集合", example = "集合")
List<TestResult> ss;
}

View File

@@ -1,24 +0,0 @@
package com.gitee.sop.storyweb.controller.result;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @author tanghc
*/
@Data
public class TreeResult {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "名称")
private String name;
@ApiModelProperty(value = "父id")
private Integer pid;
@ApiModelProperty(value = "子节点")
private List<TreeResult> children;
}

View File

@@ -1,20 +0,0 @@
package com.gitee.sop.storyweb.message;
import com.gitee.sop.servercommon.message.ServiceErrorMeta;
public enum GoodsErrorEnum {
/** 参数错误 */
NO_GOODS_NAME("100"),
/** 参数长度太短 */
LESS_GOODS_NAME_LEN("101"),
;
private ServiceErrorMeta errorMeta;
GoodsErrorEnum(String subCode) {
this.errorMeta = new ServiceErrorMeta("isp.goods_error_", subCode);
}
public ServiceErrorMeta getErrorMeta() {
return errorMeta;
}
}

View File

@@ -1,21 +0,0 @@
package com.gitee.sop.storyweb.message;
import com.gitee.sop.servercommon.message.ServiceErrorMeta;
/**
* @author tanghc
*/
public enum StoryErrorEnum {
/** 参数错误 */
param_error("isv.invalid-parameter"),
;
private ServiceErrorMeta errorMeta;
StoryErrorEnum(String subCode) {
this.errorMeta = new ServiceErrorMeta("isp.error_", subCode);
}
public ServiceErrorMeta getErrorMeta() {
return errorMeta;
}
}

View File

@@ -0,0 +1,15 @@
package com.gitee.sop.storyweb.open;
import com.gitee.sop.storyweb.open.req.StorySaveDTO;
import com.gitee.sop.storyweb.open.resp.StoryResponse;
/**
* @author 六如
*/
public interface StoryService {
Integer save(StorySaveDTO storySaveDTO);
StoryResponse getById(Integer id);
}

View File

@@ -0,0 +1,35 @@
package com.gitee.sop.storyweb.open.impl;
import com.gitee.sop.springboot.starter.annotation.Open;
import com.gitee.sop.storyweb.open.StoryService;
import com.gitee.sop.storyweb.open.req.StorySaveDTO;
import com.gitee.sop.storyweb.open.resp.StoryResponse;
import org.apache.dubbo.config.annotation.DubboService;
import javax.validation.constraints.NotNull;
/**
* 开放接口实现
*
* @author 六如
*/
@DubboService(validation = "true")
public class StoreyServiceImpl implements StoryService {
@Open("story.save")
@Override
public Integer save(StorySaveDTO storySaveDTO) {
System.out.println("save storySaveDTO:" + storySaveDTO);
return 1;
}
@Open("story.get")
@Override
public StoryResponse getById(@NotNull(message = "id必填") Integer id) {
StoryResponse storyResponse = new StoryResponse();
storyResponse.setId(id);
storyResponse.setName("乌鸦喝水");
return storyResponse;
}
}

View File

@@ -0,0 +1,23 @@
package com.gitee.sop.storyweb.open.req;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
/**
* @author 六如
*/
@Data
public class StorySaveDTO implements Serializable {
private static final long serialVersionUID = -1214422742659231037L;
@NotBlank(message = "故事名称必填")
private String storyName;
@NotNull(message = "添加时间必填")
private Date addTime;
}

View File

@@ -0,0 +1,18 @@
package com.gitee.sop.storyweb.open.resp;
import lombok.Data;
import java.io.Serializable;
/**
* @author 六如
*/
@Data
public class StoryResponse implements Serializable {
private static final long serialVersionUID = -3743413007549072654L;
private Integer id;
private String name;
}

View File

@@ -1,9 +1,9 @@
server.port=2222 server.port=8082
spring.application.name=story-service spring.application.name=story-service
register.url=127.0.0.1:8848 # nacos
# \u6CE8\u518C\u4E2D\u5FC3 nacos.host=127.0.0.1:8848
spring.cloud.nacos.discovery.server-addr=${register.url}
# \u4E0A\u4F20\u6587\u4EF6\u6700\u5927\u503C dubbo.protocol.name=dubbo
spring.servlet.multipart.max-file-size=20MB dubbo.protocol.port=-1
spring.servlet.multipart.max-request-size=20MB dubbo.application.qos-enable=false
dubbo.registry.address=nacos://${nacos.host}

View File

@@ -1,2 +1 @@
spring.profiles.active=dev spring.profiles.active=dev
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

View File

@@ -9,60 +9,14 @@
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent> </parent>
<artifactId>sop-index</artifactId> <artifactId>sop-index</artifactId>
<version>5.0.0-SNAPSHOT</version> <packaging>pom</packaging>
<name>sop-index</name> <name>sop-index</name>
<description>sop-index</description> <description>sop-index</description>
<properties> <modules>
<java.version>1.8</java.version> <module>sop-index-api</module>
</properties> <module>sop-index-service</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project> </project>

View File

@@ -0,0 +1,28 @@
<?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>com.gitee.sop</groupId>
<artifactId>sop-index</artifactId>
<version>5.0.0-SNAPSHOT</version>
</parent>
<artifactId>sop-index-api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package com.gitee.sop.index.api;
/**
* @author 六如
*/
public interface ApiRegisterService {
/**
* 接口注册
*
* @param registerDTO 接口信息
*/
void register(RegisterDTO registerDTO);
}

View File

@@ -0,0 +1,35 @@
package com.gitee.sop.index.api;
import lombok.Data;
import java.io.Serializable;
/**
* @author 六如
*/
@Data
public class RegisterDTO implements Serializable {
private static final long serialVersionUID = 2183251167679411550L;
private String application;
private String apiName;
private String apiVersion;
private String interfaceClassName;
private String methodName;
private String paramName;
private String paramTypeName;
private Integer isIgnoreValidate;
private Integer isPermission;
private Integer isNeedToken;
}

View File

@@ -0,0 +1,84 @@
<?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>com.gitee.sop</groupId>
<artifactId>sop-index</artifactId>
<version>5.0.0-SNAPSHOT</version>
</parent>
<artifactId>sop-index-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-index-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,9 +1,11 @@
package com.gitee.sop.index; package com.gitee.sop.index;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
@EnableDubbo
public class SopIndexApplication { public class SopIndexApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -0,0 +1,38 @@
package com.gitee.sop.index.common;
import lombok.Data;
import java.io.Serializable;
/**
* @author 六如
*/
@Data
public class ApiInfoDTO implements Serializable {
private static final long serialVersionUID = 2183251167679411550L;
private String application;
private String apiName;
private String apiVersion;
private String interfaceClassName;
private String methodName;
private String paramName;
private String paramTypeName;
private Integer isIgnoreValidate;
private Integer isPermission;
private Integer isNeedToken;
public String buildApiNameVersion() {
return apiName + apiVersion;
}
}

View File

@@ -0,0 +1,64 @@
package com.gitee.sop.index.common;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* @author tanghc
*/
public class SopConstants {
private SopConstants() {}
public static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
public static final String UTF8 = "UTF-8";
public static final String FORMAT_JSON = "json";
public static final String DEFAULT_SIGN_METHOD = "md5";
public static final String EMPTY_JSON = "{}";
public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path";
public static final String METADATA_SERVER_CONTEXT_PATH_COMPATIBILITY = "context-path";
/**
* 在拦截器中调用获取参数:
* String cachedBody = (String)exchange.getAttribute(SopConstants.CACHE_REQUEST_BODY_OBJECT_KEY);
*/
public static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObject";
/**
* 在拦截器中调用获取参数:
* Map<String, String> params = exchange.getAttribute(SopConstants.CACHE_REQUEST_BODY_FOR_MAP);
*/
public static final String CACHE_REQUEST_BODY_FOR_MAP = "cacheRequestBodyForMap";
public static final String CACHE_API_PARAM = "cacheApiParam";
public static final String CACHE_UPLOAD_REQUEST = "cacheUploadRequest";
public static final String X_SERVICE_ERROR_CODE = "x-service-error-code";
public static final String X_SERVICE_ERROR_MESSAGE = "x-service-error-message";
public static final String X_SERVICE_ERROR_RESPONSE = "x-service-error-response";
public static final int BIZ_ERROR_STATUS = 4000;
public static final int UNKNOWN_ERROR_STATUS = 5050;
public static final String UNKNOWN_SERVICE= "_sop_unknown_service_";
public static final String UNKNOWN_METHOD = "_sop_unknown_method_";
public static final String UNKNOWN_VERSION = "_sop_unknown_version_";
public static final String METADATA_ENV_KEY = "env";
public static final String METADATA_ENV_PRE_VALUE = "pre";
public static final String METADATA_ENV_GRAY_VALUE = "gray";
public static final String CACHE_ROUTE_INTERCEPTOR_CONTEXT = "cacheRouteInterceptorContext";
public static final String TARGET_SERVICE = "sop-target-service";
public static final String RESTFUL_REQUEST = "sop-restful-request";
public static final String METADATA_KEY_TIME_STARTUP = "server.startup-time";
public static final String CACHE_ROUTE_INFO = "cacheRouteInfo";
}

View File

@@ -0,0 +1,30 @@
package com.gitee.sop.index.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author 六如
*/
@Configuration
@ConfigurationProperties(prefix = "api")
@Data
public class ApiConfig {
/**
* 超时时间
*/
private int timeoutSeconds = 60 * 5;
/**
* 是否开启限流功能
*/
private boolean openLimit = true;
/**
* 显示返回sign
*/
private boolean showReturnSign = true;
}

View File

@@ -0,0 +1,34 @@
package com.gitee.sop.index.config;
import com.gitee.sop.index.service.manager.impl.LocalApiCacheManagerImpl;
import com.gitee.sop.index.service.manager.impl.LocalSecretManagerImpl;
import com.gitee.sop.index.service.manager.impl.RedisApiCacheManagerImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 六如
*/
@Configuration
public class IndexConfig {
@Bean
@ConditionalOnProperty(value = "manager.api-cache", havingValue = "local", matchIfMissing = true)
public LocalApiCacheManagerImpl localApiCacheManager() {
return new LocalApiCacheManagerImpl();
}
@Bean
@ConditionalOnProperty(value = "manager.api-cache", havingValue = "redis")
public RedisApiCacheManagerImpl redisApiCacheManager() {
return new RedisApiCacheManagerImpl();
}
@Bean
@ConditionalOnProperty(value = "manager.secret", havingValue = "local", matchIfMissing = true)
public LocalSecretManagerImpl localSecretManager() {
return new LocalSecretManagerImpl();
}
}

View File

@@ -1,5 +1,7 @@
package com.gitee.sop.index.controller.param; package com.gitee.sop.index.controller.param;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
@@ -117,7 +119,9 @@ public class ApiRequest implements Serializable {
@NotBlank(message = "biz_content不能为空") @NotBlank(message = "biz_content不能为空")
private String biz_content; private String biz_content;
public String takeApiName() { @JsonIgnore
@JSONField(serialize = false)
public String takeNameVersion() {
return method + version; return method + version;
} }
} }

View File

@@ -0,0 +1,118 @@
package com.gitee.sop.index.controller.param;
import com.gitee.sop.index.exception.ApiException;
import com.gitee.sop.index.message.ErrorEnum;
import com.gitee.sop.index.message.ErrorMeta;
import com.gitee.sop.index.message.IError;
import lombok.Data;
import java.util.Locale;
/**
* 默认的结果封装类.
* <pre>
*
* xml返回结果:
* <response>
* <code>50</code>
* <msg>Remote service error</msg>
* <sub_code>isv.invalid-parameter</sub_code>
* <sub_msg>非法参数</sub_msg>
* </response>
* 成功情况:
* <response>
* <code>0</code>
* <msg>成功消息</msg>
* <data>
* ...返回内容
* </data>
* </response>
*
* json返回格式
* {
* "code":"50",
* "msg":"Remote service error",
* "sub_code":"isv.invalid-parameter",
* "sub_msg":"非法参数"
* }
* 成功情况:
* {
* "code":"0",
* "msg":"成功消息内容。。。",
* "data":{
* ...返回内容
* }
* }
* </pre>
* <p>
* 字段说明:
* code:网关异常码 <br>
* msg:网关异常信息 <br>
* sub_code:业务异常码 <br>
* sub_msg:业务异常信息 <br>
*
* @author tanghc
*/
@Data
public class ApiResponse {
public static final String SUCCESS_CODE = "0";
public static final String SUCCESS_MSG = "success";
/**
* 网关异常码范围0~100 成功返回"0"
*/
private String code = SUCCESS_CODE;
/**
* 网关异常信息
*/
private String msg;
/**
* 业务异常码
*/
private String sub_code;
/**
* 业务异常信息
*/
private String sub_msg;
/**
* 返回对象
*/
private Object data;
public static ApiResponse success(Object data) {
ApiResponse apiResponse = new ApiResponse();
apiResponse.setCode(SUCCESS_CODE);
apiResponse.setMsg(SUCCESS_MSG);
apiResponse.setData(data);
return apiResponse;
}
public static ApiResponse error(ErrorEnum errorEnum) {
ApiResponse apiResponse = new ApiResponse();
ErrorMeta errorMeta = errorEnum.getErrorMeta();
IError error = errorMeta.getError(Locale.SIMPLIFIED_CHINESE);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSub_code());
apiResponse.setSub_msg(error.getSub_msg());
return apiResponse;
}
public static ApiResponse error(ErrorEnum errorEnum, String subMsg) {
ApiResponse apiResponse = new ApiResponse();
ErrorMeta errorMeta = errorEnum.getErrorMeta();
IError error = errorMeta.getError(Locale.SIMPLIFIED_CHINESE);
apiResponse.setCode(error.getCode());
apiResponse.setMsg(error.getMsg());
apiResponse.setSub_code(error.getSub_code());
apiResponse.setSub_msg(subMsg);
return apiResponse;
}
}

View File

@@ -0,0 +1,29 @@
package com.gitee.sop.index.dubbo;
import com.gitee.sop.index.api.ApiRegisterService;
import com.gitee.sop.index.api.RegisterDTO;
import com.gitee.sop.index.common.ApiInfoDTO;
import com.gitee.sop.index.service.manager.ApiCacheManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author 六如
*/
@Slf4j
@DubboService
public class ApiRegisterServiceImpl implements ApiRegisterService {
@Autowired
private ApiCacheManager apiCache;
@Override
public void register(RegisterDTO registerDTO) {
log.info("收到接口注册, registerDTO={}", registerDTO);
ApiInfoDTO apiInfoDTO = new ApiInfoDTO();
BeanUtils.copyProperties(registerDTO, apiInfoDTO);
apiCache.save(apiInfoDTO);
}
}

View File

@@ -1,9 +1,9 @@
package com.gitee.sop.index.exception; package com.gitee.sop.index.exception;
import com.gitee.sop.index.message.ErrorFactory; import com.gitee.sop.index.message.ErrorFactory;
import com.gitee.sop.index.message.ErrorMeta; import com.gitee.sop.index.message.ErrorMeta;
import com.gitee.sop.index.message.IError;
import java.util.Locale; import java.util.Locale;
@@ -11,8 +11,9 @@ import java.util.Locale;
* @author tanghc * @author tanghc
*/ */
public class ApiException extends RuntimeException { public class ApiException extends RuntimeException {
private static final long serialVersionUID = 8278005515613227643L;
private transient Error error; private transient IError error;
private transient ErrorMeta errorMeta; private transient ErrorMeta errorMeta;
private transient Object[] params; private transient Object[] params;
@@ -28,7 +29,7 @@ public class ApiException extends RuntimeException {
this.params = params; this.params = params;
} }
public Error getError(Locale locale) { public IError getError(Locale locale) {
if (error == null) { if (error == null) {
error = ErrorFactory.getError(this.errorMeta, locale, params); error = ErrorFactory.getError(this.errorMeta, locale, params);
} }

View File

@@ -51,6 +51,8 @@ public enum ErrorEnum {
/** 参数无效 */ /** 参数无效 */
ISV_INVALID_PARAMETER(Codes.CODE_INVALID, "isv.invalid-parameter"), ISV_INVALID_PARAMETER(Codes.CODE_INVALID, "isv.invalid-parameter"),
/** 参数不正确 */
ISV_ERROR_PARAMETER(Codes.CODE_INVALID, "isv.error-parameter"),
/** 文件上传失败 */ /** 文件上传失败 */
ISV_UPLOAD_FAIL(Codes.CODE_INVALID, "isv.upload-fail"), ISV_UPLOAD_FAIL(Codes.CODE_INVALID, "isv.upload-fail"),
/** 文件扩展名无效 */ /** 文件扩展名无效 */

View File

@@ -0,0 +1,25 @@
package com.gitee.sop.index.service;
import com.gitee.sop.index.common.ApiInfoDTO;
import com.gitee.sop.index.service.manager.ApiCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 六如
*/
@Service
public class ApiService {
@Autowired
private ApiCacheManager apiCacheManager;
public ApiInfoDTO getApi(String apiName, String apiVersion) {
return apiCacheManager.getOrElse(apiName, apiVersion, () -> {
ApiInfoDTO record = new ApiInfoDTO();
return record;
});
}
}

View File

@@ -4,41 +4,38 @@ import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
/** /**
* dubbo泛化调用 * dubbo泛化调用
* *
* @author 六如 * @author 六如
*/ */
public class GenericServiceInvoker { @Service
public class GenericServiceInvoker implements InitializingBean {
private ApplicationConfig applicationConfig; private ApplicationConfig applicationConfig;
@Value("${nacos.host:localhost:8848}") @Value("${nacos.host:localhost:8848}")
private String nacosHost; private String nacosHost;
@Value("${nacos.group:DEFAULT}")
private String group;
@Value("${spring.application.name}") @Value("${spring.application.name}")
private String appName; private String appName;
@Value("${generic.timeout:5000}") @Value("${generic.timeout:5000}")
private int timeout; private int timeout;
@PostConstruct @Override
public void init() { public void afterPropertiesSet() throws Exception {
RegistryConfig registryConfig = buildRegistryConfig(nacosHost, group); RegistryConfig registryConfig = buildRegistryConfig(nacosHost);
applicationConfig = new ApplicationConfig(); applicationConfig = new ApplicationConfig();
applicationConfig.setName(appName + "-generic"); applicationConfig.setName(appName + "-generic");
applicationConfig.setRegistry(registryConfig); applicationConfig.setRegistry(registryConfig);
} }
private RegistryConfig buildRegistryConfig(String nacosHost, String group) { private RegistryConfig buildRegistryConfig(String nacosHost) {
RegistryConfig config = new RegistryConfig(); RegistryConfig config = new RegistryConfig();
config.setGroup(group);
config.setAddress("nacos://" + nacosHost); config.setAddress("nacos://" + nacosHost);
return config; return config;
} }
@@ -48,10 +45,7 @@ public class GenericServiceInvoker {
reference.setGeneric("true"); reference.setGeneric("true");
reference.setApplication(applicationConfig); reference.setApplication(applicationConfig);
reference.setInterface(interfaceName); reference.setInterface(interfaceName);
reference.setGroup(group);
reference.setTimeout(timeout); reference.setTimeout(timeout);
reference.setSticky(false);
reference.setCheck(false);
try { try {
removeGenericSymbol(parameterTypes); removeGenericSymbol(parameterTypes);
GenericService genericService = reference.get(); GenericService genericService = reference.get();
@@ -61,6 +55,7 @@ public class GenericServiceInvoker {
} }
} }
/** /**
* remove generic from parameterTypes * remove generic from parameterTypes
*/ */

View File

@@ -0,0 +1,122 @@
package com.gitee.sop.index.service;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.gitee.sop.index.common.ApiInfoDTO;
import com.gitee.sop.index.controller.param.ApiRequest;
import com.gitee.sop.index.controller.param.ApiResponse;
import com.gitee.sop.index.message.ErrorEnum;
import com.gitee.sop.index.service.validate.Validator;
import com.gitee.sop.index.util.ClassUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.rpc.service.GenericException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 接口路由
*
* @author 六如
*/
@Service
@Slf4j
public class RouteService {
private static final String CONSTRAINT_VIOLATION_EXCEPTION = "ConstraintViolationException";
@Autowired
private Validator validator;
@Autowired
private GenericServiceInvoker genericServiceInvoker;
@Autowired
private ApiService apiService;
public ApiResponse route(ApiRequest apiRequest) {
// 校验
validator.validate(apiRequest);
return doRoute(apiRequest);
}
private ApiResponse doRoute(ApiRequest apiRequest) {
ApiInfoDTO apiInfo = apiService.getApi(apiRequest.getMethod(), apiRequest.getVersion());
Object result = null;
Exception error = null;
beforeRoute(apiRequest, apiInfo);
try {
result = genericServiceInvoker.invoke(
apiInfo.getInterfaceClassName(),
apiInfo.getMethodName(),
new String[]{apiInfo.getParamTypeName()},
new Object[]{buildInvokeParam(apiRequest, apiInfo)}
);
} catch (Exception e) {
error = e;
log.error("请求服务出错, apiRequest={}, apiInfo={}", apiInfo, apiInfo, e);
return buildApiResponse(e);
} finally {
afterRoute(result, apiRequest, error);
}
return ApiResponse.success(result);
}
private ApiResponse buildApiResponse(Exception e) {
if (e instanceof GenericException) {
GenericException genericException = (GenericException) e;
String exceptionClass = genericException.getExceptionClass();
if (exceptionClass.contains(CONSTRAINT_VIOLATION_EXCEPTION)) {
String exceptionMessage = genericException.getExceptionMessage();
// 参数校验:Failed to validate service: com.gitee.sop.storyweb.open.StoryService, method: save, cause: [ConstraintViolationImpl{interpolatedMessage='故事名称必填', propertyPath=storyName, rootBeanClass=class com.gitee.sop.storyweb.open.req.StorySaveDTO, messageTemplate='故事名称必填'}]
Set<String> msgs = findErrorMsg(exceptionMessage);
return ApiResponse.error(ErrorEnum.ISV_ERROR_PARAMETER, String.join(",", msgs));
}
}
return ApiResponse.error(ErrorEnum.ISP_SERVICE_UNKNOWN_ERROR);
}
private Set<String> findErrorMsg(String text) {
Pattern pattern = Pattern.compile("'([^']*)'");
Matcher matcher = pattern.matcher(text);
Set<String> msgList = new LinkedHashSet<>();
while (matcher.find()) {
msgList.add(matcher.group(1));
}
return msgList;
}
private Object buildInvokeParam(ApiRequest apiRequest, ApiInfoDTO apiInfo) {
if (ObjectUtils.isEmpty(apiInfo.getParamTypeName())) {
return null;
}
String bizContent = apiRequest.getBiz_content();
JSONObject jsonObject = JSON.parseObject(bizContent);
if (ClassUtil.isPrimitive(apiInfo.getParamTypeName())) {
try {
return jsonObject.getObject(apiInfo.getParamName(), ClassUtils.forName(apiInfo.getParamTypeName()));
} catch (ClassNotFoundException e) {
log.error("找不到参数class, paramTypeName={}, apiRequest={}, apiInfo={}",
apiInfo.getParamTypeName(), apiRequest, apiInfo, e);
throw new RuntimeException(e);
}
} else {
return jsonObject;
}
}
private void beforeRoute(ApiRequest apiRequest, ApiInfoDTO apiInfo) {
log.info("收到接口请求, apiRequest={}", apiRequest);
}
private void afterRoute(Object result, ApiRequest apiRequest, Exception e) {
}
}

View File

@@ -0,0 +1,25 @@
package com.gitee.sop.index.service.manager;
import com.gitee.sop.index.common.ApiInfoDTO;
import java.util.function.Supplier;
/**
* @author 六如
*/
public interface ApiCacheManager {
void save(ApiInfoDTO apiInfoDTO);
ApiInfoDTO get(String apiName, String apiVersion);
default ApiInfoDTO getOrElse(String apiName, String apiVersion, Supplier<ApiInfoDTO> supplier) {
ApiInfoDTO apiInfoDTO = get(apiName, apiVersion);
if (apiInfoDTO == null) {
apiInfoDTO = supplier.get();
}
save(apiInfoDTO);
return apiInfoDTO;
}
}

View File

@@ -0,0 +1,19 @@
package com.gitee.sop.index.service.manager;
/**
* 秘钥管理
*
* @author 六如
*/
public interface SecretManager {
/**
* 获取用户上传的公钥
*
* @param appId appId
* @return 返回公钥内容
*/
String getIsvPublicKey(String appId);
}

View File

@@ -0,0 +1,27 @@
package com.gitee.sop.index.service.manager.impl;
import com.gitee.sop.index.common.ApiInfoDTO;
import com.gitee.sop.index.service.manager.ApiCacheManager;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 本地存储接口信息.
* @author 六如
*/
public class LocalApiCacheManagerImpl implements ApiCacheManager {
private static final Map<String, ApiInfoDTO> CACHE = new ConcurrentHashMap<>();
@Override
public void save(ApiInfoDTO apiInfoDTO) {
String key = apiInfoDTO.buildApiNameVersion();
CACHE.put(key, apiInfoDTO);
}
@Override
public ApiInfoDTO get(String apiName, String apiVersion) {
return CACHE.get(apiName + apiVersion);
}
}

View File

@@ -0,0 +1,23 @@
package com.gitee.sop.index.service.manager.impl;
import com.gitee.sop.index.service.manager.SecretManager;
import java.util.HashMap;
import java.util.Map;
/**
* @author 六如
*/
public class LocalSecretManagerImpl implements SecretManager {
static Map<String, String> PUB_KEY_MGR = new HashMap<>();
static {
PUB_KEY_MGR.put("2019032617262200001", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlyb9aUBaljQP/vjmBFe1mF8HsWSvyfC2NTlpT/V9E+sBxTr8TSkbzJCeeeOEm4LCaVXL0Qz63MZoT24v7AIXTuMdj4jyiM/WJ4tjrWAgnmohNOegfntTto16C3l234vXz4ryWZMR/7W+MXy5B92wPGQEJ0LKFwNEoLspDEWZ7RdE53VH7w6y6sIZUfK+YkXWSwehfKPKlx+lDw3zRJ3/yvMF+U+BAdW/MfECe1GuBnCFKnlMRh3UKczWyXWkL6ItOpYHHJi/jx85op5BWDje2pY9QowzfN94+0DB3T7UvZeweu3zlP6diwAJDzLaFQX8ULfWhY+wfKxIRgs9NoiSAQIDAQAB");
}
@Override
public String getIsvPublicKey(String appId) {
return PUB_KEY_MGR.get(appId);
}
}

View File

@@ -0,0 +1,24 @@
package com.gitee.sop.index.service.manager.impl;
import com.gitee.sop.index.common.ApiInfoDTO;
import com.gitee.sop.index.service.manager.ApiCacheManager;
/**
* redis存储接口信息
*
* @author 六如
*/
public class RedisApiCacheManagerImpl implements ApiCacheManager {
@Override
public void save(ApiInfoDTO apiInfoDTO) {
}
@Override
public ApiInfoDTO get(String apiName, String apiVersion) {
return null;
}
}

View File

@@ -1,9 +1,9 @@
package com.gitee.sop.index.service.validate; package com.gitee.sop.index.service.validate;
import com.gitee.sop.gatewaycommon.message.ErrorEnum; import com.gitee.sop.index.controller.param.ApiRequest;
import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.index.message.ErrorEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.dubbo.common.utils.StringUtils;
/** /**
* @author tanghc * @author tanghc
@@ -18,15 +18,15 @@ public abstract class AbstractSigner implements Signer {
* @param secret 秘钥 * @param secret 秘钥
* @return 返回服务端签名串 * @return 返回服务端签名串
*/ */
protected abstract String buildServerSign(ApiParam params, String secret); protected abstract String buildServerSign(ApiRequest params, String secret);
@Override @Override
public boolean checkSign(ApiParam apiParam, String secret) { public boolean checkSign(ApiRequest apiRequest, String secret) {
String clientSign = apiParam.fetchSignAndRemove(); String clientSign = apiRequest.getSign();
if (StringUtils.isBlank(clientSign)) { if (StringUtils.isBlank(clientSign)) {
throw ErrorEnum.ISV_MISSING_SIGNATURE.getErrorMeta().getException(); throw ErrorEnum.ISV_MISSING_SIGNATURE.getErrorMeta().getException();
} }
String serverSign = buildServerSign(apiParam, secret); String serverSign = buildServerSign(apiRequest, secret);
return clientSign.equals(serverSign); return clientSign.equals(serverSign);
} }

View File

@@ -1,14 +1,14 @@
package com.gitee.sop.index.service.validate; package com.gitee.sop.index.service.validate;
import com.gitee.sop.index.util.AESUtil;
import com.gitee.sop.index.util.RSANewUtil;
import com.gitee.sop.index.util.RSAUtil;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import com.gitee.sop.gatewaycommon.util.AESUtil;
import com.gitee.sop.gatewaycommon.util.RSANewUtil;
import com.gitee.sop.gatewaycommon.util.RSAUtil;
/** /**
* 负责各类加解密 * 负责各类加解密
* @author tanghc
* *
* @author tanghc
*/ */
public class ApiEncrypter implements Encrypter { public class ApiEncrypter implements Encrypter {

View File

@@ -1,17 +1,21 @@
package com.gitee.sop.index.service.validate; package com.gitee.sop.index.service.validate;
import com.gitee.sop.index.common.ParamNames; import com.gitee.sop.index.common.ParamNames;
import com.gitee.sop.index.config.ApiConfig;
import com.gitee.sop.index.controller.param.ApiRequest; import com.gitee.sop.index.controller.param.ApiRequest;
import com.gitee.sop.index.message.ErrorEnum; import com.gitee.sop.index.message.ErrorEnum;
import com.gitee.sop.index.service.manager.ApiCacheManager;
import com.gitee.sop.index.service.manager.SecretManager;
import com.gitee.sop.index.service.validate.alipay.AlipaySigner;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.UploadContext; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize; import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
@@ -25,6 +29,7 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@Getter @Getter
@Service
public class ApiValidator implements Validator { public class ApiValidator implements Validator {
private static final int MILLISECOND_OF_ONE_SECOND = 1000; private static final int MILLISECOND_OF_ONE_SECOND = 1000;
@@ -44,12 +49,23 @@ public class ApiValidator implements Validator {
// @Autowired // @Autowired
// private RouteConfigManager routeConfigManager; // private RouteConfigManager routeConfigManager;
private final Signer signer = new AlipaySigner();
/** /**
* 单个文件大小 * 单个文件大小
*/ */
@Value("${upload.max-file-size:${spring.servlet.multipart.max-file-size:10MB}}") @Value("${upload.max-file-size:${spring.servlet.multipart.max-file-size:10MB}}")
private String maxFileSize; private String maxFileSize;
@Autowired
private ApiConfig apiConfig;
@Autowired
private ApiCacheManager apiCacheManager;
@Autowired
private SecretManager secretManager;
@Override @Override
public void validate(ApiRequest param) { public void validate(ApiRequest param) {
@@ -57,7 +73,7 @@ public class ApiValidator implements Validator {
// TargetRoute targetRoute = checkEnable(param); // TargetRoute targetRoute = checkEnable(param);
// initFields(targetRoute, param); // initFields(targetRoute, param);
// ApiConfig apiConfig = ApiContext.getApiConfig(); // ApiConfig apiConfig = ApiContext.getApiConfig();
checkAppKey(param); // checkAppKey(param);
// if (apiConfig.isIgnoreValidate() // if (apiConfig.isIgnoreValidate()
// || BooleanUtils.toBoolean(targetRoute.getRouteDefinition().getIgnoreValidate())) { // || BooleanUtils.toBoolean(targetRoute.getRouteDefinition().getIgnoreValidate())) {
// if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
@@ -69,9 +85,9 @@ public class ApiValidator implements Validator {
checkSign(param); checkSign(param);
checkTimeout(param); checkTimeout(param);
checkFormat(param); checkFormat(param);
checkUploadFile(param); // checkUploadFile(param);
checkPermission(param); // checkPermission(param);
checkToken(param); // checkToken(param);
} }
/** /**
@@ -100,7 +116,7 @@ public class ApiValidator implements Validator {
// if (version == null) { // if (version == null) {
// throw ErrorEnum.ISV_MISSING_VERSION.getErrorMeta().getException(); // throw ErrorEnum.ISV_MISSING_VERSION.getErrorMeta().getException();
// } // }
// String routeId = param.fetchNameVersion(); // String routeId = param.takeApiName();
// // 检查路由是否存在 // // 检查路由是否存在
// TargetRoute targetRoute = RouteRepositoryContext.getTargetRoute(routeId); // TargetRoute targetRoute = RouteRepositoryContext.getTargetRoute(routeId);
// if (targetRoute == null) { // if (targetRoute == null) {
@@ -114,50 +130,50 @@ public class ApiValidator implements Validator {
// return targetRoute; // return targetRoute;
// } // }
private void initFields(TargetRoute targetRoute, ApiRequest apiParam) { // private void initFields(TargetRoute targetRoute, ApiRequest apiParam) {
apiParam.setServiceId(targetRoute.getServiceDefinition().getServiceId()); // apiParam.setServiceId(targetRoute.getServiceDefinition().getServiceId());
boolean mergeResult; // boolean mergeResult;
Boolean defaultSetting = ApiContext.getApiConfig().getMergeResult(); // Boolean defaultSetting = ApiContext.getApiConfig().getMergeResult();
if (defaultSetting != null) { // if (defaultSetting != null) {
mergeResult = defaultSetting; // mergeResult = defaultSetting;
} else { // } else {
RouteDefinition routeDefinition = targetRoute.getRouteDefinition(); // RouteDefinition routeDefinition = targetRoute.getRouteDefinition();
mergeResult = routeDefinition == null || BooleanUtils.toBoolean(routeDefinition.getMergeResult()); // mergeResult = routeDefinition == null || BooleanUtils.toBoolean(routeDefinition.getMergeResult());
} // }
apiParam.setMergeResult(mergeResult); // apiParam.setMergeResult(mergeResult);
} // }
/** /**
* 校验上传文件内容 * 校验上传文件内容
* *
* @param param * @param param
*/ */
protected void checkUploadFile(ApiRequest param) { // protected void checkUploadFile(ApiRequest param) {
UploadContext uploadContext = param.fetchUploadContext(); // UploadContext uploadContext = param.fetchUploadContext();
if (uploadContext != null) { // if (uploadContext != null) {
try { // try {
List<MultipartFile> files = uploadContext.getAllFile(); // List<MultipartFile> files = uploadContext.getAllFile();
for (MultipartFile file : files) { // for (MultipartFile file : files) {
checkSingleFileSize(file); // checkSingleFileSize(file);
checkFileMd5(param, file); // checkFileMd5(param, file);
} // }
} catch (IOException e) { // } catch (IOException e) {
log.error("验证上传文件MD5错误", e); // log.error("验证上传文件MD5错误", e);
throw ErrorEnum.ISV_UPLOAD_FAIL.getErrorMeta().getException(); // throw ErrorEnum.ISV_UPLOAD_FAIL.getErrorMeta().getException();
} // }
} // }
} // }
private void checkFileMd5(ApiRequest param, MultipartFile file) throws IOException { // private void checkFileMd5(ApiRequest param, MultipartFile file) throws IOException {
// 客户端传来的文件md5 // // 客户端传来的文件md5
String clientMd5 = param.getString(file.getName()); // String clientMd5 = param.getString(file.getName());
if (clientMd5 != null) { // if (clientMd5 != null) {
String fileMd5 = DigestUtils.md5Hex(file.getBytes()); // String fileMd5 = DigestUtils.md5Hex(file.getBytes());
if (!clientMd5.equals(fileMd5)) { // if (!clientMd5.equals(fileMd5)) {
throw ErrorEnum.ISV_UPLOAD_FAIL.getErrorMeta().getException(); // throw ErrorEnum.ISV_UPLOAD_FAIL.getErrorMeta().getException();
} // }
} // }
} // }
/** /**
* 校验单个文件大小 * 校验单个文件大小
@@ -172,7 +188,7 @@ public class ApiValidator implements Validator {
} }
protected void checkTimeout(ApiRequest param) { protected void checkTimeout(ApiRequest param) {
int timeoutSeconds = ApiContext.getApiConfig().getTimeoutSeconds(); int timeoutSeconds = apiConfig.getTimeoutSeconds();
// 如果设置为0表示不校验 // 如果设置为0表示不校验
if (timeoutSeconds == 0) { if (timeoutSeconds == 0) {
return; return;
@@ -180,7 +196,7 @@ public class ApiValidator implements Validator {
if (timeoutSeconds < 0) { if (timeoutSeconds < 0) {
throw new IllegalArgumentException("服务端timeoutSeconds设置错误"); throw new IllegalArgumentException("服务端timeoutSeconds设置错误");
} }
String requestTime = param.fetchTimestamp(); String requestTime = param.getTimestamp();
try { try {
Date requestDate = new SimpleDateFormat(ParamNames.TIMESTAMP_PATTERN).parse(requestTime); Date requestDate = new SimpleDateFormat(ParamNames.TIMESTAMP_PATTERN).parse(requestTime);
long requestMilliseconds = requestDate.getTime(); long requestMilliseconds = requestDate.getTime();
@@ -188,42 +204,42 @@ public class ApiValidator implements Validator {
throw ErrorEnum.ISV_INVALID_TIMESTAMP.getErrorMeta().getException(); throw ErrorEnum.ISV_INVALID_TIMESTAMP.getErrorMeta().getException();
} }
} catch (ParseException e) { } catch (ParseException e) {
throw ErrorEnum.ISV_INVALID_TIMESTAMP.getErrorMeta().getException(param.fetchNameVersion()); throw ErrorEnum.ISV_INVALID_TIMESTAMP.getErrorMeta().getException(param.takeNameVersion());
} }
} }
protected void checkAppKey(ApiRequest param) { // protected void checkAppKey(ApiRequest param) {
if (StringUtils.isEmpty(param.fetchAppKey())) { // if (StringUtils.isEmpty(param.fetchAppKey())) {
throw ErrorEnum.ISV_MISSING_APP_ID.getErrorMeta().getException(); // throw ErrorEnum.ISV_MISSING_APP_ID.getErrorMeta().getException();
} // }
Isv isv = isvManager.getIsv(param.fetchAppKey()); // Isv isv = isvManager.getIsv(param.fetchAppKey());
// 没有用户 // // 没有用户
if (isv == null) { // if (isv == null) {
throw ErrorEnum.ISV_INVALID_APP_ID.getErrorMeta().getException(); // throw ErrorEnum.ISV_INVALID_APP_ID.getErrorMeta().getException();
} // }
// 禁止访问 // // 禁止访问
if (isv.getStatus() == null || isv.getStatus() == STATUS_FORBIDDEN) { // if (isv.getStatus() == null || isv.getStatus() == STATUS_FORBIDDEN) {
throw ErrorEnum.ISV_ACCESS_FORBIDDEN.getErrorMeta().getException(); // throw ErrorEnum.ISV_ACCESS_FORBIDDEN.getErrorMeta().getException();
} // }
} // }
protected void checkSign(ApiRequest param) { protected void checkSign(ApiRequest param) {
String clientSign = param.getSign(); String clientSign = param.getSign();
try { try {
if (StringUtils.isEmpty(clientSign)) { if (StringUtils.isEmpty(clientSign)) {
throw ErrorEnum.ISV_MISSING_SIGNATURE.getErrorMeta().getException(param.takeApiName(), ParamNames.SIGN_NAME); throw ErrorEnum.ISV_MISSING_SIGNATURE.getErrorMeta().getException(param.takeNameVersion(), ParamNames.SIGN_NAME);
} }
ApiConfig apiConfig = ApiContext.getApiConfig(); // ApiConfig apiConfig = ApiContext.getApiConfig();
// 根据appId获取秘钥 // // 根据appId获取秘钥
Isv isvInfo = isvManager.getIsv(param.fetchAppKey()); // Isv isvInfo = isvManager.getIsv(param.fetchAppKey());
String secret = isvInfo.getSecretInfo(); // String secret = isvInfo.getSecretInfo();
if (StringUtils.isEmpty(secret)) { String secret = secretManager.getIsvPublicKey(param.getApp_id());
throw ErrorEnum.ISV_MISSING_SIGNATURE_CONFIG.getErrorMeta().getException(); // if (StringUtils.isEmpty(secret)) {
} // throw ErrorEnum.ISV_MISSING_SIGNATURE_CONFIG.getErrorMeta().getException();
Signer signer = apiConfig.getSigner(); // }
// 错误的sign // 错误的sign
if (!signer.checkSign(param, secret)) { if (!signer.checkSign(param, secret)) {
throw ErrorEnum.ISV_INVALID_SIGNATURE.getErrorMeta().getException(param.fetchNameVersion()); throw ErrorEnum.ISV_INVALID_SIGNATURE.getErrorMeta().getException(param.takeNameVersion());
} }
} finally { } finally {
// 校验过程中会移除sign这里需要重新设置进去 // 校验过程中会移除sign这里需要重新设置进去
@@ -233,11 +249,11 @@ public class ApiValidator implements Validator {
protected void checkFormat(ApiRequest param) { protected void checkFormat(ApiRequest param) {
String format = param.fetchFormat(); String format = param.getFormat();
boolean contains = FORMAT_LIST.contains(format.toLowerCase()); boolean contains = FORMAT_LIST.contains(format.toLowerCase());
if (!contains) { if (!contains) {
throw ErrorEnum.ISV_INVALID_FORMAT.getErrorMeta().getException(param.fetchNameVersion(), format); throw ErrorEnum.ISV_INVALID_FORMAT.getErrorMeta().getException(param.takeNameVersion(), format);
} }
} }
@@ -246,37 +262,37 @@ public class ApiValidator implements Validator {
* *
* @param apiParam 参数 * @param apiParam 参数
*/ */
protected void checkPermission(ApiRequest apiParam) { // protected void checkPermission(ApiRequest apiParam) {
String routeId = apiParam.fetchNameVersion(); // String routeId = apiParam.takeApiName();
TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(routeId); // TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(routeId);
RouteDefinition routeDefinition = targetRoute.getRouteDefinition(); // RouteDefinition routeDefinition = targetRoute.getRouteDefinition();
boolean needCheckPermission = BooleanUtils.toBoolean(routeDefinition.getPermission()); // boolean needCheckPermission = BooleanUtils.toBoolean(routeDefinition.getPermission());
if (needCheckPermission) { // if (needCheckPermission) {
String appKey = apiParam.fetchAppKey(); // String appKey = apiParam.fetchAppKey();
boolean hasPermission = isvRoutePermissionManager.hasPermission(appKey, routeId); // boolean hasPermission = isvRoutePermissionManager.hasPermission(appKey, routeId);
if (!hasPermission) { // if (!hasPermission) {
throw ErrorEnum.ISV_ROUTE_NO_PERMISSIONS.getErrorMeta().getException(); // throw ErrorEnum.ISV_ROUTE_NO_PERMISSIONS.getErrorMeta().getException();
} // }
} // }
} // }
/** /**
* 校验token * 校验token
* *
* @param apiParam 参数 * @param apiParam 参数
*/ */
protected void checkToken(ApiRequest apiParam) { // protected void checkToken(ApiRequest apiParam) {
String routeId = apiParam.fetchNameVersion(); // String routeId = apiParam.takeApiName();
TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(routeId); // TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(routeId);
RouteDefinition routeDefinition = targetRoute.getRouteDefinition(); // RouteDefinition routeDefinition = targetRoute.getRouteDefinition();
boolean needToken = BooleanUtils.toBoolean(routeDefinition.getNeedToken()); // boolean needToken = BooleanUtils.toBoolean(routeDefinition.getNeedToken());
if (needToken) { // if (needToken) {
TokenValidator tokenValidator = ApiConfig.getInstance().getTokenValidator(); // TokenValidator tokenValidator = ApiConfig.getInstance().getTokenValidator();
boolean rightToken = tokenValidator.validateToken(apiParam); // boolean rightToken = tokenValidator.validateToken(apiParam);
if (!rightToken) { // if (!rightToken) {
throw ErrorEnum.AOP_INVALID_APP_AUTH_TOKEN.getErrorMeta().getException(); // throw ErrorEnum.AOP_INVALID_APP_AUTH_TOKEN.getErrorMeta().getException();
} // }
} // }
} // }
} }

View File

@@ -1,6 +1,7 @@
package com.gitee.sop.index.service.validate; package com.gitee.sop.index.service.validate;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.index.common.SopConstants;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@@ -9,7 +10,8 @@ import java.net.URLEncoder;
* @author tanghc * @author tanghc
*/ */
public class SignConfig { public class SignConfig {
private static volatile Wrapper wrapper = new Wrapper() {}; private static volatile Wrapper wrapper = new Wrapper() {
};
public static void enableUrlencodeMode() { public static void enableUrlencodeMode() {
wrapper = new Wrapper() { wrapper = new Wrapper() {

View File

@@ -2,8 +2,6 @@ package com.gitee.sop.index.service.validate;
import com.gitee.sop.index.controller.param.ApiRequest; import com.gitee.sop.index.controller.param.ApiRequest;
import javax.servlet.http.HttpServletRequest;
/** /**
* 负责签名校验 * 负责签名校验
* @author tanghc * @author tanghc
@@ -13,10 +11,10 @@ public interface Signer {
/** /**
* 签名校验 * 签名校验
* @param apiParam 参数 * @param apiRequest 参数
* @param secret 秘钥 * @param secret 秘钥
* @return true签名正确 * @return true签名正确
*/ */
boolean checkSign(ApiRequest apiParam, String secret); boolean checkSign(ApiRequest apiRequest, String secret);
} }

View File

@@ -1,11 +1,12 @@
package com.gitee.sop.index.service.validate; package com.gitee.sop.index.service.validate;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.index.controller.param.ApiRequest;
/** /**
* @author tanghc * @author tanghc
*/ */
@FunctionalInterface @FunctionalInterface
public interface TokenValidator { public interface TokenValidator {
boolean validateToken(ApiParam apiParam); boolean validateToken(ApiRequest apiRequest);
} }

Some files were not shown because too many files have changed in this diff Show More