租户id切面

This commit is contained in:
ZZ 2022-01-10 16:52:46 +08:00
parent b334803cd1
commit b0023784ee
8 changed files with 511 additions and 5 deletions

View File

@ -0,0 +1,160 @@
package com.xhpc.charging.station;
import com.xhpc.common.core.constant.CacheConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.system.api.model.LoginUser;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/*
* TODO AO切面插入创建人创建时间修改人修改时间
* @author fjd
* @date 2020-07-09 11:59
*/
@Aspect
@Component
@Configuration
public class DaoAspect {
private static final String CREATE_USER = "createBy";
private static final String CREATE_TIME = "createTime";
private static final String UPDATE_USER = "updateBy";
private static final String UPDATE_TIME = "updateTime";
private static final String TENANT_ID = "tenantId";
@Pointcut("execution(* com.xhpc..*.update*(..))")
public void daoUpdate() {
}
@Pointcut("execution(* com.xhpc..*.insert*(..))")
public void daoCreate() {
}
@Autowired
private RedisService redisService;
@Around("daoUpdate()")
public Object doAroundUpdate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
if (userName != null) {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, new Date());
}
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
@Around("daoCreate()")
public Object doAroundCreate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
Date date = new Date();
if (isProperty(arg, CREATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_USER))) {
BeanUtils.setProperty(arg, CREATE_USER, userName);
}
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, CREATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_TIME))) {
BeanUtils.setProperty(arg, CREATE_TIME, date);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
public static boolean isProperty(Object bean, String field) {
return StringUtils.isProperty(bean, field);
}
}

View File

@ -0,0 +1,160 @@
package com.xhpc.general;
import com.xhpc.common.core.constant.CacheConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.system.api.model.LoginUser;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/*
* TODO AO切面插入创建人创建时间修改人修改时间
* @author fjd
* @date 2020-07-09 11:59
*/
@Aspect
@Component
@Configuration
public class DaoAspect {
private static final String CREATE_USER = "createBy";
private static final String CREATE_TIME = "createTime";
private static final String UPDATE_USER = "updateBy";
private static final String UPDATE_TIME = "updateTime";
private static final String TENANT_ID = "tenantId";
@Pointcut("execution(* com.xhpc..*.update*(..))")
public void daoUpdate() {
}
@Pointcut("execution(* com.xhpc..*.insert*(..))")
public void daoCreate() {
}
@Autowired
private RedisService redisService;
@Around("daoUpdate()")
public Object doAroundUpdate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
if (userName != null) {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, new Date());
}
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
@Around("daoCreate()")
public Object doAroundCreate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
Date date = new Date();
if (isProperty(arg, CREATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_USER))) {
BeanUtils.setProperty(arg, CREATE_USER, userName);
}
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, CREATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_TIME))) {
BeanUtils.setProperty(arg, CREATE_TIME, date);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
public static boolean isProperty(Object bean, String field) {
return StringUtils.isProperty(bean, field);
}
}

View File

@ -0,0 +1,160 @@
package com.xhpc.invoice.aspect;
import com.xhpc.common.core.constant.CacheConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.system.api.model.LoginUser;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/*
* TODO AO切面插入创建人创建时间修改人修改时间
* @author fjd
* @date 2020-07-09 11:59
*/
@Aspect
@Component
@Configuration
public class DaoAspect {
private static final String CREATE_USER = "createBy";
private static final String CREATE_TIME = "createTime";
private static final String UPDATE_USER = "updateBy";
private static final String UPDATE_TIME = "updateTime";
private static final String TENANT_ID = "tenantId";
@Pointcut("execution(* com.xhpc..*.update*(..))")
public void daoUpdate() {
}
@Pointcut("execution(* com.xhpc..*.insert*(..))")
public void daoCreate() {
}
@Autowired
private RedisService redisService;
@Around("daoUpdate()")
public Object doAroundUpdate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
if (userName != null) {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, new Date());
}
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
@Around("daoCreate()")
public Object doAroundCreate(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return point.proceed();
}
HttpServletRequest request = attributes.getRequest();
try {
Object[] objects = point.getArgs();
if (objects != null && objects.length > 0) {
for (Object arg : objects) {
String token = request.getHeader(CacheConstants.HEADER);
token = token.substring(7);
LoginUser loginUser = new LoginUser();
try {
loginUser = redisService.getCacheObject(CacheConstants.LOGIN_TOKEN_KEY + token);
} catch (Exception e) {
}
String userName = "";
try {
userName = SecurityUtils.getUsername();
} catch (Exception e) {
}
if (StringUtils.isEmpty(userName)) {
userName = loginUser.getUsername();
}
if (StringUtils.isEmpty(userName)) {
userName = "admin";
}
Date date = new Date();
if (isProperty(arg, CREATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_USER))) {
BeanUtils.setProperty(arg, CREATE_USER, userName);
}
if (isProperty(arg, UPDATE_USER) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_USER))) {
BeanUtils.setProperty(arg, UPDATE_USER, userName);
}
if (isProperty(arg, CREATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, CREATE_TIME))) {
BeanUtils.setProperty(arg, CREATE_TIME, date);
}
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {
}
Object object = point.proceed();
return object;
}
public static boolean isProperty(Object bean, String field) {
return StringUtils.isProperty(bean, field);
}
}

