Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the custom filter can not get the session

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today to introduce to you how to solve the problem of custom filters can not get session. The content of the article Xiaobian think good, now to share with you, think there is a need for friends can understand, hope to help everyone, the following with the idea of Xiaobian to read together.

Custom filters get no session

Root cause, multiple custom filter execution sequence issues

problem

The request object in the action request is ShiroHttpServletRequest, and the session content can be retrieved.

However, in the first custom filter, the request object is requestfacade, and the session content cannot be obtained.

reason

Session is managed by shiro. Any custom filter before shiro filter order cannot get session content.

solutions

Put the shiro filter in the first position

The session retrieved by the login interceptor is empty and an interceptor @Configurationpublic class InterceptorConfig implements WebMvcConfigurer { /** * Register Interceptor */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/** .html").excludePathPatterns("/Ylogin.html","/Yindex.html","/YRegister.html"); }} Determine if there is a login

Then at that time, my session.getAttribute("user") was always empty

public class MyInterceptor implements HandlerInterceptor { //Call before request processing (before Controller method call) @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("Start Request Address Intercept"); //Get session HttpSession session = httpServletRequest.getSession(); if (session.getAttribute("user") != null) return true; httpServletResponse.sendRedirect("/Ylogin.html"); return false; } //Called after request processing, but before view is rendered (after Controller method call) @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle called"); } //is invoked after the entire request, i.e. after DispatcherServlet renders the corresponding view (mainly for resource cleanup) @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion called"); }} You can get the session value on another page

But in the interceptor there session null, vexed for a long time, thought he wrote the wrong interceptor, made a long time to finally know, is login.js written wrong. AJAX URL is wrong.

$.ajax({ type: "POST", url: "/user/doLogin", dataType: "json", data:user, async:false, success: function(res) {} })

Because I wrote url:"http://127.0.0.1:8080/user/doLogin" before, just omit the IP address in front, the difference between IP address and localhost

The above is the custom filter to get the session problem how to solve all the content, more and custom filter to get the session problem how to solve the related content can search for the previous article or browse the following article to learn Ha! I believe that Xiaobian will add more knowledge to everyone, I hope you can support it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 243

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report