shiro-过滤器
http://shiro.apache.org/authorization.html#Authorization-PermissionGranularity
shiro默认的过滤器
Shiro内置了很多默认的拦截器,比如身份验证、授权等相关的。默认拦截器可以参考org.apache.shiro.web.filter.mgt.DefaultFilter中的枚举拦截器:
默认拦截器名 |
拦截器类 |
说明(括号里的表示默认值) |
身份验证相关的 |
||
authc |
org.apache.shiro.web.filter.authc .FormAuthenticationFilter |
基于表单的拦截器;如“/**=authc”,如果没有登录会跳到相应的登录页面登录;主要属性:usernameParam:表单提交的用户名参数名( username); passwordParam:表单提交的密码参数名(password); rememberMeParam:表单提交的密码参数名(rememberMe); loginUrl:登录页面地址(/login.jsp);successUrl:登录成功后的默认重定向地址; failureKeyAttribute:登录失败后错误信息存储key(shiroLoginFailure); |
authcBasic |
org.apache.shiro.web.filter.authc .BasicHttpAuthenticationFilter |
Basic HTTP身份验证拦截器,主要属性: applicationName:弹出登录框显示的信息(application); |
logout |
org.apache.shiro.web.filter.authc .LogoutFilter |
退出拦截器,主要属性:redirectUrl:退出成功后重定向的地址(/);示例“/logout=logout” |
user |
org.apache.shiro.web.filter.authc .UserFilter |
用户拦截器,用户已经身份验证/记住我登录的都可;示例“/**=user” |
anon |
org.apache.shiro.web.filter.authc .AnonymousFilter |
匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤;示例“/static/**=anon” |
授权相关的 |
||
roles |
org.apache.shiro.web.filter.authz .RolesAuthorizationFilter |
角色授权拦截器,验证用户是否拥有所有角色;主要属性: loginUrl:登录页面地址(/login.jsp);unauthorizedUrl:未授权后重定向的地址;示例“/admin/**=roles[admin]” |
perms |
org.apache.shiro.web.filter.authz .PermissionsAuthorizationFilter |
权限授权拦截器,验证用户是否拥有所有权限;属性和roles一样;示例“/user/**=perms["user:create"]” |
port |
org.apache.shiro.web.filter.authz .PortFilter |
端口拦截器,主要属性:port(80):可以通过的端口;示例“/test= port[80]”,如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口,其他路径/参数等都一样 |
rest |
org.apache.shiro.web.filter.authz .HttpMethodPermissionFilter |
rest风格拦截器,自动根据请求方法构建权限字符串(GET=read, POST=create,PUT=update,DELETE=delete,HEAD=read,TRACE=read,OPTIONS=read, MKCOL=create)构建权限字符串;示例“/users=rest[user]”,会自动拼出“user:read,user:create,user:update,user:delete”权限字符串进行权限匹配(所有都得匹配,isPermittedAll); |
ssl |
org.apache.shiro.web.filter.authz .SslFilter |
SSL拦截器,只有请求协议是https才能通过;否则自动跳转会https端口(443);其他和port拦截器一样; |
其他 |
||
noSessionCreation |
org.apache.shiro.web.filter.session .NoSessionCreationFilter |
不创建会话拦截器,调用 subject.getSession(false)不会有什么问题,但是如果 subject.getSession(true)将抛出 DisabledSessionException异常; |
另外还提供了一个org.apache.shiro.web.filter.authz.HostFilter,即主机拦截器,比如其提供了属性:authorizedIps:已授权的ip地址,deniedIps:表示拒绝的ip地址;不过目前还没有完全实现,不可用。
这些默认的拦截器会自动注册,可以直接在ini配置文件中通过“拦截器名.属性”设置其属性:
- perms.unauthorizedUrl=/unauthorized
另外如果某个拦截器不想使用了可以直接通过如下配置直接禁用:
- perms.enabled=false
xml 表中格过滤器的汗液
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 要求登录时的链接(可根据项目的URL进行替换),非必须的属性,默认会自动寻找Web工程根目录apos.htmlhtml"页面 -->
<property name="loginUrl" value="/sys/manager/login"/>
<!-- 登录成功后要跳转的连接 -->
<property name="successUrl" value="/sys/manager/index"/>
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<!-- 若想更明显的测试此属性可以修改它的值,如unauthor.jsp-->
<property name="unauthorizedUrl" value="/sys/manager/login"/>
<property name="filters">
<map>
<entry key="authc">
<bean class="com.xx.web.shiro.UserFormAuthenticationFilter" />
</entry>
</map>
</property> <!-- Shiro连接约束配置,即过滤链的定义 -->
<!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
<!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->
<!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
<property name="filterChainDefinitions">
<value>
/statics/**=anon
/js/**=anon
/page/**=anon
/sys/manager/login=anon
/favicon.ico=anon
/**=authc
</value>
</property>
</bean>
过滤器解决的问题
1.登录session失效
自定义一个过滤器
public class LoginAuthenticationFilter extends FormAuthenticationFilter { @Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
if(this.isLoginRequest(request, response)) {
if(this.isLoginSubmission(request, response)) {
return this.executeLogin(request, response);
} else {
return true;
}
} else {
if(isAjax(request)){
Result result = Result.notLogin();
response.getWriter().print(JSONObject.toJSON(result));
}else{
this.saveRequestAndRedirectToLogin(request, response);
}
return false;
}
} public static boolean isAjax(ServletRequest request){
String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
if("XMLHttpRequest".equalsIgnoreCase(header)){
//System.out.println( "当前请求为Ajax请求");
return Boolean.TRUE;
}
//System.out.println( "当前请求非Ajax请求");
return Boolean.FALSE;
}
}
2.过滤器的信息注入到shiro的filter bean中
@Bean
public ShiroFilterFactoryBean shiroFilter(ShiroProperties shiroProperties) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager(shiroProperties)); //注册过滤器
Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
//退出登录跳转
LogoutFilter logoutFilter = new LogoutFilter();
logoutFilter.setRedirectUrl(shiroProperties.getLogoutUrl());
filters.put("logout", logoutFilter);
//登录超时过滤
LoginAuthenticationFilter loginAuthenticationFilter = new LoginAuthenticationFilter();
filters.put("authc", loginAuthenticationFilter); shiroFilterFactoryBean.setFilters(filters); Map<String, String> filterChainDefinitionManager = shiroProperties.getFilterChainDefinitions();
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionManager); shiroFilterFactoryBean.setLoginUrl(shiroProperties.getLoginUrl());
shiroFilterFactoryBean.setSuccessUrl(shiroProperties.getSuccessUrl());
shiroFilterFactoryBean.setUnauthorizedUrl(shiroProperties.getUnauthorizedUrl()); return shiroFilterFactoryBean;
}
重写js的ajax方法,自动让所有的ajax提交都能绑定上跳转
http://www.cnblogs.com/hwaggLee/p/8244631.html
shiro-过滤器的更多相关文章
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
Spring与Shiro整合 导入jar包 shiro-web的jar. shiro-spring的jar shiro-code的jar 快速入门 shiro也通过filter进行拦截.filter拦 ...
- Shiro【授权、整合Spirng、Shiro过滤器】
前言 本文主要讲解的知识点有以下: Shiro授权的方式简单介绍 与Spring整合 初始Shiro过滤器 一.Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro ...
- shiro过滤器详解分析
(原) shiro最核心的2个操作,一个是登录的实现,一就是过滤器了.登录有时间再补录说明,这里分析下shiro过滤器怎样玩的. 1.目标 这里会按如下顺序逐一看其实原理,并尽量找出其出处. 先看一下 ...
- Spring Boot环境下自定义shiro过滤器会过滤所有的url的问题
在配置shiro过滤器时增加了自定义的过滤器,主要是用来处理未登录状态下返回一些信息 //自定义过滤器 Map<String, Filter> filtersMap = new Linke ...
- Shiro过滤器的维护与匹配执行
servlet的初始化会触发核心过滤器的创建: public Object getObject() throws Exception { if (instance == null) { instanc ...
- Spring Boot 自定义 Shiro 过滤器,无法使用 @Autowired 解决方法
在 Spring Boot 中集成 Shiro,并使用 JWT 进行接口认证. 为了统一对 Token 进行过滤,所以自定义了一个 JwtTokenFilter 过滤器. 期间遇到了以下几个问题,这里 ...
- shiro过滤器机制
shiro内置过滤器介绍 https://blog.csdn.net/qq_35608780/article/details/71703197 Shiro的Filter机制详解---源码分析 http ...
- Shiro过滤器
Shiro内置过滤器 anon.authBasic.authc.user.logout perms.roles.ssl.port spring.xml <bean id="shiroF ...
- shiro过滤器解释类
anon -- org.apache.shiro.web.filter.authc.AnonymousFilter authc -- org.apache.shiro.web.filter.authc ...
- shiro过滤器过滤属性含义
securityManager:这个属性是必须的. loginUrl :没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/lo ...
随机推荐
- log4j.xml 报告集成
等级: trace< debug<info<warn<error<fatal trace 追踪 deug: eclipse, Log4j配置 [1]从零开始 a). ...
- php第二天 开始连接数据库
php连接数据库有三种方法,分别是mysqli面向对象,mysqli面向过程,pdo. 1.查了资料,最终选择则了mysqli面向过程的方式,运行效率应该要高一些. 代码如下 <?php $se ...
- window下用taskkill杀死进程
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid | /IM imagename] } ...
- Spring Security(15)——权限鉴定结构 RoleVoter
http://www.cnblogs.com/fenglan/p/5913432.html
- STC15W408AS简单使用教程-简单的光强检测!
第一步:搭建开发环境 安装最新版本的STC_ISP程序烧录软件,链接:http://pan.baidu.com/s/1slLPnOD 密码:6bov 安装keil C51的51系列单片机集成IDE软件 ...
- 解决资源id冲突
--摘自<android插件化开发指南> 1.一套完整的Android App打包流程(Gradle方案) 第一步:aapt.为res目录下的资源生成R.java文件,同时为Android ...
- Square Destroyer-POJ 1084 (IDA*)
Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...
- JDK 1.6 HashMap 源码分析
前言 前段时间研究了一下JDK 1.6 的 HashMap 源码,把部份重要的方法分析一下,当然HashMap中还有一些值得研究得就交给读者了,如有不正确之处还望留言指正. 准备 需要熟悉数组 ...
- Is there a TRY CATCH command in Bash
Is there a TRY CATCH command in Bash? No. Bash doesn't have as many luxuries as one can find in many ...
- 移动端小坑:用户长按H5文字出现复制
禁止复制方法:*{ -webkit-user-select: none;/*禁用手机浏览器的用户选择功能 */ -moz-user-select: none; -webkit-touch-callou ...