View File

@ -159,6 +159,8 @@ public class XhpcInvoice implements Serializable {
*/
private Integer delFlag;
private String tenantId;
private static final long serialVersionUID = 1L;
}
}

View File

@ -261,7 +261,7 @@
creator_id, creator_type, creator,
create_time, `status`, invoicing_time,
drawer, finance_notes, electric_invoice_url,
updator, update_time, del_flag)
updator, update_time, del_flag, tenant_id)
values (#{receiveEmail,jdbcType=VARCHAR}, #{titleType,jdbcType=INTEGER}, #{titleContent,jdbcType=VARCHAR},
#{dutyNumber,jdbcType=VARCHAR}, #{invoiceContent,jdbcType=VARCHAR}, #{invoiceMoney,jdbcType=DECIMAL},
#{invoiceOrderEletricTotalMoney,jdbcType=DECIMAL}, #{invoiceOrderServiceTotalMoney,jdbcType=DECIMAL},
@ -270,7 +270,8 @@
#{creatorId,jdbcType=BIGINT}, #{creatorType,jdbcType=INTEGER}, #{creator,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{status,jdbcType=INTEGER}, #{invoicingTime,jdbcType=TIMESTAMP},
#{drawer,jdbcType=VARCHAR}, #{financeNotes,jdbcType=VARCHAR}, #{electricInvoiceUrl,jdbcType=VARCHAR},
#{updator,jdbcType=BIGINT}, #{updateTime,jdbcType=DATE}, #{delFlag,jdbcType=INTEGER})
#{updator,jdbcType=BIGINT}, #{updateTime,jdbcType=DATE}, #{delFlag,jdbcType=INTEGER},
#{tenantId,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="invoice_id" keyProperty="invoiceId"
parameterType="com.xhpc.invoice.pojo.XhpcInvoice" useGeneratedKeys="true">
@ -354,6 +355,9 @@
<if test="delFlag != null">
del_flag,
</if>
<if test="tenantId != null">
tenant_id
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="receiveEmail != null">
@ -434,6 +438,9 @@
<if test="delFlag != null">
#{delFlag,jdbcType=INTEGER},
</if>
<if test="tenantId != null">
#{tenantId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<insert id="insertSelectiveAndReturnId" parameterType="com.xhpc.invoice.pojo.XhpcInvoice"
@ -518,6 +525,9 @@
<if test="delFlag != null">
del_flag,
</if>
<if test="tenantId != null">
tenant_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="receiveEmail != null">
@ -598,6 +608,9 @@
<if test="delFlag != null">
#{delFlag,jdbcType=INTEGER},
</if>
<if test="tenantId != null">
#{tenantId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.xhpc.invoice.pojo.XhpcInvoice">
@ -749,4 +762,4 @@
WHERE invoice_id = #{invoiceId};
</update>
</mapper>
</mapper>

View File

@ -33,6 +33,7 @@ public class DaoAspect {
private static final String CREATE_TIME = "createTime";
private static final String UPDATE_USER = "updateBy";
private static final String UPDATE_TIME = "updateTime";
private static final String TENANT_ID = "tenantId";
@Pointcut("execution(* com.xhpc..*.update*(..))")
public void daoUpdate() {
@ -134,6 +135,10 @@ public class DaoAspect {
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {

View File

@ -33,6 +33,7 @@ public class DaoAspect {
private static final String CREATE_TIME = "createTime";
private static final String UPDATE_USER = "updateBy";
private static final String UPDATE_TIME = "updateTime";
private static final String TENANT_ID = "tenantId";
@Pointcut("execution(* com.xhpc..*.update*(..))")
public void daoUpdate() {
@ -135,6 +136,9 @@ public class DaoAspect {
if (isProperty(arg, UPDATE_TIME) && StringUtils.isEmpty(BeanUtils.getProperty(arg, UPDATE_TIME))) {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {

View File

@ -121,7 +121,9 @@ public class UserDaoAspect {
BeanUtils.setProperty(arg, UPDATE_TIME, date);
}
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
if (isProperty(arg, TENANT_ID) && StringUtils.isEmpty(BeanUtils.getProperty(arg, TENANT_ID))) {
BeanUtils.setProperty(arg, TENANT_ID, loginUser.getTenantId());
}
}
}
} catch (Exception e) {