shiro web 集成

集成方法

  shiro与web集成,主要是通过配置一个ShiroFilter拦截所有URL,其中ShiroFilter类似于SpringMVC的前端控制器,是所有请求入口点,负责根据配置(如ini配置文件),判断请求进入URL是否需要登录/权限等工作。


集成步骤

  1、导入相关jar包
  2、web.xml配置ShiroFilter以拦截请求
  3、配置shiro.int文件


shiro默认的过滤器

这些默认的拦截器会自动注册

过滤器简称  对应的java类 用法

anon,authcBasic,auchc,user是认证过滤器
perms,roles,ssl,rest,port是授权过滤器


authc 拦截器主要属性:
usernameParam ----> 表单提交的用户名参数名(username)
passwordParam ----> 表单提交的密码参数名(password)
rememberMeParam ----> 记住我(rmemberMe)
loginUrl ----> 登陆页面地址(/login.jsp)
successUrl ----> 登陆成功后默认的重定向地址
failureKeyAttribute ----> 登陆失败后错误信息存储key(shiroLoginFailure)

anon org.apache.shiro.web.filter.authc.AnonymousFilter 匿名拦截器,即不登陆就可以访问,一般用于静态资源过滤
authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter 需要认证
authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter  
roles org.apache.shiro.web.filter.authc.RolesAuthorizationFilter 角色授权拦截器
perms org.apache.shiro.web.filter.authc.PermissionsAuthorizationFilter 权限授权拦截器
user org.apache.shiro.web.filter.authc.UserFilter  
logout org.apache.shiro.web.filter.authc.LogoutFilter 退出拦截器
port org.apache.shiro.web.filter.authc.PortFilter  示例“/test=port[80]”,如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口
rest org.apache.shiro.web.filter.authc.HttpMethodPermissionFilter  
ssl org.apache.shiro.web.filter.authc.SslFilter  SSL拦截器,只有请求协议是https才能通过;否则自动跳转会https端口(443)


在ini配置文件中设置默认拦截器的属性

#如果身份验证没有通过,就跳转到loginUrl指定的页面
authc.loginUrl=/login 
#如果用户没有角色,就跳转到unauthorizedUrl指定的页面
roles.unauthorizedUrl=/nopermission.jsp 
#如果用户没有权限,就跳转到unauthorizedUrl指定的页面
perms.unauthorizedUrl=/nopermission.jsp

#另外如果某个拦截器不想使用了可以直接通过如下配置直接禁用:
#perms.enabled=false 

在ini配置文件中设置访问权限

原文地址:https://www.cnblogs.com/Mike_Chang/p/9879840.html