添加自定义注解
package org.book.configuration.aop; import java.lang.annotation.*; @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface NoCheckLogin { }
创建一个SpringMvcHandler 类,继承HandlerInterceptorAdapter
package org.book.configuration.Handler; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.book.common.JsonResult; import org.book.configuration.aop.NoCheckLogin; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import java.lang.reflect.Method; @Slf4j public class SpringMvcHandler extends HandlerInterceptorAdapter { public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod hm = null; Method method = null; if(handler instanceof HandlerMethod) { hm = (HandlerMethod) handler; method = hm.getMethod();// 获取方法 } else { return true; } // 该方法是否添加 允许未登录访问注解 if (method !=null && method.isAnnotationPresent(NoCheckLogin.class)) { // 不拦截 return true; } String name = (String) request.getSession().getAttribute("user"); if (name != null) { return true; } else { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); PrintWriter out = null; try { out = response.getWriter(); out.append(new JSONObject().toJSONString(JsonResult.renderFail("用户未登录")) ); }catch (Exception e){ out = response.getWriter(); out.append(new JSONObject().toJSONString(JsonResult.renderError("后台出错"))); log.info(e.getStackTrace().toString()); } return false; } } }
被@NoLoginCheck注解的方法不会被拦截在springmvc配置文件配置拦截路径和排除路径
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:mapping path="/"/> <mvc:mapping path="/index.jsp"/> <mvc:exclude-mapping path="/assets/**"/> <mvc:exclude-mapping path="/images/**"/> <bean class="org.book.configuration.Handler.SpringMvcHandler"></bean> </mvc:interceptor> </mvc:interceptors>
1)预处理preHandle()方法
用户发送请求时,先执行preHandle()方法。会先按照顺序执行所有拦截器的preHandle方法,一直遇到return false为止,比如第二个preHandle方法是return false,则第三个以及以后所有拦截器都不会执行。若都是return true,则执行用户请求的url方法。
2)后处理postHandle()
方法调用了Service并返回ModelAndView,但未进行页面渲染,可以在这里继续修改ModelAndView
3)返回处理afterCompletion()
方法已经渲染了页面,在afterCompletion中,可以根据ex是否为null判断是否发生了异常,进行日志记录。注:一般使用preHandle这个拦截器进行预处理,对url进行请求拦截
Comments | 12,899 条评论
博客作者 canada pharmaceuticals
Way cool! Some extremely valid points! I appreciate you writing this write-up and also the rest of the website is also really good.
博客作者 cialis generic pharmacy online
What’s up colleagues, its impressive article about tutoringand fully defined, keep it up all the time.
博客作者 canadian pharmacy drugs online
Great post. I was checking continuously this blog and I’m inspired! Extremely useful info particularly the closing part :) I care for such info much. I used to be seeking this certain information for a long time. Thanks and best of luck.
博客作者 現場兒童色情片
Ddarbu kelapra çürpa only rape porn
博客作者 canadian pharmaceuticals online safe
WOW just what I was searching for. Came here by searching for %meta_keyword%
博客作者 shoppers drug mart canada
Hello there! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really appreciate your content. Please let me know. Thanks
博客作者 Ritaanzg
You’re planning to create and looking for ideas to design your [url=https://studio-sign.ru]signature[/url]? Learn more about techniques to help develop your signature.
博客作者 canadian pharmaceutical companies
Hi, just wanted to tell you, I loved this post. It was inspiring. Keep on posting!
博客作者 pharmacy discount
Good write-up. I definitely love this website. Thanks!
博客作者 canada pharmaceuticals online
I am regular visitor, how are you everybody? This article posted at this site is truly nice.
博客作者 pharmacy discount
Hi there, this weekend is fastidious in support of me, because this occasion i am reading this great educational paragraph here at my home.
博客作者 navarro pharmacy
This is very interesting, You are a very skilled blogger. I’ve joined your feed and look forward to seeking more of your excellent post. Also, I have shared your web site in my social networks!
博客作者 Starzbet Güncel Giriş
Great post thank you. Hello Administ . Seo Paketi Skype: ByuMuT@KRaLBenim.Com –– live:by_umut
博客作者 Starzbet Güncel Giriş
Thank you great post. Hello Administ .Seo Paketi Skype: ByuMuT@KRaLBenim.Com –– live:by_umut
博客作者 Starzbet Giriş
Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across.Seo Paketi Skype: ByuMuT@KRaLBenim.Com –– live:by_umut