Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Unified handling of all exceptions in the website by the Web layer framework

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

A website exception information as a professional person, is not easily exposed to users, because that is not safe, it seems that you leak is one thing, as long as you still consider the data security issues of the website, the following to share with you some common web layer framework is how to handle unified exceptions.

Before Struts2 configuration file is configured in the interceptor to deal with all the exceptions in the system, to bring you some pseudo-code bar, at least once used, ha ha.

The profile configuration information is as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /common/error.jsp17 18 /common/refuse.jsp19 20 21 26 27 /business/{1}/{2}.jsp28 29

Java class pseudocode is as follows:

ExceptionInterceptor serialVersionUID = -3570177760250354837L String intercept(ActionInvocation actionInvocation) HttpServletRequest request = String result = result = } ExceptionResultInfo exceptionResultInfo = (e exceptionResultInfo = } (e UnknownAccountException || e ResultInfo resultInfo = ResultUtil.createFail("Wrong username or password! " exceptionResultInfo = } (e ResultInfo resultInfo = ResultUtil.createFail("No permissions! " exceptionResultInfo =

First of all, let's talk about the way springmvc handles unified exceptions. At present, there are three ways I know: directly using the exception handler provided by springmvc, manually configuring the custom global exception handler and using annotations (this method has not been used before).

Let's talk about the first one, which is to configure the simple exception handler provided by springmvc itself directly in the core configuration file of springmvc.

1 2

The second is to define an exception handler yourself and configure it in the core configuration file of springmvc.

The custom exception handler class pseudocode is as follows:

1 /** 2 * Custom global exception handler: 3 */ 4 public class CustomExceptionResolver implements HandlerExceptionResolver { 5 @Override 6 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, 7 Exception ex) { 8 9 ex.printStackTrace();10 CustomException customException = null;11 //If the exception is a custom exception, convert directly to 12 if(ex instanceof CustomException) {13 customException = (CustomException) ex;14 } else {15 //If the exception thrown is not custom, reconstruct an unknown error exception 16 new SysException("The system is under maintenance, please try again later... ");17 }18 //error message returned to foreground 19 ModelAndView modelAndView = new ModelAndView("exception");20 modelAndView.addObject("message", customException.getMessage());21 return modelAndView;22 }23 }

The springmvc configuration file configuration information is as follows (remember to modify the package name when you have to copy it):

1 2

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report