Commit 4162469d37acc1aeff4b132751f08066482dc080
1 parent
02f98531
init
Showing
15 changed files
with
563 additions
and
0 deletions
pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | |
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| 5 | + <modelVersion>4.0.0</modelVersion> | |
| 6 | + | |
| 7 | + <groupId>com.irrigation</groupId> | |
| 8 | + <artifactId>common-lib</artifactId> | |
| 9 | + <version>1.0-SNAPSHOT</version> | |
| 10 | + | |
| 11 | + <properties> | |
| 12 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| 13 | + <java.version>1.8</java.version> | |
| 14 | + <swagger.version>2.7.0</swagger.version> | |
| 15 | + <lombok.version>1.16.20</lombok.version> | |
| 16 | + <commons-codec.version>1.11</commons-codec.version> | |
| 17 | + <guava.vsersion>20.0</guava.vsersion> | |
| 18 | + <slf4j.version>1.7.24</slf4j.version> | |
| 19 | + <fastjson.version>1.2.46</fastjson.version> | |
| 20 | + <spring-web.version>4.3.14.RELEASE</spring-web.version> | |
| 21 | + <spring-security-core.version>4.2.4.RELEASE</spring-security-core.version> | |
| 22 | + </properties> | |
| 23 | + | |
| 24 | + | |
| 25 | + <dependencies> | |
| 26 | + <dependency> | |
| 27 | + <groupId>org.slf4j</groupId> | |
| 28 | + <artifactId>slf4j-api</artifactId> | |
| 29 | + <version>${slf4j.version}</version> | |
| 30 | + <scope>provided</scope> | |
| 31 | + </dependency> | |
| 32 | + <dependency> | |
| 33 | + <groupId>com.google.guava</groupId> | |
| 34 | + <artifactId>guava</artifactId> | |
| 35 | + <version>${guava.vsersion}</version> | |
| 36 | + <scope>provided</scope> | |
| 37 | + </dependency> | |
| 38 | + <dependency> | |
| 39 | + <groupId>io.springfox</groupId> | |
| 40 | + <artifactId>springfox-swagger2</artifactId> | |
| 41 | + <version>${swagger.version}</version> | |
| 42 | + <scope>provided</scope> | |
| 43 | + <exclusions> | |
| 44 | + <exclusion> | |
| 45 | + <artifactId>guava</artifactId> | |
| 46 | + <groupId>com.google.guava</groupId> | |
| 47 | + </exclusion> | |
| 48 | + <exclusion> | |
| 49 | + <artifactId>slf4j-api</artifactId> | |
| 50 | + <groupId>org.slf4j</groupId> | |
| 51 | + </exclusion> | |
| 52 | + </exclusions> | |
| 53 | + </dependency> | |
| 54 | + <dependency> | |
| 55 | + <groupId>org.projectlombok</groupId> | |
| 56 | + <artifactId>lombok</artifactId> | |
| 57 | + <version>${lombok.version}</version> | |
| 58 | + <scope>provided</scope> | |
| 59 | + </dependency> | |
| 60 | + <dependency> | |
| 61 | + <groupId>commons-codec</groupId> | |
| 62 | + <artifactId>commons-codec</artifactId> | |
| 63 | + <version>${commons-codec.version}</version> | |
| 64 | + <scope>provided</scope> | |
| 65 | + </dependency> | |
| 66 | + <dependency> | |
| 67 | + <groupId>com.alibaba</groupId> | |
| 68 | + <artifactId>fastjson</artifactId> | |
| 69 | + <version>${fastjson.version}</version> | |
| 70 | + <scope>provided</scope> | |
| 71 | + </dependency> | |
| 72 | + | |
| 73 | + <dependency> | |
| 74 | + <groupId>org.springframework</groupId> | |
| 75 | + <artifactId>spring-web</artifactId> | |
| 76 | + <version>${spring-web.version}</version> | |
| 77 | + <scope>provided</scope> | |
| 78 | + </dependency> | |
| 79 | + | |
| 80 | + <dependency> | |
| 81 | + <groupId>org.springframework.security</groupId> | |
| 82 | + <artifactId>spring-security-core</artifactId> | |
| 83 | + <version>${spring-security-core.version}</version> | |
| 84 | + <scope>provided</scope> | |
| 85 | + </dependency> | |
| 86 | + | |
| 87 | + </dependencies> | |
| 88 | + <build> | |
| 89 | + <plugins> | |
| 90 | + <plugin> | |
| 91 | + <groupId>org.apache.maven.plugins</groupId> | |
| 92 | + <artifactId>maven-compiler-plugin</artifactId> | |
| 93 | + <configuration> | |
| 94 | + <source>1.8</source> | |
| 95 | + <target>1.8</target> | |
| 96 | + <encoding>UTF-8</encoding> | |
| 97 | + </configuration> | |
| 98 | + </plugin> | |
| 99 | + <plugin> | |
| 100 | + <groupId>org.apache.maven.plugins</groupId> | |
| 101 | + <artifactId>maven-source-plugin</artifactId> | |
| 102 | + <version>2.4</version> | |
| 103 | + <configuration> | |
| 104 | + <attach>true</attach> | |
| 105 | + </configuration> | |
| 106 | + <executions> | |
| 107 | + <execution> | |
| 108 | + <phase>deploy</phase> | |
| 109 | + <goals> | |
| 110 | + <goal>jar-no-fork</goal> | |
| 111 | + </goals> | |
| 112 | + </execution> | |
| 113 | + </executions> | |
| 114 | + </plugin> | |
| 115 | + </plugins> | |
| 116 | + </build> | |
| 117 | + <!--上传到私有仓库 --> | |
| 118 | + <distributionManagement> | |
| 119 | + <repository> | |
| 120 | + <id>nexus-releases</id> | |
| 121 | + <url>http://39.108.110.2:8081/repository/maven-releases/</url> | |
| 122 | + </repository> | |
| 123 | + <snapshotRepository> | |
| 124 | + <id>nexus-snapshot</id> | |
| 125 | + <url>http://39.108.110.2:8081/repository/maven-snapshots/</url> | |
| 126 | + </snapshotRepository> | |
| 127 | + </distributionManagement> | |
| 128 | +</project> | |
| 0 | 129 | \ No newline at end of file | ... | ... |
src/main/java/com/irrigation/icl/entity/PageList.java
0 → 100644
| 1 | +package com.irrigation.icl.entity; | |
| 2 | + | |
| 3 | + | |
| 4 | +import io.swagger.annotations.ApiModel; | |
| 5 | +import io.swagger.annotations.ApiModelProperty; | |
| 6 | +import lombok.AllArgsConstructor; | |
| 7 | +import lombok.Data; | |
| 8 | +import lombok.NoArgsConstructor; | |
| 9 | + | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 12 | +@Data | |
| 13 | +@NoArgsConstructor | |
| 14 | +@AllArgsConstructor | |
| 15 | +@ApiModel(value = "分页返回类") | |
| 16 | +public class PageList<E> { | |
| 17 | + | |
| 18 | + @ApiModelProperty(value = "总页数") | |
| 19 | + private int pages; | |
| 20 | + @ApiModelProperty(value = "总条数") | |
| 21 | + private long total; | |
| 22 | + @ApiModelProperty(value = "返回数据") | |
| 23 | + private List<E> list; | |
| 24 | + | |
| 25 | + | |
| 26 | +} | ... | ... |
src/main/java/com/irrigation/icl/entity/PageParam.java
0 → 100644
| 1 | +package com.irrigation.icl.entity; | |
| 2 | + | |
| 3 | +import io.swagger.annotations.ApiModel; | |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 5 | +import lombok.AllArgsConstructor; | |
| 6 | +import lombok.Data; | |
| 7 | +import lombok.NoArgsConstructor; | |
| 8 | + | |
| 9 | +@Data | |
| 10 | +@NoArgsConstructor | |
| 11 | +@AllArgsConstructor | |
| 12 | +@ApiModel(value = "分页参数类") | |
| 13 | +public class PageParam { | |
| 14 | + | |
| 15 | + @ApiModelProperty(value = "当前页") | |
| 16 | + private Integer page = 1; | |
| 17 | + @ApiModelProperty(value = "每页最大条目") | |
| 18 | + private Integer rows = 10; | |
| 19 | +} | ... | ... |
src/main/java/com/irrigation/icl/entity/RestResult.java
0 → 100644
| 1 | +package com.irrigation.icl.entity; | |
| 2 | + | |
| 3 | + | |
| 4 | +import com.alibaba.fastjson.JSON; | |
| 5 | +import com.fasterxml.jackson.annotation.JsonValue; | |
| 6 | +import com.irrigation.icl.enums.ResultEnum; | |
| 7 | +import io.swagger.annotations.ApiModel; | |
| 8 | +import io.swagger.annotations.ApiModelProperty; | |
| 9 | +import lombok.AllArgsConstructor; | |
| 10 | +import lombok.Data; | |
| 11 | +import lombok.NoArgsConstructor; | |
| 12 | + | |
| 13 | +@Data | |
| 14 | +@AllArgsConstructor | |
| 15 | +@NoArgsConstructor | |
| 16 | +@ApiModel(value = "公共返回类") | |
| 17 | +public class RestResult<T> { | |
| 18 | + @ApiModelProperty(value = "返回编码类型") | |
| 19 | + private int code; | |
| 20 | + @ApiModelProperty(value = "返回提示消息") | |
| 21 | + private String message; | |
| 22 | + @ApiModelProperty(value = "返回请求数据对象") | |
| 23 | + private T data; | |
| 24 | + | |
| 25 | + public String getMessage() { | |
| 26 | + return message != null?message: ResultEnum.getCode(code).getMessage(); | |
| 27 | + } | |
| 28 | + public String toString(){ | |
| 29 | + return JSON.toJSONString(this); | |
| 30 | + } | |
| 31 | +} | ... | ... |
src/main/java/com/irrigation/icl/enums/ResultEnum.java
0 → 100644
| 1 | +package com.irrigation.icl.enums; | |
| 2 | + | |
| 3 | +import java.util.Arrays; | |
| 4 | +import java.util.Optional; | |
| 5 | + | |
| 6 | +public enum ResultEnum { | |
| 7 | + | |
| 8 | + SUCCESS(200, "success"), | |
| 9 | + SUCCESS_NO_DATA(202,"查询成功,无数据"), | |
| 10 | + ERROR(400, "failed"), | |
| 11 | + | |
| 12 | + PARAMETER_MISMATCH(402, "参数不匹配!"), | |
| 13 | + | |
| 14 | + INVALID_REQUEST(405, "请求错误!"), | |
| 15 | + | |
| 16 | + HTTP_SERVER_ERROR(500, "服务异常!"), | |
| 17 | + | |
| 18 | + DATA_ACCESS(501, "数据库异常!"), | |
| 19 | + | |
| 20 | + DATA_OPT(502, "数据库操作异常!"), | |
| 21 | + | |
| 22 | + REMOTE_CALL_FAILED(503,"远程调用失败"), | |
| 23 | + | |
| 24 | + SESSION_EXPIRE(401, "会话失效,请重新登录!"); | |
| 25 | + | |
| 26 | + private final int code; | |
| 27 | + private final String message; | |
| 28 | + | |
| 29 | + private ResultEnum(int code, String message) { | |
| 30 | + this.code = code; | |
| 31 | + this.message = message; | |
| 32 | + } | |
| 33 | + | |
| 34 | + | |
| 35 | + public int getCode() { | |
| 36 | + return this.code; | |
| 37 | + } | |
| 38 | + | |
| 39 | + public String getMessage() { | |
| 40 | + | |
| 41 | + return this.message; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public static ResultEnum getCode(int code) { | |
| 45 | + ResultEnum[] codes = ResultEnum.values(); | |
| 46 | + Optional<ResultEnum> optional = Arrays | |
| 47 | + .stream(codes) | |
| 48 | + .filter(t -> t.code == code).findFirst(); | |
| 49 | + return optional.isPresent() ? optional.get() : ResultEnum.ERROR; | |
| 50 | + } | |
| 51 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/AbstractException.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:AbstractException.java | |
| 5 | + * Date:18-3-21 上午10:15 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.exception; | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | +import com.irrigation.icl.exception.code.ErrorCode; | |
| 14 | +import org.springframework.http.HttpStatus; | |
| 15 | + | |
| 16 | +import java.io.PrintWriter; | |
| 17 | +import java.io.StringWriter; | |
| 18 | + | |
| 19 | +public abstract class AbstractException extends RuntimeException { | |
| 20 | + private static final long serialVersionUID = 1L; | |
| 21 | + | |
| 22 | + private final HttpStatus status; | |
| 23 | + | |
| 24 | + private final int code; | |
| 25 | + | |
| 26 | + private final String message; | |
| 27 | + | |
| 28 | + private final String detailMessage; | |
| 29 | + | |
| 30 | + public AbstractException(HttpStatus status, ErrorCode errorCode) { | |
| 31 | + this.status = status; | |
| 32 | + this.code = errorCode.getCode(); | |
| 33 | + this.message = errorCode.getMessage(); | |
| 34 | + this.detailMessage = errorCode.getDetailMessage(); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public AbstractException(HttpStatus status) { | |
| 38 | + this.status = status; | |
| 39 | + code = 0; | |
| 40 | + message = null; | |
| 41 | + detailMessage = null; | |
| 42 | + } | |
| 43 | + | |
| 44 | + public AbstractException(HttpStatus status, ErrorCode errorCode, Throwable cause) { | |
| 45 | + super(cause); | |
| 46 | + this.status = status; | |
| 47 | + this.code = errorCode.getCode(); | |
| 48 | + this.message = errorCode.getMessage(); | |
| 49 | + detailMessage = (cause.getMessage() != null ? cause.getMessage() : "") + toStackTrace(cause); | |
| 50 | + } | |
| 51 | + | |
| 52 | + private String toStackTrace(Throwable e) { | |
| 53 | + StringWriter errorStackTrace = new StringWriter(); | |
| 54 | + e.printStackTrace(new PrintWriter(errorStackTrace)); | |
| 55 | + return errorStackTrace.toString(); | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | + public HttpStatus getStatus() { | |
| 60 | + return status; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public ErrorCode getCode() { | |
| 64 | + return new ErrorCode(code, message, detailMessage); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Override | |
| 68 | + public String getMessage() { | |
| 69 | + return message; | |
| 70 | + } | |
| 71 | + | |
| 72 | + public String getDetailMessage() { | |
| 73 | + return detailMessage; | |
| 74 | + } | |
| 75 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java
0 → 100644
| 1 | +package com.irrigation.icl.exception; | |
| 2 | + | |
| 3 | +public class ContextRuntimeException extends RuntimeException { | |
| 4 | + | |
| 5 | + public ContextRuntimeException(String message) { | |
| 6 | + super(message); | |
| 7 | + } | |
| 8 | + | |
| 9 | + public ContextRuntimeException(String message, Throwable cause) { | |
| 10 | + super(message, cause); | |
| 11 | + } | |
| 12 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/RemoteException.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:RemoteException.java | |
| 5 | + * Date:18-3-21 上午10:15 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.exception; | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | +import com.irrigation.icl.exception.code.ErrorCode; | |
| 14 | + | |
| 15 | +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; | |
| 16 | + | |
| 17 | +public class RemoteException extends AbstractException { | |
| 18 | + | |
| 19 | + public RemoteException() { | |
| 20 | + super(INTERNAL_SERVER_ERROR); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public RemoteException(ErrorCode errorCode) { | |
| 24 | + super(INTERNAL_SERVER_ERROR, errorCode); | |
| 25 | + } | |
| 26 | + | |
| 27 | + public RemoteException(ErrorCode errorCode, Throwable cause) { | |
| 28 | + super(INTERNAL_SERVER_ERROR, errorCode, cause); | |
| 29 | + } | |
| 30 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/code/ErrorCode.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:ErrorCode.java | |
| 5 | + * Date:18-3-21 上午10:15 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.exception.code; | |
| 10 | + | |
| 11 | +public class ErrorCode { | |
| 12 | + | |
| 13 | + private final int code; | |
| 14 | + private final String message; | |
| 15 | + private final String detailMessage; | |
| 16 | + | |
| 17 | + public ErrorCode(int code, String message, String detailMessage) { | |
| 18 | + this.code = code; | |
| 19 | + this.message = message; | |
| 20 | + this.detailMessage = detailMessage; | |
| 21 | + } | |
| 22 | + | |
| 23 | + public ErrorCode(String message, String detailMessage) { | |
| 24 | + this.code = 0; | |
| 25 | + this.message = message; | |
| 26 | + this.detailMessage = detailMessage; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public int getCode() { | |
| 30 | + return code; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public String getMessage() { | |
| 34 | + return message; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public String getDetailMessage() { | |
| 38 | + return detailMessage; | |
| 39 | + } | |
| 40 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/code/ErrorCodes.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:ErrorCodes.java | |
| 5 | + * Date:18-3-21 上午10:15 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.exception.code; | |
| 10 | + | |
| 11 | + | |
| 12 | +public class ErrorCodes { | |
| 13 | + | |
| 14 | + private ErrorCodes(){} | |
| 15 | + public static final String CLIENT_ID_IS_NULL_STR = "CLIENT_ID_IS_NULL"; | |
| 16 | + public static final String CLIENT_SECRET_IS_NULL_STR = "CLIENT_SECRET_IS_NULL"; | |
| 17 | + | |
| 18 | + public static final String ORGANIZAITON_ID_IS_NULL_STR = "ORGANIZAITON_ID_IS_NULL"; | |
| 19 | + | |
| 20 | + /* 500 Internal Server Error */ | |
| 21 | + | |
| 22 | + public static final ErrorCode DEVICE_ID_IS_NULL_STR = new ErrorCode("DEVICE_ID_IS_NULL_STR", "设备 ID 非法"); | |
| 23 | + public static final ErrorCode NOT_LOGIN_YET = new ErrorCode("NOT_LOGIN_YET", "没有用户已登录当前设备"); | |
| 24 | + | |
| 25 | + public static final ErrorCode USER_SERVICE_CALL_FAILURE = new ErrorCode("USER_SERVICE_CALL_FAILURE", "无法调用远程用户系统,或调用失败"); | |
| 26 | + public static final ErrorCode ID_SERVICE_CALL_FAILURE = new ErrorCode("ID_SERVICE_CALL_FAILURE", "无法调用远程用ID_SERVICE,或调用失败"); | |
| 27 | + | |
| 28 | + public static final ErrorCode INVALID_AUTHENTICATION_ERROR = new ErrorCode("INVALID_AUTHENTICATION", "用户证书校验失败"); | |
| 29 | + | |
| 30 | +} | ... | ... |
src/main/java/com/irrigation/icl/exception/code/GenericErrorCodes.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:GenericErrorCodes.java | |
| 5 | + * Date:18-3-21 上午10:15 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.exception.code; | |
| 10 | + | |
| 11 | +public class GenericErrorCodes { | |
| 12 | + public static ErrorCode GENERIC_API_ERROR_CODE = | |
| 13 | + new ErrorCode(0001, "GENERIC_API_ERROR_CODE", "generic API error message"); | |
| 14 | + public static ErrorCode GENERIC_UNAUTHORIZED_ERROR_CODE = | |
| 15 | + new ErrorCode(0002, "GENERIC_UNAUTHORIZED_ERROR_CODE", "generic unauthorized error message"); | |
| 16 | + public static ErrorCode DATA_ACCESS_ERROR_CODE = | |
| 17 | + new ErrorCode(0003, "DATA_ACCESS_ERROR", "database access error"); | |
| 18 | +} | ... | ... |
src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | +import org.springframework.security.authentication.encoding.BasePasswordEncoder; | |
| 4 | +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; | |
| 5 | + | |
| 6 | +public class PasswordEncoderUtil { | |
| 7 | + | |
| 8 | + private final static BasePasswordEncoder encoder = new Md5PasswordEncoder(); | |
| 9 | + | |
| 10 | + public static String encodePassword(String password) { | |
| 11 | + return encoder.encodePassword(password, null); | |
| 12 | + } | |
| 13 | + | |
| 14 | + public static boolean isPasswordValid(String rawPassword, String encodedPassword) { | |
| 15 | + return encoder.isPasswordValid(encodedPassword, rawPassword, null); | |
| 16 | + } | |
| 17 | + | |
| 18 | +} | ... | ... |
src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | + | |
| 4 | +import com.irrigation.icl.entity.RestResult; | |
| 5 | +import com.irrigation.icl.enums.ResultEnum; | |
| 6 | + | |
| 7 | +public class RestResultGeneratorUtil { | |
| 8 | + | |
| 9 | + public static <T> RestResult<T> getResult(int code, T data, String message) { | |
| 10 | + RestResult<T> result = new RestResult<>(code, message, data); | |
| 11 | + return result; | |
| 12 | + } | |
| 13 | + | |
| 14 | + public static <T> RestResult<T> getSuccessResult(T data) { | |
| 15 | + return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); | |
| 16 | + } | |
| 17 | + | |
| 18 | + public static RestResult<String> getSuccessResult() { | |
| 19 | + return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); | |
| 20 | + } | |
| 21 | + | |
| 22 | + public static RestResult<String> getErrorResult(String message) { | |
| 23 | + return getResult(ResultEnum.ERROR.getCode(), null, message); | |
| 24 | + } | |
| 25 | + | |
| 26 | + public static RestResult<String> getErrorResult(int code, String message) { | |
| 27 | + return getResult(code, null, message); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public static RestResult<String> getErrorResult(ResultEnum resultEnum) { | |
| 31 | + return getResult(resultEnum.getCode(), null, resultEnum.getMessage()); | |
| 32 | + } | |
| 33 | +} | ... | ... |
src/main/java/com/irrigation/icl/utils/SignUtil.java
0 → 100644
| 1 | +/* | |
| 2 | + * Copyright (c) 2018. | |
| 3 | + * 项目名称:auth-gateway-backend | |
| 4 | + * 文件名称:SignUtil.java | |
| 5 | + * Date:18-3-23 下午3:34 | |
| 6 | + * Author:boni | |
| 7 | + */ | |
| 8 | + | |
| 9 | +package com.irrigation.icl.utils; | |
| 10 | + | |
| 11 | +import org.apache.commons.codec.digest.DigestUtils; | |
| 12 | + | |
| 13 | +import java.text.SimpleDateFormat; | |
| 14 | +import java.util.Arrays; | |
| 15 | +import java.util.Date; | |
| 16 | +import java.util.Map; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * 签名方法 | |
| 20 | + */ | |
| 21 | +public class SignUtil { | |
| 22 | + /** | |
| 23 | + * 使用apiSecret对params进行签名,自动加入了当前日期 | |
| 24 | + * @param params | |
| 25 | + * @param apiSecret | |
| 26 | + * @return | |
| 27 | + */ | |
| 28 | + public static String sign(Map<String, String> params, String apiSecret) { | |
| 29 | + String[] sortParams = params.keySet().toArray(new String[]{}); | |
| 30 | + Arrays.sort(sortParams); | |
| 31 | + StringBuilder stringBuilder = new StringBuilder(); | |
| 32 | + for (String key : sortParams) { | |
| 33 | + stringBuilder.append(key).append(params.get(key)); | |
| 34 | + } | |
| 35 | + String signRawData = apiSecret + ":" + getDate() + ":" + stringBuilder.toString(); | |
| 36 | + String signedValue = DigestUtils.md5Hex(signRawData); | |
| 37 | + return signedValue; | |
| 38 | + } | |
| 39 | + | |
| 40 | + private static String getDate() { | |
| 41 | + Date date = new Date(); | |
| 42 | + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
| 43 | + String sDateSuffix = dateFormat.format(date); | |
| 44 | + return sDateSuffix; | |
| 45 | + } | |
| 46 | +} | ... | ... |