first commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?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.wanji.software</groupId>
|
||||
<artifactId>maint-dev</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>maint-support</artifactId>
|
||||
<description>本地开发支撑,交回代码时不包含本模块</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.wanji.software.tocc.common.exception;
|
||||
|
||||
/**
|
||||
* 业务异常。包名与公司工程一致,交回后可直接换成公司 common。
|
||||
*/
|
||||
public class BusinessException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Integer code;
|
||||
|
||||
public BusinessException(int code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(String message) {
|
||||
this(1, message);
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.wanji.software.tocc.common.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.wanji.software.tocc.common.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 接口返回体。包名与公司工程一致,业务代码交回后不用改 import。
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Result<T> implements Serializable {
|
||||
|
||||
private T data;
|
||||
private Integer code;
|
||||
private String msg;
|
||||
|
||||
public static <T> Result<T> ok() {
|
||||
return of(null, 0, "操作成功");
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(T data) {
|
||||
return of(data, 0, "操作成功");
|
||||
}
|
||||
|
||||
public static <T> Result<T> ok(T data, String msg) {
|
||||
return of(data, 0, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail() {
|
||||
return of(null, 1, "操作失败");
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(String msg) {
|
||||
return of(null, 1, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> fail(int code, String msg) {
|
||||
return of(null, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> of(T data, Integer code, String msg) {
|
||||
return new Result<T>(data, code, msg);
|
||||
}
|
||||
|
||||
public static boolean isSuccess(Integer code) {
|
||||
return Objects.equals(code, 0);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isSuccess() {
|
||||
return isSuccess(code);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isError() {
|
||||
return !isSuccess();
|
||||
}
|
||||
|
||||
public void checkError() throws BusinessException {
|
||||
if (isSuccess()) {
|
||||
return;
|
||||
}
|
||||
throw new BusinessException(code == null ? 1 : code, msg);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public T getCheckedData() {
|
||||
checkError();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.wanji.software.tocc.maint.support;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
/**
|
||||
* 当前登录用户。本地开发由过滤器填入固定用户,并入公司环境后改为网关传入的用户。
|
||||
*/
|
||||
@Value
|
||||
@Builder
|
||||
public class CurrentUser {
|
||||
|
||||
String userId;
|
||||
String userName;
|
||||
String orgId;
|
||||
String orgName;
|
||||
|
||||
public static CurrentUser localDevUser() {
|
||||
return CurrentUser.builder()
|
||||
.userId("dev")
|
||||
.userName("养护开发")
|
||||
.orgId("dev-org")
|
||||
.orgName("师宗公路分局")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.wanji.software.tocc.maint.support;
|
||||
|
||||
/**
|
||||
* 当前请求的用户。业务代码用 {@link #get()} 取创建人、机构,不要自己写死用户。
|
||||
*/
|
||||
public final class CurrentUserHolder {
|
||||
|
||||
private static final ThreadLocal<CurrentUser> HOLDER = new ThreadLocal<CurrentUser>();
|
||||
|
||||
private CurrentUserHolder() {
|
||||
}
|
||||
|
||||
public static CurrentUser get() {
|
||||
CurrentUser current = HOLDER.get();
|
||||
return current == null ? CurrentUser.localDevUser() : current;
|
||||
}
|
||||
|
||||
public static void set(CurrentUser currentUser) {
|
||||
HOLDER.set(currentUser);
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
HOLDER.remove();
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.wanji.software.tocc.maint.support;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
/**
|
||||
* 本地开发用户。没有请求头时使用固定养护用户,有请求头时沿用上游传来的用户。
|
||||
*/
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class LocalDevAuthFilter extends OncePerRequestFilter {
|
||||
|
||||
public static final String USER_ID_HEADER = "x-userid";
|
||||
public static final String USER_NAME_HEADER = "x-username";
|
||||
public static final String ORG_ID_HEADER = "x-orgid";
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
CurrentUserHolder.set(resolve(request));
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
CurrentUserHolder.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private CurrentUser resolve(HttpServletRequest request) {
|
||||
String userId = request.getHeader(USER_ID_HEADER);
|
||||
if (!StringUtils.hasText(userId)) {
|
||||
return CurrentUser.localDevUser();
|
||||
}
|
||||
CurrentUser local = CurrentUser.localDevUser();
|
||||
return CurrentUser.builder()
|
||||
.userId(userId)
|
||||
.userName(decode(request.getHeader(USER_NAME_HEADER), local.getUserName()))
|
||||
.orgId(defaultText(request.getHeader(ORG_ID_HEADER), local.getOrgId()))
|
||||
.orgName(local.getOrgName())
|
||||
.build();
|
||||
}
|
||||
|
||||
private String decode(String value, String fallback) {
|
||||
if (!StringUtils.hasText(value)) {
|
||||
return fallback;
|
||||
}
|
||||
try {
|
||||
return URLDecoder.decode(value, "UTF-8");
|
||||
} catch (IOException ex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private String defaultText(String value, String fallback) {
|
||||
return StringUtils.hasText(value) ? value : fallback;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.wanji.software.tocc.maint.support;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页结果。
|
||||
*/
|
||||
@Data
|
||||
public class PageResult<T> {
|
||||
|
||||
private long total;
|
||||
private long pageNo;
|
||||
private long pageSize;
|
||||
private List<T> records = Collections.emptyList();
|
||||
}
|
||||
Reference in New Issue
Block a user