From 8270ea505cb234a1d0b79e3a0f1e1f376b240046 Mon Sep 17 00:00:00 2001 From: lsh <lsh@163.com> Date: 星期二, 11 六月 2024 10:07:33 +0800 Subject: [PATCH] # --- src/main/java/com/zy/system/entity/UserLogin.java | 16 +++++ src/main/java/com/zy/common/web/AuthController.java | 1 src/main/java/com/zy/common/config/AspectConfig.java | 110 ++++++++++++++++++++++++++++++++++++ src/main/java/com/zy/common/config/AdminInterceptor.java | 6 + src/main/webapp/static/js/userLogin/userLogin.js | 1 src/main/resources/mapper/UserLoginMapper.xml | 2 src/main/java/com/zy/system/entity/OperateLog.java | 4 + pom.xml | 4 + src/main/resources/application.yml | 2 9 files changed, 143 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 39b2e35..6151db3 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,10 @@ <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-aop</artifactId> + </dependency> + <dependency> <groupId>cn.cool</groupId> <artifactId>framework</artifactId> <version>${cool.version}</version> diff --git a/src/main/java/com/zy/common/config/AdminInterceptor.java b/src/main/java/com/zy/common/config/AdminInterceptor.java index 3932e07..1bc1838 100644 --- a/src/main/java/com/zy/common/config/AdminInterceptor.java +++ b/src/main/java/com/zy/common/config/AdminInterceptor.java @@ -106,7 +106,11 @@ private boolean check(HttpServletRequest request, HttpServletResponse response, String memo) { try { - String token = request.getHeader("token"); + String token = new String(); + token = request.getHeader("token"); + if (Cools.isEmpty(token)){ + return true; + } UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("token", token)); if (null == userLogin){ Http.response(response, BaseRes.DENIED); diff --git a/src/main/java/com/zy/common/config/AspectConfig.java b/src/main/java/com/zy/common/config/AspectConfig.java new file mode 100644 index 0000000..780eb46 --- /dev/null +++ b/src/main/java/com/zy/common/config/AspectConfig.java @@ -0,0 +1,110 @@ +package com.zy.common.config; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.core.annotations.ManagerAuth; +import com.core.common.Cools; +import com.zy.system.entity.OperateLog; +import com.zy.system.entity.User; +import com.zy.system.entity.UserLogin; +import com.zy.system.service.OperateLogService; +import com.zy.system.service.UserLoginService; +import com.zy.system.service.UserService; +import lombok.extern.slf4j.Slf4j; +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.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@Component +@Aspect +@Slf4j +public class AspectConfig { + + @Autowired + private UserLoginService userLoginService; + @Autowired + private UserService userService; + @Autowired + private OperateLogService operateLogService; + + @Pointcut("execution(* com.zy.asrs.controller.*(..))") + private void webLog() { + } + + @Around("@within(org.springframework.web.bind.annotation.RestController)" + + "||@within(org.springframework.stereotype.Controller)") + public Object after(ProceedingJoinPoint joinPoint) throws Throwable{ + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + //if(requestAttributes != null){ + // return joinPoint.proceed(); + //} + HttpServletRequest request = requestAttributes.getRequest(); + long start = System.currentTimeMillis(); + Object result = joinPoint.proceed(joinPoint.getArgs()); + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + if (method.isAnnotationPresent(ManagerAuth.class)){ + ManagerAuth annotation = method.getAnnotation(ManagerAuth.class); + if (annotation.value().equals(ManagerAuth.Auth.CHECK)){ + if (!Cools.isEmpty(annotation.memo())) { + saveLog(joinPoint, request, result,annotation.memo()); + } + } + } + + long end = System.currentTimeMillis(); + +// log.info("璇锋眰鏃ュ織鐨勬墦鍗�"); +// log.info("璇锋眰鍦板潃:{}", Optional.ofNullable(request.getRequestURI().toString()).orElse(null)); +// log.info("璇锋眰鏂瑰紡:{}",request.getMethod()); +// log.info("璇锋眰绫绘柟娉�:{}",joinPoint.getSignature()); +// log.info("璇锋眰绫绘柟娉曞弬鏁�:{}", JSONObject.toJSONString(filterArgs(joinPoint.getArgs()))); +// log.info("璇锋眰鍝嶅簲鍙傛暟{}", JSONObject.toJSONString(result)); +// log.info("鎵ц鑰楁椂:{}", end - start); + + return result; + } + + private List<Object> filterArgs(Object[] objects) { + return Arrays.stream(objects).filter(obj -> !(obj instanceof MultipartFile) + && !(obj instanceof HttpServletResponse) + && !(obj instanceof HttpServletRequest)).collect(Collectors.toList()); + } + + private void saveLog(ProceedingJoinPoint joinPoint, HttpServletRequest request, Object result,String memo) { + Long userId = 9527L; + String token = request.getHeader("token"); + UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("token", token).eq("system", "WMS")); + if (userLogin != null) { + User user = userService.selectById(userLogin.getUserId()); + if (user != null) { + userId = user.getId(); + } + } + + // 璁板綍鎿嶄綔鏃ュ織 + OperateLog operateLog = new OperateLog(); + operateLog.setAction(Cools.isEmpty(memo)?request.getRequestURI():memo); + operateLog.setIp(request.getRemoteAddr()); + operateLog.setUserId(userId); + operateLog.setRequest(JSONObject.toJSONString(filterArgs(joinPoint.getArgs()))); + operateLog.setResponse(JSONObject.toJSONString(result)); + operateLogService.insert(operateLog); + } + +} \ No newline at end of file diff --git a/src/main/java/com/zy/common/web/AuthController.java b/src/main/java/com/zy/common/web/AuthController.java index c8bca4a..b85c56a 100644 --- a/src/main/java/com/zy/common/web/AuthController.java +++ b/src/main/java/com/zy/common/web/AuthController.java @@ -80,6 +80,7 @@ userLogin.setUserId(user.getId()); userLogin.setToken(token); userLogin.setCreateTime(new Date()); + userLogin.setSystem("WMS"); userLoginService.insert(userLogin); Map<String, Object> res = new HashMap<>(); res.put("username", user.getUsername()); diff --git a/src/main/java/com/zy/system/entity/OperateLog.java b/src/main/java/com/zy/system/entity/OperateLog.java index abee522..56b86cb 100644 --- a/src/main/java/com/zy/system/entity/OperateLog.java +++ b/src/main/java/com/zy/system/entity/OperateLog.java @@ -80,8 +80,10 @@ User user = service.selectById(this.userId); if (!Cools.isEmpty(user)){ return user.getUsername(); + } else if (this.userId==9527L){ + return "鏈煡"; } - return null; + return "鏈煡"; } public void setUserId(Long userId) { this.userId = userId; diff --git a/src/main/java/com/zy/system/entity/UserLogin.java b/src/main/java/com/zy/system/entity/UserLogin.java index 9c11c8d..a585ee8 100644 --- a/src/main/java/com/zy/system/entity/UserLogin.java +++ b/src/main/java/com/zy/system/entity/UserLogin.java @@ -8,12 +8,14 @@ import com.core.common.Cools; import com.core.common.SpringUtils; import io.swagger.annotations.ApiModelProperty; +import lombok.Data; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; @TableName("sys_user_login") +@Data public class UserLogin implements Serializable { private static final long serialVersionUID = 1L; @@ -30,6 +32,7 @@ @ApiModelProperty(value= "鎵�灞為」鐩�") @TableField("host_id") private Long hostId; + /** * 鍛樺伐 */ @@ -47,6 +50,12 @@ @TableField("create_time") private Date createTime; + /** + * 鐧诲綍绯荤粺 + */ + @TableField("system") + private String system; + public Long getId() { return id; } @@ -62,6 +71,7 @@ public void setHostId(Long hostId) { this.hostId = hostId; } + public Long getUserId() { return userId; } @@ -101,5 +111,11 @@ this.createTime = createTime; } + public String getSystem() { + return system; + } + public void setSystem(String system) { + this.system = system; + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fbb0adb..b41e798 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,7 +10,7 @@ enabled: false datasource: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver - url: jdbc:sqlserver://127.0.0.1:1433;databasename=ahyxasrs + url: jdbc:sqlserver://192.168.4.15:1433;databasename=source username: sa password: sa@123 mvc: diff --git a/src/main/resources/mapper/UserLoginMapper.xml b/src/main/resources/mapper/UserLoginMapper.xml index 3fdd032..b30171f 100644 --- a/src/main/resources/mapper/UserLoginMapper.xml +++ b/src/main/resources/mapper/UserLoginMapper.xml @@ -5,9 +5,11 @@ <!-- 閫氱敤鏌ヨ鏄犲皠缁撴灉 --> <resultMap id="BaseResultMap" type="com.zy.system.entity.UserLogin"> <id column="id" property="id" /> + <result column="host_id" property="hostId" /> <result column="user_id" property="userId" /> <result column="token" property="token" /> <result column="create_time" property="createTime" /> + <result column="system" property="system" /> </resultMap> diff --git a/src/main/webapp/static/js/userLogin/userLogin.js b/src/main/webapp/static/js/userLogin/userLogin.js index f0907ef..e6ac5e6 100644 --- a/src/main/webapp/static/js/userLogin/userLogin.js +++ b/src/main/webapp/static/js/userLogin/userLogin.js @@ -21,6 +21,7 @@ ,{field: 'id', title: 'ID', sort: true,align: 'center', fixed: 'left', width: 80} ,{field: 'userUsername', align: 'center',title: '鍛樺伐',event: 'User', style: 'text-decoration: underline;cursor:pointer'} ,{field: 'token', align: 'center',title: '鍑瘉鍊�'} + ,{field: 'memo', align: 'center',title: '璁板綍'} ,{field: 'createTime$', align: 'center',title: '娣诲姞鏃堕棿'} ,{fixed: 'right', title:'鎿嶄綔', align: 'center', toolbar: '#operate', width:150} -- Gitblit v1.9.1