Merge branch 'master' of http://8.140.179.55:20000/r/pms-SV
Conflicts:
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java
pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java
| | |
| | | package com.dy.pmsGlobal.aop; |
| | | |
| | | import com.dy.common.util.ObjectToMapUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.dy.common.webFilter.UserTokenContext; |
| | | import com.dy.common.webUtil.BaseResponse; |
| | | import com.dy.common.webUtil.BaseResponseUtils; |
| | | import com.dy.pmsGlobal.pojoBa.BaUser; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.mysql.cj.util.StringUtils; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.aspectj.lang.JoinPoint; |
| | | import org.aspectj.lang.annotation.AfterReturning; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import org.springframework.web.util.UriComponentsBuilder; |
| | | |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Aspect |
| | | @Component |
| | | public class LogAspect { |
| | |
| | | @Value("${pms.sso.curUserUrl}") |
| | | public String SsoCurUserUrl ; |
| | | |
| | | private LogSv logSv; |
| | | private LogService logSv; |
| | | @Autowired |
| | | public void setLogSv(LogSv logSv){ |
| | | public void setLogSv(LogService logSv){ |
| | | this.logSv = logSv; |
| | | } |
| | | private RestTemplate restTemplate; |
| | |
| | | } |
| | | |
| | | @AfterReturning(pointcut = "@annotation(com.dy.pmsGlobal.aop.Log)", returning = "result") |
| | | public void logAfterReturning(JoinPoint joinPoint, Object result) { |
| | | // 获取方法的中文描述 |
| | | MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| | | Log operationDescription = methodSignature.getMethod().getAnnotation(Log.class); |
| | | String operationName = operationDescription.value(); |
| | | //结果 |
| | | BaseResponse response = (BaseResponse)result; |
| | | response.isSuccess(); |
| | | // 获取用户信息 |
| | | BaUser user = (BaUser)getCurUser(response); |
| | | Long operator = user!=null?user.id: 1l; |
| | | |
| | | // 获取IP地址 |
| | | String ipAddress = getRemoteHost(); |
| | | // 记录日志 |
| | | logSv.save(operator, operationName,ipAddress); |
| | | public void logAfterReturning(JoinPoint joinPoint, BaseResponse result) { |
| | | try{ |
| | | // 获取用户信息 |
| | | BaUser user = (BaUser)getCurUser(result); |
| | | if(user!=null && user.id !=null && !StringUtils.isNullOrEmpty(user.name)){ |
| | | Long operator = user.id; |
| | | // 获取方法的中文描述 |
| | | MethodSignature sign = (MethodSignature) joinPoint.getSignature(); |
| | | Log logDesc = sign.getMethod().getAnnotation(Log.class); |
| | | String operationName = logDesc.value(); |
| | | // 获取IP地址 |
| | | String ip = getRemoteHost(); |
| | | // 记录日志 |
| | | logSv.save(operator, operationName,ip,result.getCode(),result.getMsg()); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("记录日志异常:"+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | if(!StringUtils.isNullOrEmpty(SsoCurUserUrl)){ |
| | | String token = UserTokenContext.get(); |
| | | if(StringUtils.isNullOrEmpty(token)){ |
| | | Map<String,Object> resMap = ObjectToMapUtil.objectToMap(response.getContent()); |
| | | token =resMap.containsKey("token")?resMap.get("token").toString():""; |
| | | JSONObject res = objectToJson(response.getContent()); |
| | | if(res!=null && res.containsKey("token")){ |
| | | token = res.get("token").toString(); |
| | | } |
| | | } |
| | | |
| | | String url = UriComponentsBuilder.fromUriString(SsoCurUserUrl) |
| | |
| | | } |
| | | return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; |
| | | } |
| | | |
| | | /** |
| | | * 将对象转换为JSONObject |
| | | * @param obj |
| | | * @return |
| | | */ |
| | | public static JSONObject objectToJson(Object obj) { |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | try { |
| | | JSONObject o = JSONObject.parseObject(mapper.writeValueAsString(obj)); |
| | | return o; |
| | | } catch (Exception e) { |
| | | log.error("对象转换为JSONObject失败:"+e.getMessage()); |
| | | return null; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.pmsGlobal.aop; |
| | | |
| | | import com.dy.pmsGlobal.daoBa.BaLogMapper; |
| | | import com.dy.pmsGlobal.pojoBa.BaLog; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.Date; |
| | | |
| | | @Service |
| | | public class LogService { |
| | | |
| | | @Autowired |
| | | private BaLogMapper dao; |
| | | |
| | | public void save(long operator, String operation,String ip,String code,String msg) { |
| | | BaLog log = new BaLog(); |
| | | log.userId=operator; |
| | | log.content = operation; |
| | | log.dt = new Date(); |
| | | log.ip = ip; |
| | | log.code = code; |
| | | log.msg = msg; |
| | | dao.insert(log); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.dy.pmsGlobal.pojoBa; |
| | | |
| | | import com.alibaba.fastjson2.annotation.JSONField; |
| | | import com.alibaba.fastjson2.writer.ObjectWriterImplToString; |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import lombok.*; |
| | | |
| | |
| | | @NoArgsConstructor |
| | | @AllArgsConstructor |
| | | public class BaLog { |
| | | private Long id; |
| | | |
| | | @JSONField(serializeUsing= ObjectWriterImplToString.class) |
| | | @TableId(value = "id", type = IdType.INPUT) |
| | | public Long id; |
| | | |
| | | /** |
| | | * 用户ID |
| | | */ |
| | | private Long userId; |
| | | public Long userId; |
| | | |
| | | /** |
| | | * 日志内容 |
| | | */ |
| | | private String content; |
| | | public String content; |
| | | |
| | | /** |
| | | * 请求IP |
| | | */ |
| | | private String resIp; |
| | | public String ip; |
| | | |
| | | /** |
| | | * 响应状态码 |
| | | */ |
| | | public String code; |
| | | /** |
| | | * 响应信息 |
| | | */ |
| | | public String msg; |
| | | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | private Date createDt; |
| | | public Date dt; |
| | | } |
| | |
| | | <id column="id" jdbcType="BIGINT" property="id" /> |
| | | <result column="user_id" jdbcType="BIGINT" property="userId" /> |
| | | <result column="content" jdbcType="VARCHAR" property="content" /> |
| | | <result column="res_ip" jdbcType="VARCHAR" property="resIp" /> |
| | | <result column="create_dt" jdbcType="TIMESTAMP" property="createDt" /> |
| | | <result column="ip" jdbcType="VARCHAR" property="ip" /> |
| | | <result column="code" jdbcType="VARCHAR" property="code" /> |
| | | <result column="msg" jdbcType="VARCHAR" property="msg" /> |
| | | <result column="dt" jdbcType="TIMESTAMP" property="dt" /> |
| | | </resultMap> |
| | | <sql id="Base_Column_List"> |
| | | <!--@mbg.generated--> |
| | | id, user_id, content, create_dt,res_ip |
| | | id, user_id, content, dt,ip,code,msg |
| | | </sql> |
| | | <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> |
| | | <!--@mbg.generated--> |
| | |
| | | </select> |
| | | <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.dy.pmsGlobal.pojoBa.BaLog" useGeneratedKeys="true"> |
| | | <!--@mbg.generated--> |
| | | insert into ba_log (user_id, content, create_dt,res_ip |
| | | insert into ba_log (user_id, content, ip,code,msg |
| | | ) |
| | | values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{createDt,jdbcType=TIMESTAMP} |
| | | , #{resIp,jdbcType=VARCHAR} |
| | | values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{ip,jdbcType=VARCHAR}, |
| | | #{code,jdbcType=VARCHAR},#{msg,jdbcType=VARCHAR} |
| | | ) |
| | | </insert> |
| | | <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.dy.pmsGlobal.pojoBa.BaLog" useGeneratedKeys="true"> |
| | |
| | | <if test="content != null"> |
| | | content, |
| | | </if> |
| | | <if test="createDt != null"> |
| | | create_dt, |
| | | <if test="code != null"> |
| | | code, |
| | | </if> |
| | | <if test="resIp != null"> |
| | | res_ip, |
| | | <if test="msg != null"> |
| | | msg, |
| | | </if> |
| | | <if test="ip != null"> |
| | | ip, |
| | | </if> |
| | | </trim> |
| | | <trim prefix="values (" suffix=")" suffixOverrides=","> |
| | |
| | | <if test="content != null"> |
| | | #{content,jdbcType=VARCHAR}, |
| | | </if> |
| | | <if test="createDt != null"> |
| | | #{createDt,jdbcType=TIMESTAMP}, |
| | | <if test="code != null"> |
| | | #{code,jdbcType=VARCHAR}, |
| | | </if> |
| | | <if test="resIp != null"> |
| | | #{resIp,jdbcType=VARCHAR}, |
| | | <if test="msg != null"> |
| | | #{msg,jdbcType=VARCHAR}, |
| | | </if> |
| | | <if test="ip != null"> |
| | | #{ip,jdbcType=VARCHAR}, |
| | | </if> |
| | | </trim> |
| | | </insert> |
| | |
| | | <if test="content != null and content != '' "> |
| | | content like concat('%', #{content}, '%') and |
| | | </if> |
| | | <if test="createDt != null and createDt != '' "> |
| | | create_dt = #{createDt,jdbcType=TIMESTAMP} and |
| | | <if test="dt != null and dt != '' "> |
| | | dt = #{dt,jdbcType=TIMESTAMP} and |
| | | </if> |
| | | <if test="resIp != null and resIp != '' "> |
| | | res_ip =#{resIp,jdbcType=VARCHAR} and |
| | | <if test="ip != null and ip != '' "> |
| | | ip =#{ip,jdbcType=VARCHAR} and |
| | | </if> |
| | | </trim> |
| | | order by id desc |
| | |
| | | <if test="content != null and content != '' "> |
| | | content like concat('%', #{content}, '%') and |
| | | </if> |
| | | <if test="createDt != null and createDt != '' "> |
| | | create_dt = #{createDt,jdbcType=TIMESTAMP} and |
| | | <if test="dt != null and dt != '' "> |
| | | dt = #{dt,jdbcType=TIMESTAMP} and |
| | | </if> |
| | | <if test="resIp != null and resIp != '' "> |
| | | res_ip =#{resIp,jdbcType=VARCHAR} and |
| | | <if test="ip != null and ip != '' "> |
| | | ip =#{ip,jdbcType=VARCHAR} and |
| | | </if> |
| | | </trim> |
| | | </select> |
| | |
| | | @Autowired |
| | | private BaLogMapper dao; |
| | | |
| | | |
| | | /** |
| | | * 得到日志 |
| | | * |
| | |
| | | rsVo.obj = this.dao.selectSome(params) ; |
| | | return rsVo ; |
| | | } |
| | | } |
| | | } |
| | |
| | | package com.dy.pmsBase.log; |
| | | |
| | | |
| | | import com.dy.common.webUtil.QueryConditionVo; |
| | | import lombok.*; |
| | | |
| | |
| | | @Builder |
| | | public class QueryVo extends QueryConditionVo { |
| | | public String name; |
| | | } |
| | | |
| | | } |
| | |
| | | @NotEmpty(message = "密码不能为空") //不能为空也不能为null |
| | | @Length(message = "密码必须{min}位", min = 6, max = 6) |
| | | public String password ; |
| | | |
| | | @NotEmpty(message = "验证码不能为空") //不能为空也不能为null |
| | | @Length(message = "验证码必须{min}位", min = 4, max = 4) |
| | | public String captcha ; |
| | | } |
| | | |
| | |
| | | import com.dy.pmsGlobal.pojoBa.BaUser; |
| | | import com.mysql.cj.util.StringUtils; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpSession; |
| | | import jakarta.validation.Valid; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.IOException; |
| | | import java.util.Objects; |
| | | import java.util.Random; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据 |
| | | @Log("用户登录(json)") |
| | | public BaseResponse<UserVo> login(@RequestBody @Valid LoginVo vo, BindingResult bindingResult) { |
| | | public BaseResponse<UserVo> login(@RequestBody @Valid LoginVo vo, |
| | | HttpSession session, |
| | | BindingResult bindingResult) { |
| | | try { |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | return this.doLogin(vo) ; |
| | | |
| | | // 从Session中获取保存的验证码 |
| | | String sessionCaptcha = (String) session.getAttribute("captcha"); |
| | | // 首先验证验证码 |
| | | if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { |
| | | session.removeAttribute("captcha"); |
| | | return this.doLogin(vo) ; |
| | | } else { |
| | | // 验证码错误,返回登录页面并显示错误信息 |
| | | return BaseResponseUtils.buildFail("验证码错误"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("查询一个用户数据异常", e); |
| | | log.error("用户登录异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | */ |
| | | @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//前端提交form表单数据 |
| | | @Log("用户登录(form)") |
| | | public BaseResponse<UserVo> loginForm(@RequestBody @Valid LoginVo vo, BindingResult bindingResult){ |
| | | public BaseResponse<UserVo> loginForm(@RequestBody @Valid LoginVo vo, HttpSession session,BindingResult bindingResult){ |
| | | try{ |
| | | if(bindingResult != null && bindingResult.hasErrors()){ |
| | | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); |
| | | } |
| | | return this.doLogin(vo) ; |
| | | // 从Session中获取保存的验证码 |
| | | String sessionCaptcha = (String) session.getAttribute("captcha"); |
| | | // 首先验证验证码 |
| | | if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { |
| | | session.removeAttribute("captcha"); |
| | | return this.doLogin(vo) ; |
| | | } else { |
| | | // 验证码错误,返回登录页面并显示错误信息 |
| | | return BaseResponseUtils.buildFail("验证码错误"); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("查询一个用户数据异常", e); |
| | | log.error("用户登录异常", e); |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | return BaseResponseUtils.buildException(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 此方法供子模块系统调用,所以不公开在API接口中 |
| | |
| | | } |
| | | return vo ; |
| | | } |
| | | |
| | | /** |
| | | * 生成登录验证码 |
| | | * @param response |
| | | * @param session |
| | | * @throws IOException |
| | | */ |
| | | @GetMapping("/captcha") |
| | | public void captcha(HttpServletResponse response, HttpSession session) throws IOException { |
| | | // 设置响应的类型格式为图片格式 |
| | | response.setContentType("image/jpeg"); |
| | | // 禁止图像缓存 |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | response.setDateHeader("Expires", 0); |
| | | |
| | | int width = 100, height = 50; |
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| | | Graphics g = image.getGraphics(); |
| | | // 设定背景色 |
| | | g.setColor(Color.WHITE); |
| | | g.fillRect(0, 0, width, height); |
| | | // 设定字体 |
| | | g.setFont(new Font("Arial", Font.BOLD, 30)); |
| | | // 随机生成验证码 |
| | | String captcha = generateCaptcha(); |
| | | // 将验证码存入Session |
| | | session.setAttribute("captcha", captcha); |
| | | // 在图片上绘制验证码 |
| | | g.setColor(Color.BLACK); |
| | | g.drawString(captcha, 15, 35); |
| | | g.dispose(); |
| | | // 输出图片 |
| | | ImageIO.write(image, "JPEG", response.getOutputStream()); |
| | | } |
| | | |
| | | |
| | | ///////////////////////////////////////////////////////////////// |
| | | // |
| | | // 以下私有方法 |
| | | // |
| | | ///////////////////////////////////////////////////////////////// |
| | | |
| | | /** |
| | | * 生成四位随机数 |
| | | * @return |
| | | */ |
| | | private String generateCaptcha() { |
| | | Random r = new Random(); |
| | | return r.nextInt(9000) + 1000 + ""; |
| | | } |
| | | /** |
| | | * 用户登录 |
| | | * @param vo 登录用户form表单对象 |