这里说第一种方法
创建类然后实现HandlerInterceptor
先上代码
package cn.com.doone.sc.tx.cloud.dyb.system.handle;
import cn.com.doone.sc.tx.cloud.dyb.system.bean.po.member.MemberPo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
-
@Auther: liufei
-
@Date: 2020/7/1 09:28
-
@Description:
*/
@Component
public class AuthInterceptor implements HandlerInterceptor {@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println(“开始拦截…”);
String requestURI = request.getRequestURI();
if(requestURI.startsWith("/static")||requestURI.startsWith("/error")){
return true;
}
HttpSession session = request.getSession(true);
MemberPo memberPo = (MemberPo)session.getAttribute("");
if(memberPo!=null){
UserContext.setUser(memberPo);
}
//业务代码
return true;
}/**
- 后处理回调方法,实现处理器(controller)的后处理,但在渲染视图之前
- 此时我们可以通过modelAndView对模型数据进行处理或对视图进行处理
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**- 整个请求处理完毕回调方法,即在视图渲染完毕时回调,
- 如性能监控中我们可以在此记录结束时间并输出消耗时间,
- 还可以进行一些资源清理,类似于try-catch-finally中的finally,
- 但仅调用处理器执行链中
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
UserContext.remove();
}
}
然后讲解
然后创建一个类
将刚刚的拦截器注入
package cn.com.doone.sc.tx.cloud.dyb.system.handle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
-
@Auther: liufei
-
@Date: 2020/7/1 09:54
-
@Description:
*/
@Configuration
public class MvcInterceptorConfig extends WebMvcConfigurationSupport {@Autowired
private AuthInterceptor authInterceptor;@Override
protected void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则,/表示拦截所有请求
// excludePathPatterns 用户排除拦截
registry.addInterceptor(authInterceptor).addPathPatterns("/")
.excludePathPatterns("/stuInfo/getAllStuInfoA","/account/register");
super.addInterceptors(registry);
}
}
这里讲解下上面
二
如果我们这边要弄多个拦截器
比如说我这里在加一个拦截器
package cn.com.doone.sc.tx.cloud.dyb.system.handle;
import cn.com.doone.sc.tx.cloud.dyb.system.bean.po.member.MemberPo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
/**
-
@Auther: liufei
-
@Date: 2020/7/1 10:55
-
@Description:
*/
@Component
public class AuthActionInterceptor implements HandlerInterceptor {@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println(“开始拦截1…1”);
MemberPo memberPo = UserContext.getUser();
if(memberPo==null){
String mString = URLEncoder.encode(“请先登录”, “utf-8”);
/* String target = URLEncoder.encode(request.getRequestURL().toString(), “uft-8”);*/
returnJson(response,"{“code”:400,“msg”:“请登录!”}");
return false;
}else{
return true;
}
}/**
- 后处理回调方法,实现处理器(controller)的后处理,但在渲染视图之前
- 此时我们可以通过modelAndView对模型数据进行处理或对视图进行处理
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**- 整个请求处理完毕回调方法,即在视图渲染完毕时回调,
- 如性能监控中我们可以在此记录结束时间并输出消耗时间,
- 还可以进行一些资源清理,类似于try-catch-finally中的finally,
- 但仅调用处理器执行链中
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
/返回客户端数据/
private void returnJson(HttpServletResponse response, String json) throws Exception{
PrintWriter writer = null;
response.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html; charset=utf-8”);
try {
writer = response.getWriter();
writer.print(json);} catch (IOException e) { } finally { if (writer != null){ writer.close();} }
}
}
我这里需要第一个拦截器在第二个拦截器之前拦截
这里就需要这样设置
package cn.com.doone.sc.tx.cloud.dyb.system.handle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
-
@Auther: liufei
-
@Date: 2020/7/1 09:54
-
@Description:
*/
@Configuration
public class MvcInterceptorConfig extends WebMvcConfigurationSupport {@Autowired
private AuthInterceptor authInterceptor;
@Autowired
private AuthActionInterceptor authActionInterceptor;@Override
protected void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则,/表示拦截所有请求
// excludePathPatterns 用户排除拦截
registry.addInterceptor(authInterceptor).addPathPatterns("/")
.excludePathPatterns("/stuInfo/getAllStuInfoA","/account/register");
registry.addInterceptor(authActionInterceptor).addPathPatterns("/**")
.excludePathPatterns("/stuInfo/getAllStuInfoA","/account/register");
super.addInterceptors(registry);
}
}
这里稍微讲解下