java http 401_java – 处理401错误(Spring Security)

这是一个完整的处理程序,适用于所有一小部分错误页面:

@Controller

public class ErrorCodeController extends BaseController {

@ExceptionHandler(ApplicationException.class)

@RequestMapping(value="errorPage400", method=RequestMethod.GET)

@ResponseStatus(value = HttpStatus.BAD_REQUEST)

public String handleBadRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) {

map.addAttribute("http-error-code", HttpStatus.BAD_REQUEST);

return processErrorCodes(ex,response,map);

}

@ExceptionHandler(ApplicationException.class)

@RequestMapping(value="errorPage401", method=RequestMethod.GET)

@ResponseStatus(value=HttpStatus.UNAUTHORIZED,reason="Unauthorized Request")

public String handleUnauthorizedRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) {

map.addAttribute("http-error-code", HttpStatus.UNAUTHORIZED);

return processErrorCodes(ex,response,map);

}

@ExceptionHandler(ApplicationException.class)

@RequestMapping(value="errorPage404", method=RequestMethod.GET)

@ResponseStatus(HttpStatus.NOT_FOUND)

public String handleNotFoundRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) {

map.addAttribute("http-error-code", HttpStatus.NOT_FOUND);

return processErrorCodes(ex,response,map);

}

@ExceptionHandler(ApplicationException.class)

@RequestMapping(value="errorPage500", method=RequestMethod.GET)

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR,reason="Internal Server Error")

public String handleInternalServerError(ApplicationException ex,HttpServletResponse response, ModelMap map) {

map.addAttribute("http-error-code", HttpStatus.INTERNAL_SERVER_ERROR);

return processErrorCodes(ex,response,map);

}

@ExceptionHandler(ApplicationException.class)

public void handleApplicationExceptions(Throwable exception, HttpServletResponse response) {

}

private String processErrorCodes(ApplicationException ex,HttpServletResponse response, ModelMap map){

map.addAttribute("class", ClassUtils.getShortName(ex.getClass()));

map.addAttribute("message", ex.getMessage());

map.addAttribute("errorMessage", ex.getErrorMessage());

map.addAttribute("errorCode", ex.getErrorCode());

map.addAttribute("timestamp", ex.getCurrentDate());

return "errorPage";

}

}

基础控制器:

@Controller

@RequestMapping("/")

public class BaseController {

// Remember to add any exception that you suspect can be thrown in this web application.

@ExceptionHandler({ApplicationException.class,NullPointerException.class})

public ModelAndView handleException(Throwable exception,HttpServletRequest req) {

ModelMap model = new ModelMap();

ApplicationException ex = new ApplicationException();

String timeStamp = ex.getCurrentDate().toString();

//String temp = ClassUtils.getShortName(ex.getClass());

//model.addAttribute("class", ClassUtils.getShortName(ex.getClass()));

model.addAttribute("timeStamp", timeStamp);

return new ModelAndView("errorPage", model);

}

Web.xml:

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

myApp

400

/errorPage400.xhtml

401

/errorPage401.xhtml

404

/errorPage404.xhtml

500

/errorPage500.xhtml

java.lang.Throwable

/errorPage.xhtml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值