浏览代码

首批api演示代码提交

luoshi 4 年之前
父节点
当前提交
91e817fae7

+ 31 - 0
api-demo/pom.xml

@@ -0,0 +1,31 @@
+<?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.qmth.demo</groupId>
+        <artifactId>qmth-demo</artifactId>
+        <version>1.0</version>
+    </parent>
+    <artifactId>api-demo</artifactId>
+
+    <name>api-demo</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>starter-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-uid</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>

+ 15 - 0
api-demo/src/main/java/com/qmth/demo/api/ApiDemoApplication.java

@@ -0,0 +1,15 @@
+package com.qmth.demo.api;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * api demo application
+ */
+@SpringBootApplication(scanBasePackages = "com.qmth.*")
+public class ApiDemoApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(ApiDemoApplication.class, args);
+    }
+}

+ 15 - 0
api-demo/src/main/java/com/qmth/demo/api/auth/DemoAuthenticationService.java

@@ -0,0 +1,15 @@
+package com.qmth.demo.api.auth;
+
+import com.qmth.boot.core.security.model.AccessEntity;
+import com.qmth.boot.core.security.service.AuthenticationService;
+import com.qmth.boot.tools.signature.SignatureType;
+import org.springframework.stereotype.Component;
+
+@Component
+public class DemoAuthenticationService implements AuthenticationService {
+
+    @Override
+    public AccessEntity findByIdentity(String identity, SignatureType type) {
+        return new DemoSession(identity, "token");
+    }
+}

+ 37 - 0
api-demo/src/main/java/com/qmth/demo/api/auth/DemoSession.java

@@ -0,0 +1,37 @@
+package com.qmth.demo.api.auth;
+
+import com.qmth.boot.core.security.model.AccessEntity;
+
+public class DemoSession implements AccessEntity {
+
+    private String identity;
+
+    private String token;
+
+    public DemoSession(String identity, String token) {
+        this.identity = identity;
+        this.token = token;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setIdentity(String identity) {
+        this.identity = identity;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    @Override
+    public String getIdentity() {
+        return identity;
+    }
+
+    @Override
+    public String getSecret() {
+        return token;
+    }
+}

+ 72 - 0
api-demo/src/main/java/com/qmth/demo/api/controller/IndexController.java

@@ -0,0 +1,72 @@
+package com.qmth.demo.api.controller;
+
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
+import com.qmth.boot.api.config.ApiConfig;
+import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.core.enums.Platform;
+import com.qmth.boot.core.uid.service.UidService;
+import org.apache.commons.lang3.RandomUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+@RestController
+@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX)
+@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
+public class IndexController {
+
+    @Resource
+    private ApiConfig apiConfig;
+
+    @Resource
+    private UidService uidService;
+
+    @RequestMapping("/strict")
+    @Aac(strict = BOOL.TRUE)
+    public Object strict() {
+        return true;
+    }
+
+    @RequestMapping("/platform")
+    @Aac(platform = Platform.WEB)
+    public Object platform() {
+        return true;
+    }
+
+    @RequestMapping("/auth")
+    @Aac(auth = BOOL.TRUE)
+    public Object auth() {
+        return true;
+    }
+
+    @RequestMapping("/ipAllow")
+    @Aac(ipAllow = "172.16.*.*")
+    public Object ipallow() {
+        return true;
+    }
+
+    @RequestMapping("/config")
+    public Object config() {
+        return apiConfig;
+    }
+
+    @RequestMapping("/uid")
+    public Object getUid() {
+        return uidService.getId();
+    }
+
+    @RequestMapping("/rateLimit")
+    @Aac(rateLimit = "1/1s")
+    public Object rateLimit() {
+        return true;
+    }
+
+    @RequestMapping("/caller")
+    public Object caller(HttpServletRequest request) {
+        request.setAttribute(ApiConstant.ATTRIBUTE_CALLER, String.valueOf(RandomUtils.nextInt()));
+        return true;
+    }
+}

+ 10 - 0
api-demo/src/main/resources/application.properties

@@ -0,0 +1,10 @@
+server.port=8080
+
+#om.qmth.api.uri-prefix=/aaa
+#com.qmth.api.metrics-endpoint=/metrics
+#com.qmth.api.global-auth=false
+#com.qmth.api.global-strict=false
+#com.qmth.api.global-rate-limit=1/5s
+
+#com.qmth.logging.root-level=info
+#com.qmth.logging.file-path=/Users/luoshi/Downloads/demo.log

+ 88 - 0
pom.xml

@@ -0,0 +1,88 @@
+<?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>
+
+    <groupId>com.qmth.demo</groupId>
+    <artifactId>qmth-demo</artifactId>
+    <packaging>pom</packaging>
+    <version>1.0</version>
+    <modules>
+        <module>api-demo</module>
+    </modules>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <qmth.boot.version>1.0.0</qmth.boot.version>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>starter-api</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>core-uid</artifactId>
+                <version>${qmth.boot.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>junit</groupId>
+                <artifactId>junit</artifactId>
+                <version>4.11</version>
+                <scope>test</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <build>
+        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
+            <plugins>
+                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
+                <plugin>
+                    <artifactId>maven-clean-plugin</artifactId>
+                    <version>3.1.0</version>
+                </plugin>
+                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
+                <plugin>
+                    <artifactId>maven-resources-plugin</artifactId>
+                    <version>3.0.2</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-compiler-plugin</artifactId>
+                    <version>3.8.0</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-surefire-plugin</artifactId>
+                    <version>2.22.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-jar-plugin</artifactId>
+                    <version>3.0.2</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-install-plugin</artifactId>
+                    <version>2.5.2</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-deploy-plugin</artifactId>
+                    <version>2.8.2</version>
+                </plugin>
+                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
+                <plugin>
+                    <artifactId>maven-site-plugin</artifactId>
+                    <version>3.7.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-project-info-reports-plugin</artifactId>
+                    <version>3.0.0</version>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+    </build>
+</project>