SpringBoot中有一个ControllerAdvice的注解,使用该注解表示开启了全局异常的捕获,我们只需在自定义一个方法使用ExceptionHandler注解,即可对Controller注解中产生的异常进行处理,需要注意的是该注解只拦截被 @controller 层抛出的异常。
- 全局异常处理器
package com.strtz3.blog.config.custom;
import com.strtz3.blog.config.PageConfig;
import com.strtz3.blog.config.custom.execption.AdminException;
import com.strtz3.blog.config.custom.execption.VerifyCodeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
//拦截标注@controller的抛出的异常
@ControllerAdvice
@Slf4j
public class CustomExceptionHandle {
@ExceptionHandler(Exception.class)
//@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //标注状态码
public String errorHandler(Exception exception, HttpServletRequest request, Model model) throws Exception {
String error_url = request.getRequestURL().toString();
model.addAttribute("error_url", error_url);
model.addAttribute("exception", exception);
throw exception;
}
/**
* 博客异常处理
*
* @param exception
* @param request
* @param model
* @return
*/
@ExceptionHandler(AdminException.class)
public String handlerMyException(AdminException exception, HttpServletRequest request, Model model) {
String error_url = request.getRequestURL().toString();
model.addAttribute("error_url", error_url);
model.addAttribute("exception", exception);
return PageConfig.ERROR;
}
/**
* 图片码异常处理
*
* @param exception
* @param request
* @param model
* @return
*/
@ExceptionHandler(VerifyCodeException.class)
public String handlerMyException(VerifyCodeException exception, HttpServletRequest request, Model model) {
String error_url = request.getRequestURL().toString();
model.addAttribute("error_url", error_url);
model.addAttribute("exception", exception);
return PageConfig.ERROR;
}
}
- Filter 过滤器处理图片码
package com.strtz3.blog.config.custom.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Component("validateCodeFilter")
@Slf4j
public class VerifyCodeFilter extends OncePerRequestFilter implements Filter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
// 必须是登录的post请求才能进行验证,其他的直接放行
if (request.getRequestURI().equalsIgnoreCase("/loginPage") && request.getMethod().equalsIgnoreCase("POST")) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpSession session = httpServletRequest.getSession();
String verifyCode = (String) session.getAttribute("verifyCode");
session.removeAttribute("verifyCode");
String form_verifyCode = request.getParameter("verifyCode");
if (StringUtils.isEmpty(form_verifyCode)) {
request.getRequestDispatcher("/verify/null").forward(request, response);
}
if (!verifyCode.equalsIgnoreCase(form_verifyCode)) {
request.getRequestDispatcher("/verify/error").forward(request, response);
}
}
filterChain.doFilter(request, response);
}
}
- controller 层需要标注 @Controller
package com.strtz3.blog.controller;
import com.strtz3.blog.config.custom.enums.AuthEnum;
import com.strtz3.blog.config.custom.execption.VerifyCodeException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class ExceptionController {
@RequestMapping("/verify/null")
public void verify_null(HttpServletResponse response, HttpSession session) throws IOException {
throw new VerifyCodeException(AuthEnum.NULL_VERIFY_CODE);
}
@RequestMapping("/verify/error")
public void verify_error(HttpServletResponse response, HttpSession session) throws IOException {
throw new VerifyCodeException(AuthEnum.ERROR_VERIFY_CODE);
}
}