init
This commit is contained in:
parent
b350df98a8
commit
1d410bbc01
98
.gitignore
vendored
98
.gitignore
vendored
@ -1,73 +1,33 @@
|
|||||||
# ---> Java
|
HELP.md
|
||||||
# Compiled class file
|
|
||||||
*.class
|
|
||||||
|
|
||||||
# Log file
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# BlueJ files
|
|
||||||
*.ctxt
|
|
||||||
|
|
||||||
# Mobile Tools for Java (J2ME)
|
|
||||||
.mtj.tmp/
|
|
||||||
|
|
||||||
# Package Files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.nar
|
|
||||||
*.ear
|
|
||||||
*.zip
|
|
||||||
*.tar.gz
|
|
||||||
*.rar
|
|
||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
|
||||||
hs_err_pid*
|
|
||||||
replay_pid*
|
|
||||||
|
|
||||||
# ---> macOS
|
|
||||||
# General
|
|
||||||
.DS_Store
|
|
||||||
.AppleDouble
|
|
||||||
.LSOverride
|
|
||||||
|
|
||||||
# Icon must end with two \r
|
|
||||||
Icon
|
|
||||||
|
|
||||||
# Thumbnails
|
|
||||||
._*
|
|
||||||
|
|
||||||
# Files that might appear in the root of a volume
|
|
||||||
.DocumentRevisions-V100
|
|
||||||
.fseventsd
|
|
||||||
.Spotlight-V100
|
|
||||||
.TemporaryItems
|
|
||||||
.Trashes
|
|
||||||
.VolumeIcon.icns
|
|
||||||
.com.apple.timemachine.donotpresent
|
|
||||||
|
|
||||||
# Directories potentially created on remote AFP share
|
|
||||||
.AppleDB
|
|
||||||
.AppleDesktop
|
|
||||||
Network Trash Folder
|
|
||||||
Temporary Items
|
|
||||||
.apdisk
|
|
||||||
|
|
||||||
# ---> Maven
|
|
||||||
target/
|
target/
|
||||||
pom.xml.tag
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
pom.xml.releaseBackup
|
!**/src/main/**/target/
|
||||||
pom.xml.versionsBackup
|
!**/src/test/**/target/
|
||||||
pom.xml.next
|
|
||||||
release.properties
|
|
||||||
dependency-reduced-pom.xml
|
|
||||||
buildNumber.properties
|
|
||||||
.mvn/timing.properties
|
|
||||||
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
|
|
||||||
.mvn/wrapper/maven-wrapper.jar
|
|
||||||
|
|
||||||
# Eclipse m2e generated files
|
### STS ###
|
||||||
# Eclipse Core
|
.apt_generated
|
||||||
.project
|
|
||||||
# JDT-specific (Eclipse Java Development Tools)
|
|
||||||
.classpath
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
115
pom.xml
Normal file
115
pom.xml
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.athis.tech</groupId>
|
||||||
|
<artifactId>athis-server</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>athis-server</name>
|
||||||
|
<description>athis-server</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<spring-boot.version>2.7.6</spring-boot.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.vavr</groupId>
|
||||||
|
<artifactId>vavr</artifactId>
|
||||||
|
<version>0.10.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>33.3.1-jre</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
<version>4.0.23</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>2.0.53</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle.database.jdbc</groupId>
|
||||||
|
<artifactId>ojdbc8</artifactId>
|
||||||
|
<version>18.3.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.athis.tech.athisserver.AthisServerApplication</mainClass>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>repackage</id>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.athis.tech.athisserver;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class AthisServerApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(AthisServerApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.athis.tech.athisserver.conf;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class HuginnRpcApplicationContextAware implements ApplicationContextAware {
|
||||||
|
private static ApplicationContext ctx;
|
||||||
|
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.ctx = applicationContext;
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/com/athis/tech/athisserver/conf/WebConfig.java
Normal file
17
src/main/java/com/athis/tech/athisserver/conf/WebConfig.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.athis.tech.athisserver.conf;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.controller.filter.LoginInterceptor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new LoginInterceptor())
|
||||||
|
.addPathPatterns("/**")
|
||||||
|
.excludePathPatterns("/","/login","/login/**");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,213 @@
|
|||||||
|
package com.athis.tech.athisserver.controller;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.controller.filter.LoginUtil;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
import com.athis.tech.athisserver.service.engine.ProcedureEngineService;
|
||||||
|
import com.athis.tech.athisserver.service.exception.NotFoundException;
|
||||||
|
import com.athis.tech.athisserver.service.exception.ServiceException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
@RequestMapping("/server/procedure")
|
||||||
|
public class ProcedureEngineController {
|
||||||
|
@Autowired
|
||||||
|
private ProcedureEngineService procedureEngineService;
|
||||||
|
|
||||||
|
@GetMapping("/engine/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureEngineGet(@PathVariable("key") String key, HttpServletRequest request) {
|
||||||
|
try {
|
||||||
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
|
||||||
|
if (stringEntry.getValue() == null) {
|
||||||
|
paramMap.put(stringEntry.getKey(), null);
|
||||||
|
} else if (stringEntry.getValue().length == 0) {
|
||||||
|
paramMap.put(stringEntry.getKey(), null);
|
||||||
|
} else if (stringEntry.getValue().length == 1) {
|
||||||
|
paramMap.put(stringEntry.getKey(), stringEntry.getValue()[0]);
|
||||||
|
} else {
|
||||||
|
List<String> list = new ArrayList<>(stringEntry.getValue().length);
|
||||||
|
list.addAll(Arrays.asList(stringEntry.getValue()));
|
||||||
|
paramMap.put(stringEntry.getKey(), list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return runProcedure(key,paramMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/engine/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureEnginePost(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedure(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/engine/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureEnginePut(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedure(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/engine/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureEnginePatch(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedure(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/engine/group/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureGroupEngineGet(@PathVariable("key") String key, HttpServletRequest request) {
|
||||||
|
try {
|
||||||
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
|
||||||
|
if (stringEntry.getValue() == null) {
|
||||||
|
paramMap.put(stringEntry.getKey(), null);
|
||||||
|
} else if (stringEntry.getValue().length == 0) {
|
||||||
|
paramMap.put(stringEntry.getKey(), null);
|
||||||
|
} else if (stringEntry.getValue().length == 1) {
|
||||||
|
paramMap.put(stringEntry.getKey(), stringEntry.getValue()[0]);
|
||||||
|
} else {
|
||||||
|
List<String> list = new ArrayList<>(stringEntry.getValue().length);
|
||||||
|
list.addAll(Arrays.asList(stringEntry.getValue()));
|
||||||
|
paramMap.put(stringEntry.getKey(), list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedureGroup(key, paramMap, true);
|
||||||
|
return ResponseEntity.ok(stringObjectMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/engine/group/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureGroupEnginePost(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedureGroup(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/engine/group/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureGroupEnginePut(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedureGroup(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/engine/group/run/{key}")
|
||||||
|
public ResponseEntity<Map<String, Object>> procedureGroupEnginePatch(@PathVariable("key") String key, @RequestBody Map<String, Object> param) {
|
||||||
|
return runProcedureGroup(key, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Map<String, Object>> runProcedureGroup(String key, Map<String, Object> param) {
|
||||||
|
try {
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedureGroup(key, param, true);
|
||||||
|
return ResponseEntity.ok(stringObjectMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Map<String, Object>> runProcedure(String key, Map<String, Object> param) {
|
||||||
|
try {
|
||||||
|
TokenEntityData tokenData = LoginUtil.getTokenData();
|
||||||
|
if(tokenData != null) {
|
||||||
|
String tokenFrom = tokenData.getTokenFrom();
|
||||||
|
String yhid = tokenData.getYhid();
|
||||||
|
Long bmid = tokenData.getBmid();
|
||||||
|
Long jsid = tokenData.getJsid();
|
||||||
|
param.put("current_yhid", yhid);
|
||||||
|
param.put("current_from", tokenFrom);
|
||||||
|
param.put("current_bmid", bmid);
|
||||||
|
param.put("current_jsid", jsid);
|
||||||
|
}
|
||||||
|
Set<String> ks = new HashSet<>();
|
||||||
|
ks.addAll(param.keySet());
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedure(key, param, true);
|
||||||
|
for (String k : ks) {
|
||||||
|
stringObjectMap.remove(k);
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(stringObjectMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.athis.tech.athisserver.controller;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.ProcedureEngineMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
@Autowired
|
||||||
|
private ProcedureEngineMapper procedureEngineMapper;
|
||||||
|
|
||||||
|
@GetMapping("test")
|
||||||
|
public void test() {
|
||||||
|
try {
|
||||||
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
|
map.put("yhid", "888");
|
||||||
|
// map.put("errcode",0);
|
||||||
|
// map.put("errmsg","");
|
||||||
|
// map.put("paraCursor",new ArrayList<>());
|
||||||
|
procedureEngineMapper.runProcedure(map);
|
||||||
|
System.out.println("---------");
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.filter;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.conf.HuginnRpcApplicationContextAware;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
import com.athis.tech.athisserver.service.token.TokenService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class LoginInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
String token = LoginUtil.getToken(request);
|
||||||
|
if (token == null || token.isEmpty()) {
|
||||||
|
response.sendError(401,"请登录");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
TokenService tokenService = HuginnRpcApplicationContextAware.getApplicationContext().getBean(TokenService.class);
|
||||||
|
TokenEntityData tokenEntityData = tokenService.checkToken(token);
|
||||||
|
boolean b = tokenEntityData != null;
|
||||||
|
if(b) {
|
||||||
|
LoginUtil.setTokenData(tokenEntityData);
|
||||||
|
} else {
|
||||||
|
response.sendError(401,"请登录");
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
LoginUtil.clearTokenData();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.filter;
|
||||||
|
|
||||||
|
public class LoginUser {
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.filter;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
public class LoginUtil {
|
||||||
|
private static final String key = "access_token";
|
||||||
|
private static final ThreadLocal<TokenEntityData> tokenDatas = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void setTokenData(TokenEntityData tokenEntityData) {
|
||||||
|
tokenDatas.set(tokenEntityData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TokenEntityData getTokenData() {
|
||||||
|
return tokenDatas.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearTokenData() {
|
||||||
|
tokenDatas.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getToken(HttpServletRequest request) {
|
||||||
|
String accessToken = request.getHeader(key);
|
||||||
|
if ((accessToken == null || accessToken.isEmpty()) && request.getCookies() != null) {
|
||||||
|
for (Cookie cookie : request.getCookies()) {
|
||||||
|
if (key.equals(cookie.getName())) {
|
||||||
|
accessToken = cookie.getValue();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.login;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class LoginCheckReq {
|
||||||
|
private String username;
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.login;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class LoginCheckResp {
|
||||||
|
private Boolean isUsing;
|
||||||
|
}
|
@ -0,0 +1,197 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.login;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.controller.filter.LoginUtil;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
import com.athis.tech.athisserver.service.engine.ProcedureEngineService;
|
||||||
|
import com.athis.tech.athisserver.service.exception.NotFoundException;
|
||||||
|
import com.athis.tech.athisserver.service.exception.ServiceException;
|
||||||
|
import com.athis.tech.athisserver.service.token.TokenService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Slf4j
|
||||||
|
public class LoginController {
|
||||||
|
@Autowired
|
||||||
|
private ProcedureEngineService procedureEngineService;
|
||||||
|
@Autowired
|
||||||
|
private TokenService tokenService;
|
||||||
|
|
||||||
|
@GetMapping("/login/roles")
|
||||||
|
public ResponseEntity<Map<String, Object>> rules(@RequestParam("yhid") String yhid) {
|
||||||
|
try {
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
paramMap.put("yhid", yhid);
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedure("user-roles", paramMap, false);
|
||||||
|
Object o = stringObjectMap.get("paraCursor");
|
||||||
|
if (o != null && o instanceof List) {
|
||||||
|
List<Map<String, Object>> list = (List<Map<String, Object>>) o;
|
||||||
|
for (Map<String, Object> objectMap : list) {
|
||||||
|
objectMap.remove("YHMM");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(stringObjectMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/login/departs")
|
||||||
|
public ResponseEntity<Map<String, Object>> departs(@RequestParam("yhid") String yhid) {
|
||||||
|
try {
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
paramMap.put("yhid", yhid);
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedure("user-departs", paramMap, false);
|
||||||
|
Object o = stringObjectMap.get("paraCursor");
|
||||||
|
if (o != null && o instanceof List) {
|
||||||
|
List<Map<String, Object>> list = (List<Map<String, Object>>) o;
|
||||||
|
for (Map<String, Object> objectMap : list) {
|
||||||
|
objectMap.remove("YHMM");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(stringObjectMap);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login/check-using")
|
||||||
|
public ResponseEntity loginCheck(@RequestBody LoginCheckReq req) {
|
||||||
|
try {
|
||||||
|
boolean b = tokenService.checkUsing(req.getUsername());
|
||||||
|
LoginCheckResp loginCheckResp = new LoginCheckResp();
|
||||||
|
loginCheckResp.setIsUsing(b);
|
||||||
|
return ResponseEntity.ok(loginCheckResp);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
public ResponseEntity login(@RequestBody LoginReq loginReq) {
|
||||||
|
try {
|
||||||
|
TokenEntityData login = tokenService.login(loginReq);
|
||||||
|
return ResponseEntity.ok(login);
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/logout")
|
||||||
|
public ResponseEntity logout(HttpServletRequest request) {
|
||||||
|
try {
|
||||||
|
String token = LoginUtil.getToken(request);
|
||||||
|
if (token == null || token.isEmpty()) {
|
||||||
|
return ResponseEntity.badRequest().body("token is null");
|
||||||
|
}
|
||||||
|
tokenService.logout(token);
|
||||||
|
return ResponseEntity.ok("");
|
||||||
|
} catch (ServiceException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errCode", e.getErrCode());
|
||||||
|
errorBody.put("errorMessage", e.getErrMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorBody);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("error", e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
Map<String, Object> errorBody = new HashMap<>();
|
||||||
|
errorBody.put("errorMessage", e.getMessage());
|
||||||
|
return ResponseEntity.internalServerError().body(errorBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.athis.tech.athisserver.controller.login;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class LoginReq {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
private Long bmid;
|
||||||
|
private Long jsid;
|
||||||
|
private String tokenFrom;
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ProcedureEngineData;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ProcedureEngineGroupData;
|
||||||
|
import com.athis.tech.athisserver.mybatis.provider.ProcedureUtil;
|
||||||
|
import org.apache.ibatis.annotations.*;
|
||||||
|
import org.apache.ibatis.mapping.StatementType;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ProcedureEngineMapper {
|
||||||
|
|
||||||
|
@SelectProvider(type = ProcedureUtil.class, method = "runProcedure")
|
||||||
|
@Options(statementType = StatementType.CALLABLE)
|
||||||
|
void runProcedure(Map<String, Object> map);
|
||||||
|
|
||||||
|
@Select("select * from t_engine_procedure where apikey = #{key}")
|
||||||
|
ProcedureEngineData getProcedure(@Param("key") String key);
|
||||||
|
|
||||||
|
@Select("select * from t_engine_procedure_group where apikey = #{key}")
|
||||||
|
ProcedureEngineGroupData getProcedureGroup(@Param("key") String key);
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ScriptEntityData;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ScriptMapper {
|
||||||
|
@Select("select * from t_engine_script where apikey = #{key}")
|
||||||
|
ScriptEntityData getScript(@Param("key") String key);
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
import org.apache.ibatis.annotations.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface TokenMapper {
|
||||||
|
@Select("select * from t_user_token where token = #{token}")
|
||||||
|
TokenEntityData getToken(@Param("token") String token);
|
||||||
|
|
||||||
|
@Update("update t_user_token set end_time = #{endTime} where token = #{token}")
|
||||||
|
void updateToken(@Param("token") String token, @Param("endTime") Long endTime);
|
||||||
|
|
||||||
|
@Select("select * from t_user_token where yhid = #{yhid} ")
|
||||||
|
List<TokenEntityData> getTokensByUser(@Param("yhid") String yhid);
|
||||||
|
@Select("select count(token) cnt from t_user_token where yhid = #{yhid} ")
|
||||||
|
Integer getTokenCountByUser(@Param("yhid") String yhid);
|
||||||
|
|
||||||
|
@Delete("delete from t_user_token where token = #{token}")
|
||||||
|
void deleteUserByToken(@Param("token") String token);
|
||||||
|
|
||||||
|
@Insert("insert into t_user_token (token,yhid,bmid,jsid,end_time,token_from) values (#{token},#{yhid},#{bmid},#{jsid},#{endTime},#{tokenFrom})")
|
||||||
|
void insertUserToken(TokenEntityData tokenEntityData);
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class BaseEntityData {
|
||||||
|
private String id;
|
||||||
|
private Long createdTime;
|
||||||
|
private Long updatedTime;
|
||||||
|
private String createdUser;
|
||||||
|
private String updatedUser;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ProcedureEngineData extends BaseEntityData {
|
||||||
|
private String apikey;
|
||||||
|
private String script;
|
||||||
|
private String paramScriptKey;
|
||||||
|
private String resultScriptKey;
|
||||||
|
private Integer isWeb;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ProcedureEngineGroupData extends BaseEntityData {
|
||||||
|
private String apikey;
|
||||||
|
private String procedureKeys;
|
||||||
|
private String paramScriptKey;
|
||||||
|
private String resultScriptKey;
|
||||||
|
private Integer isTransactional;
|
||||||
|
private Integer isWeb;
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ScriptEntityData extends BaseEntityData {
|
||||||
|
private String key;
|
||||||
|
private String type;//procedure_before,procedure_after, procedureGroup_before,procedureGroup_after,sql,sqlGroup 先支持procedure和procedureGroup
|
||||||
|
private String script;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.mapper.entity;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class TokenEntityData {
|
||||||
|
private String token;
|
||||||
|
private String yhid;
|
||||||
|
private Long bmid;
|
||||||
|
private Long jsid;
|
||||||
|
private Long endTime;
|
||||||
|
private String tokenFrom;
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
package com.athis.tech.athisserver.mybatis.provider;
|
||||||
|
|
||||||
|
import groovy.lang.Binding;
|
||||||
|
import groovy.lang.GroovyShell;
|
||||||
|
import groovy.lang.Script;
|
||||||
|
import io.vavr.Tuple;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ProcedureUtil {
|
||||||
|
private static final ThreadLocal<String> scriptThreadLocal = new ThreadLocal<>();
|
||||||
|
public static void setProcedure(String script) {
|
||||||
|
scriptThreadLocal.set(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear() {
|
||||||
|
scriptThreadLocal.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String runProcedure(Map<String,Object> map) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("<script>");
|
||||||
|
// String s = "{ call FRAME.yhjsxx(\n" +
|
||||||
|
// " #{yhid,mode=IN,jdbcType=VARCHAR},\n" +
|
||||||
|
// " #{errcode,mode=OUT,jdbcType=INTEGER},\n" +
|
||||||
|
// " #{errmsg,mode=OUT,jdbcType=VARCHAR},\n" +
|
||||||
|
// " #{paraCursor,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=mapResult}\n" +
|
||||||
|
// " )}";
|
||||||
|
// sb.append(s);
|
||||||
|
sb.append(scriptThreadLocal.get());
|
||||||
|
sb.append("</script>");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
System.out.println("Hello, Groovy");
|
||||||
|
System.out.println(Tuple.of("xxx"));
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
Binding binding = new Binding();
|
||||||
|
binding.setVariable("message","hello groovy");
|
||||||
|
List list = new ArrayList<>();
|
||||||
|
// binding.setVariable("list", list);
|
||||||
|
// binding.setThreadVariables(vars);
|
||||||
|
|
||||||
|
// 创建GroovyShell实例
|
||||||
|
GroovyShell shell = new GroovyShell(binding);
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
// 定义Groovy脚本
|
||||||
|
String script = "import io.vavr.Tuple;" +
|
||||||
|
"list.add('xx1');" +
|
||||||
|
"println message;" +
|
||||||
|
"println Tuple.of('xxx')";
|
||||||
|
Script script1 = shell.parse(script);
|
||||||
|
// 设置内置变量
|
||||||
|
// shell.setVariable("message", "Hello, Groovy!");
|
||||||
|
// shell.setVariable("list", new ArrayList<>());
|
||||||
|
// 执行Groovy脚本
|
||||||
|
// Object result = shell.evaluate(script1);
|
||||||
|
// shell.evaluate("println(list.size())");
|
||||||
|
// System.out.println(System.currentTimeMillis());
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
Object run = script1.run();
|
||||||
|
shell.removeVariable("list");
|
||||||
|
script1.run();
|
||||||
|
shell.evaluate("println(list.size())");
|
||||||
|
System.out.println(System.currentTimeMillis());
|
||||||
|
System.out.println();
|
||||||
|
// 输出结果
|
||||||
|
// System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
package com.athis.tech.athisserver.service.engine;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.ProcedureEngineMapper;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ProcedureEngineData;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ProcedureEngineGroupData;
|
||||||
|
import com.athis.tech.athisserver.mybatis.provider.ProcedureUtil;
|
||||||
|
import com.athis.tech.athisserver.service.exception.NotFoundException;
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import io.vavr.control.Option;
|
||||||
|
import org.mybatis.spring.SqlSessionTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ProcedureEngineService {
|
||||||
|
@Autowired
|
||||||
|
private ProcedureEngineMapper procedureEngineMapper;
|
||||||
|
@Autowired
|
||||||
|
private ScriptService scriptService;
|
||||||
|
@Autowired
|
||||||
|
private SqlSessionTemplate sqlSessionTemplate;
|
||||||
|
@Value("${athis.engine.isCache}")
|
||||||
|
private Boolean useCache;
|
||||||
|
|
||||||
|
|
||||||
|
private final static Cache<String, Option<ProcedureEngineData>> procedureCache = CacheBuilder.newBuilder()
|
||||||
|
//设置cache的初始大小为10,要合理设置该值
|
||||||
|
.initialCapacity(10)
|
||||||
|
//设置并发数为5,即同一时间最多只能有1000个线程往cache执行写入操作
|
||||||
|
.concurrencyLevel(1000)
|
||||||
|
//设置cache中的数据在写入之后的存活时间为10分钟
|
||||||
|
.expireAfterWrite(10, TimeUnit.MINUTES).build();
|
||||||
|
|
||||||
|
private final static Cache<String, Option<ProcedureEngineGroupData>> procedureGroupCache = CacheBuilder.newBuilder()
|
||||||
|
//设置cache的初始大小为10,要合理设置该值
|
||||||
|
.initialCapacity(10)
|
||||||
|
//设置并发数为5,即同一时间最多只能有1000个线程往cache执行写入操作
|
||||||
|
.concurrencyLevel(1000)
|
||||||
|
//设置cache中的数据在写入之后的存活时间为10分钟
|
||||||
|
.expireAfterWrite(10, TimeUnit.MINUTES).build();
|
||||||
|
|
||||||
|
public Map<String, Object> runProcedure(String key, Map<String, Object> paramMap, boolean isWeb) {
|
||||||
|
ProcedureEngineData procedure = getProcedure(key);
|
||||||
|
if (procedure == null) {
|
||||||
|
throw new NotFoundException("not found procedure key");
|
||||||
|
}
|
||||||
|
if (isWeb) {
|
||||||
|
if (procedure.getIsWeb() == null || procedure.getIsWeb() != 1) {
|
||||||
|
throw new NotFoundException("not found web controller");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ProcedureUtil.setProcedure(procedure.getScript());
|
||||||
|
try {
|
||||||
|
Map<String, Object> newParamMap = scriptService.runBeforeScript(procedure.getParamScriptKey(), key, paramMap);
|
||||||
|
procedureEngineMapper.runProcedure(newParamMap);
|
||||||
|
return scriptService.runAfterScript(procedure.getResultScriptKey(), key, newParamMap);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
ProcedureUtil.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> runProcedureGroup(String key, Map<String, Object> paramMap, boolean isWeb) {
|
||||||
|
ProcedureEngineGroupData procedureGroup = getProcedureGroup(key);
|
||||||
|
if (procedureGroup == null) {
|
||||||
|
throw new NotFoundException("not found procedure group key");
|
||||||
|
}
|
||||||
|
if (isWeb) {
|
||||||
|
if (procedureGroup.getIsWeb() == null || procedureGroup.getIsWeb() != 1) {
|
||||||
|
throw new NotFoundException("not found web controller");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String procedureKeys = procedureGroup.getProcedureKeys();
|
||||||
|
if (procedureKeys == null || procedureKeys.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (procedureGroup.getIsTransactional() != null && procedureGroup.getIsTransactional() == 1) {
|
||||||
|
runProcedureGroupTransactional(key, paramMap, procedureGroup);
|
||||||
|
}
|
||||||
|
return toRunProcedureGroup(key, paramMap, procedureGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Map<String, Object> runProcedureGroupTransactional(String key, Map<String, Object> paramMap, ProcedureEngineGroupData procedureGroup) {
|
||||||
|
return toRunProcedureGroup(key, paramMap, procedureGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> toRunProcedureGroup(String key, Map<String, Object> paramMap, ProcedureEngineGroupData procedureGroup) {
|
||||||
|
try {
|
||||||
|
String procedureKeys = procedureGroup.getProcedureKeys();
|
||||||
|
String[] split = procedureKeys.split(",");
|
||||||
|
Map<String, Object> resultMap = new HashMap<>();
|
||||||
|
for (String s : split) {
|
||||||
|
if (s.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
//复制入参,保持每个调用的入参都一致且不会被覆盖
|
||||||
|
Map<String, Object> paramCopy = (Map<String, Object>) JSON.parse(JSON.toJSONString(paramMap));
|
||||||
|
Map<String, Object> newParamMap = scriptService.runBeforeScript(procedureGroup.getParamScriptKey(), key, paramCopy);
|
||||||
|
ProcedureEngineData procedure = getProcedure(key);
|
||||||
|
ProcedureUtil.setProcedure(procedure.getScript());
|
||||||
|
procedureEngineMapper.runProcedure(newParamMap);
|
||||||
|
resultMap.put(s, newParamMap);
|
||||||
|
}
|
||||||
|
return scriptService.runAfterScript(procedureGroup.getResultScriptKey(), key, resultMap);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
ProcedureUtil.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProcedureEngineData getProcedure(String key) {
|
||||||
|
try {
|
||||||
|
if(useCache) {
|
||||||
|
Option<ProcedureEngineData> procedureEngineData = procedureCache.get(key, () -> Option.of(procedureEngineMapper.getProcedure(key)));
|
||||||
|
return procedureEngineData.getOrNull();
|
||||||
|
}
|
||||||
|
return procedureEngineMapper.getProcedure(key);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProcedureEngineGroupData getProcedureGroup(String key) {
|
||||||
|
try {
|
||||||
|
if(useCache) {
|
||||||
|
Option<ProcedureEngineGroupData> procedureEngineGroupData = procedureGroupCache.get(key, () -> Option.of(procedureEngineMapper.getProcedureGroup(key)));
|
||||||
|
return procedureEngineGroupData.getOrNull();
|
||||||
|
}
|
||||||
|
return procedureEngineMapper.getProcedureGroup(key);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
package com.athis.tech.athisserver.service.engine;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.ScriptMapper;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.ScriptEntityData;
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import groovy.lang.GroovyShell;
|
||||||
|
import groovy.lang.Script;
|
||||||
|
import io.vavr.Tuple;
|
||||||
|
import io.vavr.Tuple2;
|
||||||
|
import io.vavr.control.Option;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ScriptService {
|
||||||
|
@Autowired
|
||||||
|
private ScriptMapper scriptMapper;
|
||||||
|
@Value("${athis.engine.isCache}")
|
||||||
|
private Boolean useCache;
|
||||||
|
private static final GroovyShell groovyShell = new GroovyShell();
|
||||||
|
private static final String SCRIPT_IMPORT = "import com.athis.tech.athisserver.service.engine.ScriptUtil;\n";
|
||||||
|
|
||||||
|
private final static Cache<String, Option<Tuple2<String, Script>>> scriptCache = CacheBuilder.newBuilder()
|
||||||
|
//设置cache的初始大小为10,要合理设置该值
|
||||||
|
.initialCapacity(10)
|
||||||
|
//设置并发数为5,即同一时间最多只能有1000个线程往cache执行写入操作
|
||||||
|
.concurrencyLevel(1000)
|
||||||
|
//设置cache中的数据在写入之后的存活时间为10分钟
|
||||||
|
.expireAfterWrite(365, TimeUnit.DAYS).build();
|
||||||
|
|
||||||
|
private final static Cache<String, Option<ScriptEntityData>> scriptDataCache = CacheBuilder.newBuilder()
|
||||||
|
//设置cache的初始大小为10,要合理设置该值
|
||||||
|
.initialCapacity(10)
|
||||||
|
//设置并发数为5,即同一时间最多只能有1000个线程往cache执行写入操作
|
||||||
|
.concurrencyLevel(1000)
|
||||||
|
//设置cache中的数据在写入之后的存活时间为10分钟
|
||||||
|
.expireAfterWrite(10, TimeUnit.DAYS).build();
|
||||||
|
|
||||||
|
public Map<String, Object> runBeforeScript(String scriptKey, String procedureKey, Map<String, Object> paramMap) {
|
||||||
|
if(scriptKey == null || scriptKey.isEmpty()) {
|
||||||
|
return paramMap;
|
||||||
|
}
|
||||||
|
Script script = getScript(scriptKey);
|
||||||
|
if (script == null) {
|
||||||
|
return paramMap;
|
||||||
|
}
|
||||||
|
Option<ScriptEntityData> scriptEntity = getScriptEntity(scriptKey);
|
||||||
|
ScriptUtil.ScriptData scriptData = new ScriptUtil.ScriptData();
|
||||||
|
scriptData.setData(paramMap);
|
||||||
|
scriptData.setKey(scriptKey);
|
||||||
|
scriptData.setType(scriptEntity.map(ScriptEntityData::getType).getOrNull());
|
||||||
|
scriptData.setProcedureKey(procedureKey);
|
||||||
|
ScriptUtil.setData(scriptData);
|
||||||
|
return runScript(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> runAfterScript(String scriptKey, String procedureKey, Map<String, Object> resultMap) {
|
||||||
|
if(scriptKey == null || scriptKey.isEmpty()) {
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
Script script = getScript(scriptKey);
|
||||||
|
if (script == null) {
|
||||||
|
return resultMap;
|
||||||
|
}
|
||||||
|
Option<ScriptEntityData> scriptEntity = getScriptEntity(scriptKey);
|
||||||
|
ScriptUtil.ScriptData scriptData = new ScriptUtil.ScriptData();
|
||||||
|
scriptData.setData(resultMap);
|
||||||
|
scriptData.setKey(scriptKey);
|
||||||
|
scriptData.setType(scriptEntity.map(ScriptEntityData::getType).getOrNull());
|
||||||
|
scriptData.setProcedureKey(procedureKey);
|
||||||
|
ScriptUtil.setData(scriptData);
|
||||||
|
return runScript(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Option<ScriptEntityData> getScriptEntity(String key) {
|
||||||
|
try {
|
||||||
|
if(useCache) {
|
||||||
|
return scriptDataCache.get(key, () -> Option.of(scriptMapper.getScript(key)));
|
||||||
|
}
|
||||||
|
return Option.of(scriptMapper.getScript(key));
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Script getScript(String key) {
|
||||||
|
try {
|
||||||
|
//如果key在数据库中查不到,则清除编译后的script
|
||||||
|
|
||||||
|
Option<ScriptEntityData> scriptEntityData = getScriptEntity(key);
|
||||||
|
if(useCache) {
|
||||||
|
if (scriptEntityData.isEmpty()) {
|
||||||
|
Option<Tuple2<String, Script>> ifPresent = scriptCache.getIfPresent(key);
|
||||||
|
if (ifPresent != null) {
|
||||||
|
scriptCache.invalidate(key);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Option<Tuple2<String, Script>> scriptT2 = scriptCache.get(key, () -> {
|
||||||
|
String scriptStr = scriptEntityData.get().getScript();
|
||||||
|
scriptStr = SCRIPT_IMPORT + scriptStr;
|
||||||
|
Script parse = groovyShell.parse(scriptStr);
|
||||||
|
return Option.of(Tuple.of(scriptEntityData.get().getKey(), parse));
|
||||||
|
});
|
||||||
|
return scriptT2.map(t2 -> t2._2).getOrNull();
|
||||||
|
}
|
||||||
|
String scriptStr = scriptEntityData.get().getScript();
|
||||||
|
scriptStr = SCRIPT_IMPORT + scriptStr;
|
||||||
|
return groovyShell.parse(scriptStr);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> runScript(Script script) {
|
||||||
|
try {
|
||||||
|
return (Map<String, Object>) script.run();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
ScriptUtil.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.athis.tech.athisserver.service.engine;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import oracle.jdbc.internal.OracleCallableStatement;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static java.sql.JDBCType.*;
|
||||||
|
|
||||||
|
public class ScriptUtil {
|
||||||
|
private static final ThreadLocal<ScriptData> threadLocal = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static ScriptData getData() {
|
||||||
|
return threadLocal.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setData(ScriptData scriptData) {
|
||||||
|
threadLocal.set(scriptData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear() {
|
||||||
|
threadLocal.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class ScriptData {
|
||||||
|
private String key;
|
||||||
|
private String type;
|
||||||
|
private String procedureKey;
|
||||||
|
private Map<String, Object> data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Class.forName("oracle.jdbc.driver.OracleDriver");
|
||||||
|
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@47.111.224.22:1521:athis", "athis", "jsjzxabc");
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery("select a,b from (select 2001 a,2001 b from dual)");
|
||||||
|
OracleCallableStatement callableStatement = (OracleCallableStatement) connection.prepareCall("{call FRAME.yhjsbc(?,?,?,?)}");
|
||||||
|
// callableStatement.setCursor(1,resultSet);
|
||||||
|
callableStatement.setObject(1, resultSet);
|
||||||
|
callableStatement.registerOutParameter(2, INTEGER);
|
||||||
|
callableStatement.registerOutParameter(3, VARCHAR);
|
||||||
|
callableStatement.registerOutParameter(4, REF);
|
||||||
|
|
||||||
|
// ResultSet cursor = (ResultSet) callableStatement.getObject(4);
|
||||||
|
// while (cursor.next()) {
|
||||||
|
// System.out.println(cursor.getObject(1));
|
||||||
|
// System.out.println(cursor.getObject(2));
|
||||||
|
// }
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.athis.tech.athisserver.service.exception;
|
||||||
|
|
||||||
|
public class NotFoundException extends RuntimeException {
|
||||||
|
public NotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return super.getMessage();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.athis.tech.athisserver.service.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class ServiceException extends RuntimeException {
|
||||||
|
private String errCode;
|
||||||
|
private String errMessage;
|
||||||
|
|
||||||
|
public ServiceException(String code,String msg) {
|
||||||
|
super(msg);
|
||||||
|
this.errCode = code;
|
||||||
|
this.errMessage = msg;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.athis.tech.athisserver.service.token;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
public class Md5Util {
|
||||||
|
public static String getMd5ByString(String str) throws NoSuchAlgorithmException {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
md.update(str.getBytes());
|
||||||
|
byte[] digest = md.digest();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (byte b : digest) {
|
||||||
|
sb.append(String.format("%02x", b & 0xff));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,116 @@
|
|||||||
|
package com.athis.tech.athisserver.service.token;
|
||||||
|
|
||||||
|
import com.athis.tech.athisserver.controller.login.LoginReq;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.TokenMapper;
|
||||||
|
import com.athis.tech.athisserver.mybatis.mapper.entity.TokenEntityData;
|
||||||
|
import com.athis.tech.athisserver.service.engine.ProcedureEngineService;
|
||||||
|
import com.athis.tech.athisserver.service.exception.ServiceException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TokenService {
|
||||||
|
@Autowired
|
||||||
|
private TokenMapper tokenMapper;
|
||||||
|
@Autowired
|
||||||
|
private ProcedureEngineService procedureEngineService;
|
||||||
|
@Value("${athis.login.expiration}")
|
||||||
|
private Long expiration;
|
||||||
|
|
||||||
|
public boolean checkUsing(String yhid) {
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
paramMap.put("yhid", yhid);
|
||||||
|
paramMap.put("csdm", "SYS_USER_MODE");
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedure("xtcs", paramMap, false);
|
||||||
|
Object o = stringObjectMap.get("csz");
|
||||||
|
if (o != null && o.toString().equals("Y")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Integer tokenCountByUser = tokenMapper.getTokenCountByUser(yhid);
|
||||||
|
return tokenCountByUser > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TokenEntityData login(LoginReq loginReq) throws NoSuchAlgorithmException {
|
||||||
|
if (loginReq.getUsername() == null || loginReq.getUsername().isEmpty() || loginReq.getPassword() == null || loginReq.getPassword().isEmpty()) {
|
||||||
|
throw new ServiceException("errornull", "用户名 密码 不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginReq.getJsid() == null || loginReq.getBmid() == null) {
|
||||||
|
throw new ServiceException("errornull", "部门 角色 必选");
|
||||||
|
}
|
||||||
|
|
||||||
|
checkPassword(loginReq);
|
||||||
|
|
||||||
|
boolean b = checkUsing(loginReq.getUsername());
|
||||||
|
List<TokenEntityData> tokens = tokenMapper.getTokensByUser(loginReq.getUsername());
|
||||||
|
if (b) {
|
||||||
|
for (TokenEntityData token : tokens) {
|
||||||
|
tokenMapper.deleteUserByToken(token.getToken());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
for (TokenEntityData token : tokens) {
|
||||||
|
if (token.getEndTime() >= now) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
tokenMapper.deleteUserByToken(token.getToken());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
double random = Math.random();
|
||||||
|
String endStr = uuid + Md5Util.getMd5ByString(random + "");
|
||||||
|
String newTokenStr = loginReq.getUsername() + "_" + endStr;
|
||||||
|
TokenEntityData tokenEntityData = new TokenEntityData();
|
||||||
|
tokenEntityData.setToken(newTokenStr);
|
||||||
|
tokenEntityData.setBmid(loginReq.getBmid());
|
||||||
|
tokenEntityData.setYhid(loginReq.getUsername());
|
||||||
|
tokenEntityData.setJsid(loginReq.getJsid());
|
||||||
|
tokenEntityData.setTokenFrom(loginReq.getTokenFrom());
|
||||||
|
tokenEntityData.setEndTime(System.currentTimeMillis() + expiration);
|
||||||
|
tokenMapper.insertUserToken(tokenEntityData);
|
||||||
|
return tokenEntityData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TokenEntityData checkToken(String token) {
|
||||||
|
TokenEntityData token1 = tokenMapper.getToken(token);
|
||||||
|
if(token1 == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
if(token1.getEndTime() >= now) {
|
||||||
|
tokenMapper.updateToken(token, now + expiration);
|
||||||
|
return token1;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logout(String token) {
|
||||||
|
tokenMapper.deleteUserByToken(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkPassword(LoginReq loginReq) throws NoSuchAlgorithmException {
|
||||||
|
Map<String, Object> paramMap = new LinkedHashMap<>();
|
||||||
|
paramMap.put("yhid", loginReq.getUsername());
|
||||||
|
Map<String, Object> stringObjectMap = procedureEngineService.runProcedure("user-roles", paramMap, false);
|
||||||
|
Object o = stringObjectMap.get("paraCursor");
|
||||||
|
String yhmm = null;
|
||||||
|
if (o != null && o instanceof List) {
|
||||||
|
List<Map<String, Object>> list = (List<Map<String, Object>>) o;
|
||||||
|
if (!list.isEmpty()) {
|
||||||
|
yhmm = list.get(0).get("YHMM").toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String md5ByString = Md5Util.getMd5ByString(loginReq.getPassword());
|
||||||
|
if (!md5ByString.equals(yhmm)) {
|
||||||
|
throw new ServiceException("errornull", "用户名或密码错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
src/main/resources/application.properties
Normal file
23
src/main/resources/application.properties
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# 应用服务 WEB 访问端口
|
||||||
|
server.port=8089
|
||||||
|
#下面这些内容是为了让MyBatis映射
|
||||||
|
#指定Mybatis的Mapper文件
|
||||||
|
mybatis.mapper-locations=classpath:mappers/*.xml
|
||||||
|
#指定Mybatis的实体目录
|
||||||
|
mybatis.type-aliases-package=com.athis.tech.athisserver.mybatis.mapper
|
||||||
|
mybatis.configuration.map-underscore-to-camel-case=true
|
||||||
|
|
||||||
|
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
|
||||||
|
spring.datasource.url=jdbc:oracle:thin:@47.111.224.22:1521:athis
|
||||||
|
spring.datasource.password=jsjzxabc
|
||||||
|
spring.datasource.username=athis
|
||||||
|
spring.datasource.hikari.connection-init-sql=select 1 from dual
|
||||||
|
spring.datasource.hikari.maximum-pool-size=10
|
||||||
|
spring.datasource.hikari.minimum-idle=10
|
||||||
|
spring.datasource.hikari.connection-timeout=30000
|
||||||
|
spring.datasource.hikari.max-lifetime=1800000
|
||||||
|
spring.datasource.hikari.connection-test-query=select 1 from dual
|
||||||
|
spring.datasource.hikari.idle-timeout=600000
|
||||||
|
|
||||||
|
athis.engine.isCache=false
|
||||||
|
athis.login.expiration=7200000
|
40
src/main/resources/mappers/ProcedureEngineMapper.xml
Normal file
40
src/main/resources/mappers/ProcedureEngineMapper.xml
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="com.athis.tech.athisserver.mybatis.mapper.ProcedureEngineMapper">
|
||||||
|
<!-- <select id="test" parameterType="java.util.HashMap" statementType="CALLABLE">-->
|
||||||
|
<!-- { call FRAME.yhjsxx(-->
|
||||||
|
<!-- #{yhid,mode=IN,jdbcType=VARCHAR},-->
|
||||||
|
<!-- #{errcode,mode=OUT,jdbcType=INTEGER},-->
|
||||||
|
<!-- #{errmsg,mode=OUT,jdbcType=VARCHAR},-->
|
||||||
|
<!-- #{paraCursor,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=mapResult}-->
|
||||||
|
<!-- )}-->
|
||||||
|
<!-- </select>-->
|
||||||
|
<!-- <select id="" parameterMap="">-->
|
||||||
|
<!-- { call FRAME.yhjsbc(-->
|
||||||
|
<!-- #{pcursor,mode=IN,jdbcType=CURSOR,javaType=ResultSet,resultMap=mapResult,typeHandler=com.athis.tech.athisserver.mybatis.handler.ListToArrayTypeHandler},-->
|
||||||
|
<!-- #{errcode,mode=OUT,jdbcType=INTEGER},-->
|
||||||
|
<!-- #{errmsg,mode=OUT,jdbcType=VARCHAR},-->
|
||||||
|
<!-- #{paraCursor,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=mapResult}-->
|
||||||
|
<!-- )}-->
|
||||||
|
<!-- </select>-->
|
||||||
|
<!-- <select id="">-->
|
||||||
|
<!-- DECLARE-->
|
||||||
|
<!-- my_c SYS_REFCURSOR;-->
|
||||||
|
<!-- BEGIN-->
|
||||||
|
<!-- OPEN my_c for select feedetl_sn,det_itemfee_sumamt from-->
|
||||||
|
<!-- <foreach collection="pcursor" item="pmap" open="(" close=")" separator=" union all ">-->
|
||||||
|
<!-- <foreach item="value" index="key" collection="pmap" open=" select " separator=" , " close=" from dual ">-->
|
||||||
|
<!-- #{value} as ${key}-->
|
||||||
|
<!-- </foreach>-->
|
||||||
|
<!-- </foreach>;-->
|
||||||
|
<!-- FRAME.YHJSBC(my_c, #{errcode,mode=OUT,jdbcType=INTEGER},-->
|
||||||
|
<!-- #{errmsg,mode=OUT,jdbcType=VARCHAR},-->
|
||||||
|
<!-- #{paraCursor,mode=OUT,jdbcType=CURSOR,javaType=ResultSet,resultMap=mapResult});-->
|
||||||
|
<!-- END;-->
|
||||||
|
<!-- </select>-->
|
||||||
|
<parameterMap id="mapParam" type="java.util.LinkedHashMap">
|
||||||
|
</parameterMap>
|
||||||
|
|
||||||
|
<resultMap id="mapResult" type="java.util.LinkedHashMap" autoMapping="true">
|
||||||
|
</resultMap>
|
||||||
|
</mapper>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.athis.tech.athisserver;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class AthisServerApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user