| | |
| | | //package com.whyc.aop; |
| | | // |
| | | //import com.whyc.constant.OperationLogEnum; |
| | | //import com.whyc.dto.Response; |
| | | //import com.whyc.service.OperationLogService; |
| | | //import org.springframework.beans.factory.annotation.Autowired; |
| | | //import org.springframework.http.HttpStatus; |
| | | //import org.springframework.stereotype.Controller; |
| | | //import org.springframework.stereotype.Service; |
| | | //import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | //import org.springframework.web.bind.annotation.ResponseStatus; |
| | | //import org.springframework.web.bind.annotation.RestController; |
| | | //import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | // |
| | | //import javax.servlet.http.HttpServletRequest; |
| | | //import javax.servlet.http.HttpServletResponse; |
| | | // |
| | | ///** |
| | | // * 针对RestController层捕捉异常,结果统一返回 |
| | | // */ |
| | | //@RestControllerAdvice(annotations = {RestController.class, Controller.class, Service.class}) |
| | | //public class CustomExceptionResultHandler { |
| | | // |
| | | // @Autowired |
| | | // private OperationLogService logService; |
| | | // |
| | | // /**错误捕捉,状态码:202*/ |
| | | // @ExceptionHandler(Exception.class) |
| | | // @ResponseStatus(HttpStatus.ACCEPTED) |
| | | // public Response sendErrorResponse2Defined(Exception e, HttpServletResponse response, HttpServletRequest request){ |
| | | // String exceptionStr = e.toString(); |
| | | // String requestURI = request.getRequestURI(); |
| | | // //单项提取-登录超时 |
| | | // Integer type = OperationLogEnum.TYPE_1_SYS.getType(); |
| | | // if (exceptionStr.contains("login") && exceptionStr.contains("imeout")) { |
| | | // logService.record(OperationLogEnum.TYPE_1_SYS.getType(),OperationLogEnum.TYPE_2_CONNECTION_TIMEOUT.getType(), "登录请求超时", "异常信息:" + exceptionStr); |
| | | // } else { |
| | | // logService.record(OperationLogEnum.TYPE_1_SYS.getType(),OperationLogEnum.TYPE_2_EXCEPTION.getType(),"接口调用异常", "接口调用异常:调用接口" + requestURI + "发生错误:" + exceptionStr); |
| | | // } |
| | | // return new Response().set(0, "接口请求异常,请联系软件人员进行处理.异常信息" + exceptionStr); |
| | | // } |
| | | // |
| | | // |
| | | //} |
| | | package com.whyc.aop; |
| | | |
| | | import com.whyc.constant.UserLogTypeEnum; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.service.UserLogService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.ResponseStatus; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | /** |
| | | * 针对RestController层捕捉异常,结果统一返回 |
| | | */ |
| | | @RestControllerAdvice(annotations = {RestController.class, Controller.class, Service.class}) |
| | | public class CustomExceptionResultHandler { |
| | | |
| | | @Autowired |
| | | private UserLogService logService; |
| | | |
| | | /**错误捕捉,状态码:202*/ |
| | | @ExceptionHandler(Exception.class) |
| | | @ResponseStatus(HttpStatus.ACCEPTED) |
| | | public Response sendErrorResponse2Defined(Exception e, HttpServletResponse response, HttpServletRequest request){ |
| | | String exceptionStr = e.toString(); |
| | | String requestURI = request.getRequestURI(); |
| | | |
| | | logService.add(UserLogTypeEnum.CATEGORY_SYSTEM.getType(),UserLogTypeEnum.EXCEPTION.getType(),"接口调用异常:被调用的接口为:"+requestURI, "异常信息:" + exceptionStr); |
| | | return new Response().set(0, "接口请求异常,请联系软件人员进行处理.异常信息" + exceptionStr); |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.constant; |
| | | |
| | | /** |
| | | * 审计日志/用户操作日志 操作类型定义 |
| | | * TODO type的定义暂不确定,待后续更新 |
| | | */ |
| | | public enum UserLogTypeEnum { |
| | | |
| | | /*==========系统|业务级别范畴==========*/ |
| | | CATEGORY_SYSTEM(1, "系统级别"), |
| | | CATEGORY_BUSINESS(2, "业务级别"), |
| | | |
| | | /*==========系统级别日志==========*/ |
| | | LOGIN(1, "登录系统"), |
| | | LOGIN_FAIL(1, "登录失败"), |
| | | LOGOUT(1, "登出系统"), |
| | | LOGIN_TIMEOUT(1, "登录超时"), |
| | | PASSWORD_CHANGE(1, "密码修改"), |
| | | USER_UPDATE(1, "用户信息修改"), |
| | | USER_ADD(1, "用户增加"), |
| | | USER_DELETE(1, "用户删除"), |
| | | UNAUTHORIZED_ACCESS(1, "越权访问"), |
| | | EXCEPTION(1, "调用异常"), |
| | | SYS_PARAM_UPDATE(1, "系统配置修改"), |
| | | SYS_PARAM_DELETE(1, "系统配置删除"), |
| | | |
| | | /*==========业务级别日志==========*/ |
| | | |
| | | ; |
| | | |
| | | private Integer type; |
| | | private String name; |
| | | |
| | | UserLogTypeEnum(Integer type, String name) { |
| | | this.type = type; |
| | | this.name = name; |
| | | } |
| | | |
| | | public Integer getType() { |
| | | return type; |
| | | } |
| | | |
| | | public String getName() { |
| | | return name; |
| | | } |
| | | } |
| | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.User; |
| | | import com.whyc.service.UserService; |
| | | import com.whyc.util.RSAUtil; |
| | | import com.whyc.util.UserUtil; |
| | | import com.whyc.util.CommonUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | @RestController |
| | | @RequestMapping("user") |
| | |
| | | @PostMapping("/updatePasswordByRSA") |
| | | @ApiOperation(value = "修改密码-RSA") |
| | | public Response updatePasswordByRSA(@RequestParam String password){ |
| | | User user = UserUtil.getUser(); |
| | | User user = CommonUtil.getUser(); |
| | | return userService.updatePasswordByRSA(user,password); |
| | | } |
| | | |
New file |
| | |
| | | package com.whyc.mapper; |
| | | |
| | | import com.whyc.pojo.UserLog; |
| | | |
| | | public interface UserLogMapper extends CustomMapper<UserLog>{ |
| | | } |
| | |
| | | package com.whyc.pojo; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import lombok.Data; |
| | | import lombok.ToString; |
| | | import org.apache.ibatis.type.Alias; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | @Data |
| | | @ToString |
| | | @Alias("User") |
| | | @TableName(schema = "db_power_intelligence",value = "tb_user") |
| | | public class User{ |
| | | |
New file |
| | |
| | | package com.whyc.pojo; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import lombok.Data; |
| | | import lombok.ToString; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * CREATE TABLE `db_power_intelligence`.`tb_user_log` ( |
| | | * `id` INT NOT NULL AUTO_INCREMENT, |
| | | * `category` INT NOT NULL COMMENT '业务级别:1-系统,2-业务', |
| | | * `type` INT NOT NULL COMMENT '事件类型', |
| | | * `message` VARCHAR(45) NULL COMMENT '执行的操作', |
| | | * `message_detail` TEXT NULL COMMENT '操作的详细参数', |
| | | * `user_id` INT NOT NULL, |
| | | * `user_name` VARCHAR(45) NOT NULL, |
| | | * `terminal_ip` VARCHAR(45) NULL, |
| | | * `create_time` DATETIME NOT NULL, |
| | | * PRIMARY KEY (`id`)) |
| | | * COMMENT = '涵盖所有用户的安全审计日志,保存所有的重要事件'; |
| | | */ |
| | | @Data |
| | | @ToString |
| | | @TableName(schema = "db_power_intelligence",value = "tb_user_log") |
| | | public class UserLog { |
| | | |
| | | private Integer id; |
| | | @ApiModelProperty(value = "业务级别:1-系统,2-业务") |
| | | private Integer category; |
| | | /** |
| | | * 时间类型持续迭代中,请查看枚举类 |
| | | * {@link com.whyc.constant.UserLogTypeEnum} |
| | | */ |
| | | @ApiModelProperty(value = "事件类型") |
| | | private Integer type; |
| | | @ApiModelProperty(value = "执行的操作") |
| | | private String message; |
| | | @ApiModelProperty(value = "操作的详细参数") |
| | | private String messageDetail; |
| | | @ApiModelProperty(value = "用户id") |
| | | private Integer userId; |
| | | @ApiModelProperty(value = "用户名") |
| | | private String userName; |
| | | @ApiModelProperty(value = "终端ip") |
| | | private String terminalIp; |
| | | @ApiModelProperty(value = "创建时间") |
| | | private Date createTime; |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.service; |
| | | |
| | | import com.whyc.mapper.UserLogMapper; |
| | | import com.whyc.pojo.UserLog; |
| | | import com.whyc.util.CommonUtil; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Date; |
| | | |
| | | @Service |
| | | public class UserLogService { |
| | | |
| | | @Resource |
| | | private UserLogMapper mapper; |
| | | |
| | | |
| | | public void add(Integer categoryId, Integer type, String message, String messageDetail) { |
| | | HttpServletRequest request = CommonUtil.getRequest(); |
| | | |
| | | UserLog userLog = new UserLog(); |
| | | userLog.setCategory(categoryId); |
| | | userLog.setType(type); |
| | | userLog.setMessage(message); |
| | | userLog.setMessageDetail(messageDetail); |
| | | userLog.setUserId(CommonUtil.getUser().getId()); |
| | | userLog.setUserName(CommonUtil.getUser().getName()); |
| | | userLog.setTerminalIp(request.getRemoteAddr()); |
| | | userLog.setCreateTime(new Date()); |
| | | |
| | | mapper.insert(userLog); |
| | | } |
| | | } |
| | |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.gson.JsonSyntaxException; |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.whyc.pojo.User; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.springframework.boot.system.ApplicationHome; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | |
| | | public static ServletContext getApplication(){ |
| | | return getSession().getServletContext(); |
| | | } |
| | | |
| | | public static User getUser(){ |
| | | |
| | | User principal = (User) SecurityUtils.getSubject().getPrincipal(); |
| | | if(principal == null){ |
| | | User user = new User(); |
| | | user.setName("unLogged-in user"); |
| | | user.setId(0); |
| | | return user; |
| | | }else { |
| | | return principal; |
| | | } |
| | | } |
| | | |
| | | //获取application中互斥上传的标志位flag |
| | | public static int getFlag(){ |
| | | ServletContext app=getApplication(); |