添加自定义注解
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,250 条评论
Everything is very open with a really clear explanation of the issues. It was definitely informative. Your website is very useful. Thank you for sharing!
You need to be a part of a contest for one of the finest websites online. I am going to highly recommend this website!
Hi there! I just would like to offer you a huge thumbs up for the excellent info you have right here on this post. I will be returning to your site for more soon.
Nice post. I learn something new and challenging on blogs I stumbleupon every day. Its always useful to read through content from other writers and use a little something from their websites.
A fascinating discussion is worth comment. I do think that you ought to publish more about this subject matter, it might not be a taboo matter but generally people do not discuss these issues. To the next! Best wishes!!
An interesting discussion is worth comment. Theres no doubt that that you need to write more on this subject matter, it might not be a taboo matter but usually people dont speak about such subjects. To the next! Many thanks!!
Ofefbjawkdefnk jnjfanfkdwnafjkewnfjkew jfejknfewjnfjkewfngewjkfj nkjfendwfjknewjkgnwejkfnejn jfenwwjfnjdgbrkjfnklfne jnfewwjfnewjkf https://uufewhdwjidewfhjfkmsdjfejgbrjefkd.com
I was very pleased to uncover this great site. I need to to thank you for ones time for this fantastic read!! I definitely appreciated every bit of it and I have you bookmarked to look at new information on your blog.
Good write-up. I definitely appreciate this website. Continue the good work!
Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. Its always useful to read content from other authors and practice something from their websites.
A motivating discussion is definitely worth comment. Theres no doubt that that you need to write more on this subject matter, it may not be a taboo matter but generally folks dont discuss such subjects. To the next! Kind regards!!
Greetings! Very useful advice within this article! Its the little changes that make the most significant changes. Thanks a lot for sharing!
Itís nearly impossible to find educated people in this particular subject, however, you seem like you know what youíre talking about! Thanks
Купить полипропиленовые трубы для газа – только у нас вы найдете качественную продукцию. Быстрей всего сделать заказ на труба пнд 180 можно только у нас!
[url=https://pnd-truba-sdr-17.ru/]труба пнд 16[/url]
трубы пнд 20 – [url=]https://www.pnd-truba-sdr-17.ru/[/url]
[url=https://www.apalanj.org/?URL=pnd-truba-sdr-17.ru]https://google.be/url?q=https://pnd-truba-sdr-17.ru[/url]
[url=https://buyoneer.co/2022/09/17/hello-world/#comment-52]Труба пнд пэ 80 – у нас большой выбор фитингов для труб ПНД ПЭ любых размеров и диаметров.[/url] a494d5b
fucking milf tit
https://www.rolleriklubi.net/proxy.php?link=https://tubesweet.xyz/categories/
https://w.everbikini.com/member/login.html?noMemberOrder=&returnUrl=http%3a%2f%2ftubesweet.xyz
https://7ba.ru/out.php?url=https://tubesweet.xyz/categories/
https://privatelink.de/?https://tubesweet.xyz/categories/