| | |
| | | |
| | | @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); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | |
| | | 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); |
| | | } |
| | | |
| | |
| | | @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; |
| | | } |
| | |
| | | <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--> |
| | |
| | | </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"> |
| | |
| | | <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=","> |
| | |
| | | <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> |
| | |
| | | <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> |
| | |
| | | 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; |
New file |
| | |
| | | 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 ; |
| | | } |
| | | } |
New file |
| | |
| | | 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; |
| | | } |
| | |
| | | @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)) { |
| | | 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)) { |
| | | 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表单对象 |