From 985f56c3cf4f94119be28430cde83b578539b2ee Mon Sep 17 00:00:00 2001 From: liuxm <liuxm@fescotech.com> Date: 星期三, 24 四月 2024 14:04:32 +0800 Subject: [PATCH] 日志管理相关修改 --- pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java | 10 +- pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java | 1 pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogSv.java | 12 +- pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java | 26 ++-- pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java | 14 ++ pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java | 48 +++++++++ pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/LoginVo.java | 4 pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java | 86 ++++++++++++++++- pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml | 44 ++++---- 9 files changed, 192 insertions(+), 53 deletions(-) diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java index 0902667..62d2281 100644 --- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java +++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogAspect.java @@ -45,21 +45,23 @@ @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(); + BaseResponse res = (BaseResponse)result; + res.isSuccess(); // 鑾峰彇鐢ㄦ埛淇℃伅 - BaUser user = (BaUser)getCurUser(response); - Long operator = user!=null?user.id: 1l; + BaUser user = (BaUser)getCurUser(res); + if(user!=null){ + 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); + } - // 鑾峰彇IP鍦板潃 - String ipAddress = getRemoteHost(); - // 璁板綍鏃ュ織 - logSv.save(operator, operationName,ipAddress); } /** diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogSv.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogSv.java index f31954b..2accad8 100644 --- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogSv.java +++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/aop/LogSv.java @@ -8,8 +8,6 @@ import org.springframework.stereotype.Service; import java.util.Date; -import java.util.List; -import java.util.Map; @Service public class LogSv { @@ -17,12 +15,12 @@ @Autowired private BaLogMapper dao; - public void save(long operator, String operation,String resIp) { + public void save(long operator, String operation,String ip) { BaLog log = new BaLog(); - log.setUserId(operator); - log.setContent(operation); - log.setCreateDt(new Date()); - log.setResIp(resIp); + log.userId=operator; + log.content = operation; + log.dt = new Date(); + log.ip = ip; dao.insert(log); } diff --git a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java index dc0b617..213d9cb 100644 --- a/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java +++ b/pms-parent/pms-global/src/main/java/com/dy/pmsGlobal/pojoBa/BaLog.java @@ -12,25 +12,25 @@ @NoArgsConstructor @AllArgsConstructor public class BaLog { - private Long id; + public Long id; /** * 鐢ㄦ埛ID */ - private Long userId; + public Long userId; /** * 鏃ュ織鍐呭 */ - private String content; + public String content; /** * 璇锋眰IP */ - private String resIp; + public String ip; /** * 鍒涘缓鏃堕棿 */ - private Date createDt; + public Date dt; } \ No newline at end of file diff --git a/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml b/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml index c7cd531..f02960f 100644 --- a/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml +++ b/pms-parent/pms-global/src/main/resources/mapper/BaLogMapper.xml @@ -7,12 +7,12 @@ <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="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 </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"> <!--@mbg.generated--> @@ -23,10 +23,10 @@ </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, dt,ip ) - values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{createDt,jdbcType=TIMESTAMP} - , #{resIp,jdbcType=VARCHAR} + values (#{userId,jdbcType=BIGINT}, #{content,jdbcType=VARCHAR}, #{dt,jdbcType=TIMESTAMP} + , #{ip,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.dy.pmsGlobal.pojoBa.BaLog" useGeneratedKeys="true"> @@ -39,11 +39,11 @@ <if test="content != null"> content, </if> - <if test="createDt != null"> - create_dt, + <if test="dt != null"> + dt, </if> - <if test="resIp != null"> - res_ip, + <if test="ip != null"> + ip, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> @@ -53,11 +53,11 @@ <if test="content != null"> #{content,jdbcType=VARCHAR}, </if> - <if test="createDt != null"> - #{createDt,jdbcType=TIMESTAMP}, + <if test="dt != null"> + #{dt,jdbcType=TIMESTAMP}, </if> - <if test="resIp != null"> - #{resIp,jdbcType=VARCHAR}, + <if test="ip != null"> + #{ip,jdbcType=VARCHAR}, </if> </trim> </insert> @@ -75,11 +75,11 @@ <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 @@ -100,11 +100,11 @@ <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> diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java index e0be1b7..7a1fb2c 100644 --- a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogCtrl.java @@ -4,7 +4,6 @@ import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.QueryResultVo; -import com.dy.pmsGlobal.aop.LogSv; import com.dy.pmsGlobal.aop.Log; import com.dy.pmsGlobal.pojoBa.BaLog; import lombok.extern.slf4j.Slf4j; diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java new file mode 100644 index 0000000..5a46afb --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/LogSv.java @@ -0,0 +1,48 @@ +package com.dy.pmsBase.log; + +import com.dy.common.webUtil.QueryResultVo; +import com.dy.pmsGlobal.daoBa.BaLogMapper; +import com.dy.pmsGlobal.pojoBa.BaLog; +import org.apache.dubbo.common.utils.PojoUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Service +public class LogSv { + + @Autowired + private BaLogMapper dao; + + /** + * 寰楀埌鏃ュ織 + * + * @param id 鏃ュ織ID + * @return 瀹炰綋 + */ + public BaLog selectById(Long id) { + return dao.selectByPrimaryKey(id); + } + + + /** + * 鑾峰彇鏃ュ織鍒楄〃 + */ + public QueryResultVo<List<BaLog>> selectSome(QueryVo queryVo) { + Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); + + //鏌ヨ绗﹀悎鏉′欢鐨勮褰曟�绘暟 + Long itemTotal = dao.selectSomeCount(params); + + QueryResultVo<List<BaLog>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ; + //璁$畻鍒嗛〉绛変俊鎭� + rsVo.calculateAndSet(itemTotal, params); + + //鏌ヨ绗﹀悎鏉′欢鐨勮褰� + rsVo.obj = this.dao.selectSome(params) ; + return rsVo ; + } +} \ No newline at end of file diff --git a/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java new file mode 100644 index 0000000..8fab024 --- /dev/null +++ b/pms-parent/pms-web-base/src/main/java/com/dy/pmsBase/log/QueryVo.java @@ -0,0 +1,14 @@ +package com.dy.pmsBase.log; + +import com.dy.common.webUtil.QueryConditionVo; +import lombok.*; + +@Data +@EqualsAndHashCode(callSuper = false) +@ToString(callSuper = true) +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class QueryVo extends QueryConditionVo { + public String name; +} \ No newline at end of file diff --git a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/LoginVo.java b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/LoginVo.java index f610db8..b1b32b5 100644 --- a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/LoginVo.java +++ b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/LoginVo.java @@ -26,5 +26,9 @@ @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 ; } diff --git a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java index 8d16790..c3ee57c 100644 --- a/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java +++ b/pms-parent/pms-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java @@ -8,6 +8,8 @@ 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; @@ -15,7 +17,12 @@ 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; /** @@ -47,14 +54,25 @@ */ @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) ; + + // 浠嶴ession涓幏鍙栦繚瀛樼殑楠岃瘉鐮� + String sessionCaptcha = (String) session.getAttribute("captcha"); + // 棣栧厛楠岃瘉楠岃瘉鐮� + if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { + return this.doLogin(vo) ; + } else { + // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭� + return BaseResponseUtils.buildFail("楠岃瘉鐮侀敊璇�"); + } } catch (Exception e) { - log.error("鏌ヨ涓�涓敤鎴锋暟鎹紓甯�", e); + log.error("鐢ㄦ埛鐧诲綍寮傚父", e); return BaseResponseUtils.buildException(e.getMessage()); } } @@ -66,14 +84,22 @@ */ @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) ; + // 浠嶴ession涓幏鍙栦繚瀛樼殑楠岃瘉鐮� + String sessionCaptcha = (String) session.getAttribute("captcha"); + // 棣栧厛楠岃瘉楠岃瘉鐮� + if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { + return this.doLogin(vo) ; + } else { + // 楠岃瘉鐮侀敊璇紝杩斿洖鐧诲綍椤甸潰骞舵樉绀洪敊璇俊鎭� + return BaseResponseUtils.buildFail("楠岃瘉鐮侀敊璇�"); + } } catch (Exception e) { - log.error("鏌ヨ涓�涓敤鎴锋暟鎹紓甯�", e); + log.error("鐢ㄦ埛鐧诲綍寮傚父", e); return BaseResponseUtils.buildException(e.getMessage()); } } @@ -100,6 +126,8 @@ return BaseResponseUtils.buildException(e.getMessage()); } } + + /** * 姝ゆ柟娉曚緵瀛愭ā鍧楃郴缁熻皟鐢紝鎵�浠ヤ笉鍏紑鍦ˋPI鎺ュ彛涓� @@ -187,11 +215,57 @@ } 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琛ㄥ崟瀵硅薄 -- Gitblit v1.8.0