[转] shiro简单配置
shiro(1)
注:这里只介绍spring配置模式。
因为官方例子虽然中有更加简洁的ini配置形式,但是使用ini配置无法与spring整合。而且两种配置方法一样,只是格式不一样。
涉及的jar包
Jar包名称
版本
核心包shiro-core
1.2.0
Web相关包shiro-web
1.2.0
缓存包shiro-ehcache
1.2.0
与spring整合包shiro-spring
1.2.0
Ehcache缓存核心包ehcache-core
2.5.3
Shiro自身日志包slf4j-jdk14
1.6.4
使用maven时,在pom中添加依赖包
[html] view plain copy
在CODE上查看代码片派生到我的代码片
Spring整合配置
1.在web.xml中配置shiro的过滤器
[html] view plain copy
在CODE上查看代码片派生到我的代码片
org.springframework.web.filter.DelegatingFilterProxy
2.在Spring的applicationContext.xml中添加shiro配置
[html] view plain copy
在CODE上查看代码片派生到我的代码片
/home* = anon
/ = anon
/logout = logout
/role/** = roles[admin]
/permission/** = perms[permssion:look]
/** = authc
securityManager:这个属性是必须的。
loginUrl:没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。
successUrl:登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。
unauthorizedUrl:没有权限默认跳转的页面。
过滤器简称
过滤器简称
对应的java类
anon
org.apache.shiro.web.filter.authc.AnonymousFilter
authc
org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
perms
org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port
org.apache.shiro.web.filter.authz.PortFilter
rest
org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles
org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl
org.apache.shiro.web.filter.authz.SslFilter
user
org.apache.shiro.web.filter.authc.UserFilter
logout
org.apache.shiro.web.filter.authc.LogoutFilter
anon:例子/admins/**=anon 没有参数,表示可以匿名使用。
authc:例如/admins/user/**=authc表示需要认证(登录)才能使用,没有参数
roles:例子/admins/user/=roles[admin],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,当有多个参数时,例如admins/user/=roles["admin,guest"],每个参数通过才算通过,相当于hasAllRoles()方法。
perms:例子/admins/user/=perms[user:add:*],参数可以写多个,多个时必须加上引号,并且参数之间用逗号分割,例如/admins/user/=perms["user:add:,user:modify:"],当有多个参数时必须每个参数都通过才通过,想当于isPermitedAll()方法。
rest:例子/admins/user/=rest[user],根据请求的方法,相当于/admins/user/=perms[user:method] ,其中method为post,get,delete等。
port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到schemal://serverName:8081?queryString,其中schmal是协议http或https等,serverName是你访问的host,8081是url配置里port的端口,queryString
是你访问的url里的?后面的参数。
authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证
ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https
user:例如/admins/user/**=user没有参数表示必须存在用户,当登入操作时不做检查
注:anon,authcBasic,auchc,user是认证过滤器,
perms,roles,ssl,rest,port是授权过滤器
3.在applicationContext.xml中添加securityManagerper配置
[html] view plain copy
在CODE上查看代码片派生到我的代码片
[html] view plain copy
在CODE上查看代码片派生到我的代码片
4.配置jdbcRealm
[html] view plain copy
在CODE上查看代码片派生到我的代码片
dataSource数据源,配置不说了。
authenticationQuery登录认证用户的查询SQL,需要用登录用户名作为条件,查询密码字段。
userRolesQuery用户角色查询SQL,需要通过登录用户名去查询。查询角色字段
permissionsQuery用户的权限资源查询SQL,需要用单一角色查询角色下的权限资源,如果存在多个角色,则是遍历每个角色,分别查询出权限资源并添加到集合中。
permissionsLookupEnabled默认false。False时不会使用permissionsQuery的SQL去查询权限资源。设置为true才会去执行。
saltStyle密码是否加盐,默认是NO_SALT不加盐。加盐有三种选择CRYPT,COLUMN,EXTERNAL。详细可以去看文档。这里按照不加盐处理。
credentialsMatcher密码匹配规则。下面简单介绍。
[html] view plain copy
在CODE上查看代码片派生到我的代码片
hashAlgorithmName必须的,没有默认值。可以有MD5或者SHA-1,如果对密码安全有更高要求可以用SHA-256或者更高。这里使用MD5
storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码
hashIterations迭代次数,默认值是1。
登录JSP页面
[html] view plain copy
在CODE上查看代码片派生到我的代码片

浙公网安备 33010602011771号