欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springMVC拦截器HandlerInterceptor用法代码示例

程序员文章站 2023-12-17 10:12:40
摘要:很多时候我们都会去修改其他同事的bug,甚至是已经离职的同事的bug,有时候我们点击页面去不着到后台对应的是哪个controller,针对这个问题,其实我们可以通过s...

摘要:很多时候我们都会去修改其他同事的bug,甚至是已经离职的同事的bug,有时候我们点击页面去不着到后台对应的是哪个controller,针对这个问题,其实我们可以通过sprngmvc的拦截器来拦击用户的请求从而知道页面请求的是哪个class的哪个方法,当然这些打印日志信息肯能并不适合放在生产环境,或者这个拦截器也是非必要的。。。。

一、handlerinterceptor用法

第一步:注册拦截器

<!-- 注册拦截器 -->
	<mvc:interceptors>
		<bean class="com.project.base.interceptor.controlinterceptor" />
	</mvc:interceptors>

第二步:继承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
		public boolean prehandle(httpservletrequest request, httpservletresponse arg1, object arg2) throws exception {
		int i = 1;
		try {
			@suppresswarnings("unchecked")
						map<string, object> parmmap = request.getparametermap();
			iterator<string> iter = parmmap.keyset().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;
	}
}

二、后台打印信息效果

springMVC拦截器HandlerInterceptor用法代码示例

三、拓展使用拦截器实现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.println("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实现的登录拦截器代码分享

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

上一篇:

下一篇: