6 changed files with 184 additions and 10 deletions
-
36src/main/java/com/gaotao/common/utils/LoginUserContextUtils.java
-
114src/main/java/com/gaotao/config/MybatisLoginUserInterceptor.java
-
3src/main/java/com/gaotao/config/MybatisPlusConfig.java
-
11src/main/java/com/gaotao/modules/handlingunit/controller/PdaLabelController.java
-
15src/main/resources/mapper/procurement/PoReceiptMapper.xml
-
15src/main/resources/mapper/warehouse/LabelQueryMapper.xml
@ -0,0 +1,36 @@ |
|||
package com.gaotao.common.utils; |
|||
|
|||
import com.gaotao.modules.sys.entity.SysUserEntity; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.apache.shiro.SecurityUtils; |
|||
|
|||
/** |
|||
* 登录人上下文工具 |
|||
* 统一提供当前会话登录名,供 MyBatis 权限注入与业务代码复用。 |
|||
*/ |
|||
public final class LoginUserContextUtils { |
|||
|
|||
private LoginUserContextUtils() { |
|||
} |
|||
|
|||
/** |
|||
* 获取当前登录名(access_site.user_id 对应字段)。 |
|||
* 无登录上下文(如定时任务)时返回 null。 |
|||
*/ |
|||
public static String getCurrentLoginAccount() { |
|||
try { |
|||
Object principal = SecurityUtils.getSubject().getPrincipal(); |
|||
if (principal instanceof SysUserEntity) { |
|||
String principalName = ((SysUserEntity) principal).getUsername(); |
|||
return StringUtils.isBlank(principalName) ? null : principalName.trim(); |
|||
} |
|||
if (principal instanceof String) { |
|||
String principalName = (String) principal; |
|||
return StringUtils.isBlank(principalName) ? null : principalName.trim(); |
|||
} |
|||
} catch (Exception ignored) { |
|||
// 定时任务/匿名访问等场景没有登录态,按 null 处理 |
|||
} |
|||
return null; |
|||
} |
|||
} |
|||
@ -0,0 +1,114 @@ |
|||
package com.gaotao.config; |
|||
|
|||
import com.gaotao.common.utils.LoginUserContextUtils; |
|||
import org.apache.ibatis.cache.CacheKey; |
|||
import org.apache.ibatis.executor.Executor; |
|||
import org.apache.ibatis.executor.statement.StatementHandler; |
|||
import org.apache.ibatis.mapping.BoundSql; |
|||
import org.apache.ibatis.mapping.MappedStatement; |
|||
import org.apache.ibatis.plugin.Interceptor; |
|||
import org.apache.ibatis.plugin.Intercepts; |
|||
import org.apache.ibatis.plugin.Invocation; |
|||
import org.apache.ibatis.plugin.Plugin; |
|||
import org.apache.ibatis.plugin.Signature; |
|||
import org.apache.ibatis.reflection.MetaObject; |
|||
import org.apache.ibatis.reflection.SystemMetaObject; |
|||
import org.apache.ibatis.session.ResultHandler; |
|||
import org.apache.ibatis.session.RowBounds; |
|||
import org.springframework.core.Ordered; |
|||
import org.springframework.core.annotation.Order; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.sql.Connection; |
|||
import java.util.Map; |
|||
import java.util.Properties; |
|||
|
|||
/** |
|||
* 统一注入当前登录名,供 Mapper 直接使用 currentLoginUserName。 |
|||
*/ |
|||
@Component |
|||
@Order(Ordered.LOWEST_PRECEDENCE) |
|||
@Intercepts({ |
|||
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), |
|||
@Signature(type = Executor.class, method = "query", args = { |
|||
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class |
|||
}), |
|||
@Signature(type = Executor.class, method = "query", args = { |
|||
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class |
|||
}), |
|||
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) |
|||
}) |
|||
public class MybatisLoginUserInterceptor implements Interceptor { |
|||
|
|||
private static final String LOGIN_USER_PARAM = "currentLoginUserName"; |
|||
|
|||
@Override |
|||
public Object intercept(Invocation invocation) throws Throwable { |
|||
String loginUserName = LoginUserContextUtils.getCurrentLoginAccount(); |
|||
Object target = invocation.getTarget(); |
|||
if (target instanceof Executor) { |
|||
injectToParameterObject(invocation.getArgs(), loginUserName); |
|||
} else if (target instanceof StatementHandler) { |
|||
injectToBoundSql((StatementHandler) target, loginUserName); |
|||
} |
|||
return invocation.proceed(); |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private void injectToParameterObject(Object[] args, String loginUserName) { |
|||
if (args == null || args.length < 2) { |
|||
return; |
|||
} |
|||
Object parameterObject = args[1]; |
|||
if (parameterObject == null) { |
|||
return; |
|||
} |
|||
|
|||
if (parameterObject instanceof Map) { |
|||
Map<String, Object> paramMap = (Map<String, Object>) parameterObject; |
|||
if (!paramMap.containsKey(LOGIN_USER_PARAM)) { |
|||
paramMap.put(LOGIN_USER_PARAM, loginUserName); |
|||
} |
|||
if (paramMap.containsKey("params")) { |
|||
Object paramsObject = paramMap.get("params"); |
|||
if (paramsObject != null) { |
|||
injectToBeanOrMap(paramsObject, loginUserName); |
|||
} |
|||
} |
|||
return; |
|||
} |
|||
injectToBeanOrMap(parameterObject, loginUserName); |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
private void injectToBeanOrMap(Object bean, String loginUserName) { |
|||
if (bean instanceof Map) { |
|||
Map<String, Object> mapBean = (Map<String, Object>) bean; |
|||
if (!mapBean.containsKey(LOGIN_USER_PARAM)) { |
|||
mapBean.put(LOGIN_USER_PARAM, loginUserName); |
|||
} |
|||
return; |
|||
} |
|||
MetaObject metaObject = SystemMetaObject.forObject(bean); |
|||
if (metaObject.hasSetter(LOGIN_USER_PARAM) && metaObject.getValue(LOGIN_USER_PARAM) == null) { |
|||
metaObject.setValue(LOGIN_USER_PARAM, loginUserName); |
|||
} |
|||
} |
|||
|
|||
private void injectToBoundSql(StatementHandler statementHandler, String loginUserName) { |
|||
BoundSql boundSql = statementHandler.getBoundSql(); |
|||
if (boundSql != null && !boundSql.hasAdditionalParameter(LOGIN_USER_PARAM)) { |
|||
boundSql.setAdditionalParameter(LOGIN_USER_PARAM, loginUserName); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public Object plugin(Object target) { |
|||
return Plugin.wrap(target, this); |
|||
} |
|||
|
|||
@Override |
|||
public void setProperties(Properties properties) { |
|||
// no-op |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue