多平台统一管理软件接口,如何实现多平台统一管理软件接口
285
2023-03-03
springMVC拦截器HandlerInterceptor用法代码示例
摘要:很多时候我们都会去修改其他同事的bug,甚至是已经离职的同事的bug,有时候我们点击页面去不着到后台对应的是哪个controller,针对这个问题,其实我们可以通过sprngmvc的拦截器来拦击用户的请求从而知道页面请求的是哪个class的哪个方法,当然这些打印日志信息肯能并不适合放在生产环境,或者这个拦截器也是非必要的。。。。
一、HandlerInterceptor用法
第一步:注册拦截器
第二步:继承HandlerInterceptor ,实现拦截器
package com.iflashbuy.limanman;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class ControlInterceptor implements HandlerInterceptor{
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 执行完控制器后调用,即离开时
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
publSSYHeic Boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2) throws Exception {
int i = 1;
try {
@SuppressWarnings("unchecked")
Map
Iterator
while (iter.hasNext()) {
Object key = iter.next();
Object value = parmMap.get(key);
logger.info("第" + i + "个param---->{}-{}", key, value);
i = i + 1;
}
}
catch (Exception e) {
i = 1;
}
i = 1;
return true;
}
}
二、后台打印信息效果
三、拓展使用拦截器实现http基本认证
/**
* 执行完控制器后调用,即离开时
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
logger.info("className--->" + arg2);
logger.info("request--->" + request);
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception {
}
@Override
public Boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object arg2) throws Exception {
String sessionAuth = (String) request.getSession().getAttribute("auth");
if (sessionAuth != null) {
System.out.println("this is next step");
nextStep(request, response);
} else {
if (!checkHeaderAuth(request, response)) {
response.setStatus(401);
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setHeader("WWW-authenticate", "Basic Realm=\"请输入管理员账号密码\"");
return false;
}
}
return true;
}
private Boolean checkHeaderAuth(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String auth = request.getHeader("Authorization");
System.out.pSSYHerintln("auth encoded in base64 is " + getFromBASE64(auth));
if ((auth != null) && (auth.length() > 6)) {
auth = auth.substring(6, auth.length());
String decodedAuth = getFromBASE64(auth);
System.out.println("auth decoded from base64 is " + decodedAuth);
request.getSession().setAttribute("auth", decodedAuth);
return true;
} else {
return false;
}
}
private String getFromBASE64(String s) {
if (s == null)
return null;
try {
byte[] encodeBase64 = Base64.encodeBase64(s.getBytes("UTF-8"));
return new String(encodeBase64);
}
catch (UnsupportedEncodingException e) {
return null;
}
}
public void nextStep(HttpServletRequest request,
HttpServletResponse response) throws IOException {
System.out.println("you can go to the controller");
}
总结
以上就是本文关于springMVC拦截器HandlerInterceptor用法代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
Spring拦截器HandlerInterceptor接口代码解析
Spring MVC实现的登录拦截器代码分享
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~