自定义过滤器获取不到session问题

问题现象

action请求中request对象为ShiroHttpServletRequest, 可以取到session内容 

而在第一个自定义过滤器中request对象中或取不到session内容

原因

session由shiro管理,凡是在shiro过滤器顺序之前的自定义过滤器都取不到session内容

根本原因,多个自定义过滤器执行顺序问题

解决方法

将shiro过滤器放在第一个位置

 

 

代码示例

1、过滤器代码

 1     @Override
 2     protected String getUserPin(HttpServletRequest httpServletRequest) {
 3         HttpSession s = httpServletRequest.getSession(false);
 4         if (s != null) {
 5             UserInfoService usersService = (UserInfoService) WebApplicationContextUtils.getWebApplicationContext(httpServletRequest.getServletContext()).getBean("userInfoService");
 6             log.info("usersService#:{}", JSON.toJSONString(usersService));
 7             Long userId = (Long) s.getAttribute(Constants.SESSION_USER_ID);
 8             UserInfo userInfo = usersService.getUserInfoById(Integer.valueOf(userId.toString()));
 9             return userInfo != null ? userInfo.getStaffNo() : "";
10         }
11         return "erp";
12     }
13 
14   
15     @Override
16     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
17         HttpServletRequest req = (HttpServletRequest) servletRequest;
18         String a = req.getRequestURI();
19         if (a.contains(".xls") || a.contains(".xlsx")
20                 || a.contains("export") || a.contains("download")
21                 || a.contains("Export") || a.contains("Download")) {
22             filterChain.doFilter(servletRequest, servletResponse);
23         } else {
24             super.doFilter(servletRequest, servletResponse, filterChain);
25         }
26     }
View Code

2、在application-context.xml 注册 用户bean

1 <bean id="userInfoService" class="com.xxx.biz.impl.UserInfoServiceImpl" />
View Code

3、Shiro过滤器放到最上面

 1 <!-- Apache Shiro -->
 2     <filter>
 3         <filter-name>shiroFilter</filter-name>
 4         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 5         <init-param>
 6             <param-name>targetFilterLifecycle</param-name>
 7             <param-value>true</param-value>
 8         </init-param>
 9     </filter>
10     <filter-mapping>
11         <filter-name>shiroFilter</filter-name>
12         <url-pattern>/*</url-pattern>
13     </filter-mapping>
View Code

 

posted @ 2021-03-09 23:36  二叉树的博客  阅读(748)  评论(0编辑  收藏  举报