springboot项目系列-论坛系统05全局异常处理+日志处理+MD5加密
springboot项目系列-论坛系统05全局异常处理+日志处理使用了spring的AOP
论坛地址:http://www.cywloveyou.top
配置日志
logging:level:root: infocom.cyw: debugfile: log/blog-dev.log
编写异常处理类
package com.cyw.hander;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;/*** @Description: 拦截异常处理*/
//拦截有Controller注解的控制器
@ControllerAdvice
public class ControllerExceptionHandler {private final Logger logger = LoggerFactory.getLogger(this.getClass());@ExceptionHandler(Exception.class)public ModelAndView exceptionHandler(HttpServletRequest request, Exception e) throws Exception {logger.error("Request URL : {},Exception : {}", request.getRequestURL(),e);// 当标识了状态码的时候就不拦截if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {throw e;}ModelAndView mv = new ModelAndView();mv.addObject("url",request.getRequestURL());mv.addObject("exception", e);mv.setViewName("error/error");return mv;}
}
package com.cyw;import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;/****/
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException{public NotFoundException() {}public NotFoundException(String message) {super(message);}public NotFoundException(String message, Throwable cause) {super(message, cause);}
}
日志处理类
package com.cyw.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;/*** @Description: 日志切面处理* @Author: Cyw*/
@Aspect
@Component
public class LogAspect {private final Logger logger = LoggerFactory.getLogger(this.getClass());// 拦截控制器@Pointcut("execution(* com.cyw.controller.*.*(..))")public void log() {}/*** 前置通知* @param joinPoint*/@Before("log()")public void doBefore(JoinPoint joinPoint) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();String url = request.getRequestURL().toString();String ip = request.getRemoteAddr();//获取方法名String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();//获取传入的参数Object[] args = joinPoint.getArgs();RequestLog requestLog = new RequestLog(url, ip, classMethod, args);logger.info("Request : {}", requestLog);}/*** 后置通知* @param joinPoint*/@After("log()")public void doAfter(JoinPoint joinPoint) {// logger.info("--------doAfter--------");}/*** 返回通知,在方法返回结果之后执行* @param result*/@AfterReturning(returning = "result",pointcut = "log()")public void doAfterRuturn(Object result) {logger.info("Result : {}", result);}private class RequestLog {private String url;private String ip;private String classMethod;private Object[] args;public RequestLog(String url, String ip, String classMethod, Object[] args) {this.url = url;this.ip = ip;this.classMethod = classMethod;this.args = args;}@Overridepublic String toString() {return "{" +"url='" + url + '\'' +", ip='" + ip + '\'' +", classMethod='" + classMethod + '\'' +", args=" + Arrays.toString(args) +'}';}}
}
MD5盐值加密(工具类而已,不必深究)
package com.cyw.util;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {/*** @Description: MD5加密* @Date: 10:35 2020/3/27* @Param: 要加密的字符串* @Return: 加密后的字符串*/public static String code(String str){try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(str.getBytes());byte[]byteDigest = md.digest();int i;StringBuffer buf = new StringBuffer("");for (int offset = 0; offset < byteDigest.length; offset++) {i = byteDigest[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}//32位加密return buf.toString();// 16位的加密//return buf.toString().substring(8, 24);} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;}}
}
springboot项目系列-论坛系统05全局异常处理+日志处理+MD5加密相关推荐
- springboot项目系列-论坛系统04登录注册实现
springboot项目系列-论坛系统04登录注册实现 论坛地址:http://www.cywloveyou.top 注册(使用AJAX,邮件任务,RabbitMQ) 首先跳转到注册页面,进行注册,跳 ...
- springboot项目系列-论坛系统06整合rabbitmq实现异步发邮件
springboot项目系列-博客系统06整合rabbitmq实现异步发邮件 今天突发奇想,既然会了rabbitmq为何不用,之前的注册发邮件功能是使用springboot的异步任务做的,但是想想以后 ...
- springboot项目系列-论坛系统01介绍技术栈
论坛系统 论坛地址:http://cywloveyou.top Gitee地址:https://gitee.com/cywloveyou/my-forum-opened 前言: 2020年经历了好多, ...
- java flux api,SpringBoot学习系列-WebFlux REST API 全局异常处理
本文内容 为什么要全局异常处理? WebFlux REST 全局异常处理实战 小结 摘录:只有不断培养好习惯,同时不断打破坏习惯,我们的行为举止才能够自始至终都是正确的. 一.为什么要全局异常处理? ...
- 基于Springboot的社区论坛系统(源代码+数据库)055
部分代码地址 https://gitee.com/ynwynwy/forum-public 基于Springboot的社区论坛系统(源代码+数据库) 一.系统介绍 前台: 话题列表,搜索话题,发布话题 ...
- springboot(2.2.4)全局异常处理(RestControllerAdvice,ExceptionHandler)
当程序出现异常我们一般try,catch处理,这样代码结构不好看,我们希望程序员写的代码业务和框架是分离的,通过注解进行绑定使用.springboot里通过@RestControllerAdvice注 ...
- SpringBoot+拦截器+自定义异常+自定义注解+全局异常处理简单实现接口权限管理...
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_44102992/article/ details/107335702 前言 提到权限管理这块肯定很多人第一想到的 ...
- SpringBoot + VUE BBS论坛系统
BBS论坛 项目源代码见文章结尾 项目介绍 BBS论坛系统是一套前后端分离的开源社区系统,基于目前主流的前端框架VUE + 后端SpringBoot + MyBatis-plus + MySQL等,其 ...
- 从零开始带你部署springboot项目到ubuntu服务器05
从零开始带你部署springboot项目到ubuntu服务器 1 重装系统 2 更换源 3 部署 1 安装jdk 2 安装tomcat 3 安装mysql 1) 查看本地mysql版本 2)在服务器上 ...
最新文章
- 为什么我们需要比特币(BCH)
- SharePoint PowerShell命令系列 (9) New-SPWeb
- html怎么让图片重叠_PS倒影效果怎么做,如何利用PS给产品制作倒影
- PHP yii 框架源码阅读(一)
- 计算机应用问题,计算机应用的现状与发展的问题
- leetcode243. 最短单词距离(vip题)好像挺简单?
- Java 9 揭秘(9. 打破模块封装)
- MongoDB (六) MongoDB 集合操作
- How Blink Works 中文译文
- c语言 字节 半字 字,PLC的位,半字节,字节,字介绍
- mysql安装和下载过程_mysql下载与安装过程
- 【Daily】IE弹窗SQL分割字符串
- Java applet 类
- 基于java毕业生就业信息管理系统的设计(含源文件)
- 怎么在图片上直接编辑文字?建议收藏这些编辑方法
- 字符串函数strtolower解析
- c语言项目实战 —— 图书管理系统
- 计算机考试运用的数学知识点,计算器的使用知识点
- win10修改和隐藏系统输入法指示器
- BLE MESH组网(七)真机配置