一,请求路径的过滤器

SessionExpireFilter.java如下:

package com.innotek.core.support.filter;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;import com.innotek.core.Constants;/********* 会话超时控制过滤器** @author qiuzq*         <p/>*         类功能: 用于ajax和普通请求,会话超时情况下:*         1. ajax进行全局监测complete事件,浏览器通过发现SESSIONSTATUS=TIMEOUT,采取跳转。*         2. 普通请求直接进行redirect。*/public class SessionExpireFilter extends AccessControlFilter {// @Autowired// private UserService userService;@Overrideprotected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {HttpServletRequest req = (HttpServletRequest) request;HttpServletResponse resp = (HttpServletResponse) response;HttpSession session = req.getSession();// 判断session是否失效if (session.getAttribute(Constants.CURRENT_USER) != null) {return true;}// contextPathString cxtPath = req.getContextPath();// XMLHttpRequestString type = req.getHeader("X-Requested-With") == null ? "" : req.getHeader("X-Requested-With");// BASEPATHString basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + cxtPath + "/";if (type.equals("XMLHttpRequest")) {// 处理ajax请求, 设置响应header:超时标识以及重定向路径resp.setHeader("SESSIONSTATUS", "TIMEOUT");resp.setHeader("CONTEXTPATH", basePath + "login.jsp");// 处理ajax请求, 设置状态 为403  未授权resp.setStatus(HttpServletResponse.SC_FORBIDDEN);} else {// 普通请求直接进行redirectresp.sendRedirect(cxtPath + "/login.jsp?TIME_OUT_REQ=1");}//WEB服务上其他受控资源。return false;}@Overrideprotected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)throws Exception {return true;}@Overrideprotected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {return true;}}

二,shiro.xml的文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:util="http://www.springframework.org/schema/util"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://code.alibabatech.lily.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://code.alibabatech.lily.com/schema/dubbo http://code.alibabatech.lily.com/schema/dubbo/dubbo.xsd"><!-- 拦截器使用对象,使用Spring注入 --><dubbo:reference id="sysUserProvider" interface="com.innotek.provider.sys.SysUserProvider" check="false"/><!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 --><bean id="realm" class="com.innotek.core.support.shiro.Realm"/><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="realm"/></bean><!-- 记录菜单 --><bean id="rememberMenuFilter" class="com.innotek.core.support.filter.RememberMenuFilter"><property name="redirectUrl" value="/login.jsp"/></bean><bean id="sessionExpireFilter" class="com.innotek.core.support.filter.SessionExpireFilter" /><!-- Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><!-- Shiro的核心安全接口,这个属性是必须的 --><property name="securityManager" ref="securityManager"/><!-- 要求登录时的链接 --><property name="loginUrl" value="/login.jsp"/><!-- 用户访问未对其授权的资源时,所显示的连接 --><property name="unauthorizedUrl" value="/forbidden"/><!-- Shiro连接约束配置,即过滤链的定义 --><!-- anon:它对应的过滤器里面是空的,什么都没做 --><!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter --><property name="filters"><util:map><entry key="rememberMenu" value-ref="rememberMenuFilter"></entry><entry key="sessionExpire" value-ref="sessionExpireFilter"></entry></util:map></property><property name="filterChainDefinitions"><value>/login.jsp = anon/login=anon/noPermission.jsp = anon/sunflower/bill/getParkFee*=anon/logout = anon/*.ico=anon/upload/*=anon/theme/**=anon/unauthorized=anon/forbidden=anon/**=user,sessionExpire,authc,perms,rememberMenu</value></property></bean><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
</beans>

三、记住点击过的菜单

RememberMenuFilter.java如下:

package com.innotek.core.support.filter;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;
import org.apache.shiro.web.servlet.AdviceFilter;
import org.apache.shiro.web.util.WebUtils;import com.innotek.core.Constants;
import com.innotek.core.support.data.permission.model.UserSession;
import com.innotek.core.util.JsonUtil;
import com.innotek.core.util.RedisUtil;
import com.innotek.core.util.WebUtil;
import com.innotek.model.cfg.generator.Paramter;/**** 记住点击哪个菜单*/
public class RememberMenuFilter extends AdviceFilter {private static Logger logger = Logger.getLogger(RememberMenuFilter.class);private String redirectUrl = "/login.jsp";@Overrideprotected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {HttpServletRequest req = (HttpServletRequest) request;UserSession currentUserSession = WebUtil.getCurrentUserSession();if (currentUserSession == null) {WebUtils.issueRedirect(request, response, getRedirectUrl());return false;}//是否显示工作台Integer status = WebUtil.getCurrentUserSession().getShowStatus();req.getSession().setAttribute("showStatus", status);req.getSession().setAttribute("userName", currentUserSession.getUserName());String systemArray = JsonUtil.list2json(WebUtil.getCurrentUserSession().getSystemManageSession());req.getSession().setAttribute("systemList", systemArray);//返回工作台路径Paramter param = (Paramter) RedisUtil.getNoExpiry(Constants.PARAMTER_CACHE + "WORK_URL");if (param != null) {req.getSession().setAttribute("workUrl", param.getParamValue());}return true;}/*** 页面跳转** @return*/public String getRedirectUrl() {return redirectUrl;}public void setRedirectUrl(String redirectUrl) {this.redirectUrl = redirectUrl;}
}

四、然后再spring.xml配置文件中加入

<import resource="spring/shiro-cas.xml"/>

即可,

Shiro和Spring结合对请求路径进行过滤和记住我过滤操作相关推荐

  1. Spring Boot 2.1之后如何在启动日志中打印请求路径列表

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | http://blog.di ...

  2. Spring Boot 2.x基础教程:找回启动日志中的请求路径列表

    如果您看过之前的Spring Boot 1.x教程,或者自己原本就对Spring Boot有一些经验,或者对Spring MVC很熟悉.那么对于Spring构建的Web应用在启动的时候,都会输出当前应 ...

  3. Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】

    Spring与Shiro整合 导入jar包 shiro-web的jar. shiro-spring的jar shiro-code的jar 快速入门 shiro也通过filter进行拦截.filter拦 ...

  4. Spring MVC 接收请求参数所有方式总结!

    来源:简书,作者:zhrowable 链接:https://www.jianshu.com/p/5f6abd08ee08 SpringMVC请求参数接收 其实一般的表单或者JSON数据的请求都是相对简 ...

  5. java webmethod 参数_java详解Spring接收web请求参数的方式

    本篇文章给大家带来的内容是java详解Spring接收web请求参数的方式 .有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助. 1 查询参数 请求格式:url?参数1=值1&参 ...

  6. springboot控制台日志打印到日志文件 (请求路径,执行sql,参数等)

    经过几天的尝试 重终于把这个东西肝出来了 目录结构 我添加太多pom了 忘记有哪些了 也懒得找了 也就是常用的那几个吧 依赖pom 小伙子们自己添加吧!!! 首先你要把你项目跑起来 配置数据源如下 s ...

  7. Shiro和Spring Security的简单对比

    Shiro和Spring Security是Java中常用的两种安全认证框架,安全认证主要包括认证和授权鉴权两部分. 认证指应用程序验证一个用户信息的过程,应用程序需要确认当前的用户是否是合法的用户, ...

  8. 【开发技术】2万字分析shiro、spring security两大安全框架,spring session,OAuth2 入门级教程

    SpringBoot 内容管理 Shiro 创建demo 用户[用户包括token ,角色,权限] :fist_oncoming: Shiro配置 配置5个基本对象 + 3额外对象 路径匹配有先后 : ...

  9. Spring Security-限制请求

    前言 上面只是验证了用户,并且还可以基于用户赋予了不同的角色,但是对于不同的角色而言其访问的权限也是不一样的.例如,一个网站可能存在普通用户和管理员用户,管理员用户拥有的权限会比破童用户大的多,所以用 ...

最新文章

  1. FFmpeg for ios架构:中级
  2. mt65xx android phone win10驱动,mt65xx android phone驱动下载
  3. php给留言分配id_php留言板更新代码
  4. python爬虫-爬取今日头条(图集)
  5. 01-2.C(C程序编译步骤-gcc 编译过程)
  6. UVA4671 K-neighbor substrings FFT+字符串hash
  7. checkboxlist与数组结合用的技
  8. eclipse服务器添加项目工程,eclipse创建和发布web项目(示例代码)
  9. c2c旅游springboot开源_7个开源的 Spring Boot 前后端分离项目,一定要收藏!
  10. Ajax学习总结(2)——Ajax参数详解及使用场景介绍
  11. LeetCode 93. Restore IP Addresses
  12. java多线程——CAS
  13. 图像的缩放,旋转,转置和镜像
  14. Inceptor一次执行多个存储过程
  15. 论文阅读笔记——Adversarial Attack on Attackers Post-Process to
  16. POJO类中属性必须使用包装数据类型
  17. java math pow_Java Math.pow() 方法
  18. oracle水仙花,希腊神话中的水仙花和回声
  19. 机器人唱歌bgm_变形金刚5所有背景乐插曲盘点 长老机器人弹钢琴BGM是什么歌
  20. 华为鸿蒙是诺基亚,诺基亚、三星前车之鉴,华为鸿蒙必须做好这两点

热门文章

  1. 元宇宙浪潮之下,数字身份至关重要
  2. PyqtGraph中ImageView的ROI里边是什么信号,有些懵逼
  3. PDF阅读器还是福昕阅读好
  4. Conformal ECO 流程介绍
  5. Jquery-validate无法验证,出现相应表单has no name assigned
  6. 价值8800元SEO自动化养权重流量站课程分享(升级版)!
  7. 【Python 解压的几种方法】
  8. 彩票中奖概率计算过程
  9. 乐器的音色与交响乐的欣赏
  10. Java常用类库API