注:这里只介绍Spring配置模式。

因为官方例子虽然中有更加简洁的ini配置形式,但是使用ini配置无法与spring整合。而且两种配置方法一样,只是格式不一样。


涉及的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中添加依赖包

  1. <dependency>  
  2.     <groupId>org.apache.shiro</groupId>  
  3.     <artifactId>shiro-core</artifactId>  
  4.     <version>1.2.0</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.apache.shiro</groupId>  
  8.     <artifactId>shiro-web</artifactId>  
  9.     <version>1.2.0</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>org.apache.shiro</groupId>  
  13.     <artifactId>shiro-ehcache</artifactId>  
  14.     <version>1.2.0</version>  
  15. </dependency>  
  16. <dependency>  
  17.     <groupId>org.apache.shiro</groupId>  
  18.     <artifactId>shiro-spring</artifactId>  
  19.     <version>1.2.0</version>  
  20. </dependency>  
  21. <dependency>  
  22.     <groupId>net.sf.ehcache</groupId>  
  23.     <artifactId>ehcache-core</artifactId>  
  24.     <version>2.5.3</version>  
  25. </dependency>  
  26. <dependency>  
  27.     <groupId>org.slf4j</groupId>  
  28.     <artifactId>slf4j-jdk14</artifactId>  
  29.     <version>1.6.4</version>  
  30. </dependency>  

Spring整合配置

1.在web.xml中配置shiro的过滤器

  1. <!-- Shiro filter-->    
  2. <filter>   
  3.     <filter-name>shiroFilter</filter-name>   
  4.     <filter-class>   
  5.         org.springframework.web.filter.DelegatingFilterProxy    
  6.     </filter-class>    
  7. </filter>    
  8. <filter-mapping>    
  9.     <filter-name>shiroFilter</filter-name>    
  10.     <url-pattern>/*</url-pattern>    
  11. </filter-mapping>    

2.在Spring的applicationContext.xml中添加shiro配置 

  1. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  2.         <property name="securityManager" ref="securityManager" />  
  3.         <property name="loginUrl" value="/login" />  
  4.         <property name="successUrl" value="/login/loginSuccessFull" />  
  5.         <property name="unauthorizedUrl" value="/login/unauthorized" />  
  6.         <property name="filterChainDefinitions">  
  7.             <value>  
  8.                 /home* = anon  
  9.                 / = anon  
  10.                 /logout = logout  
  11.                 /role/** = roles[admin]  
  12.                 /permission/** = perms[permssion:look]  
  13.                 /** = authc  
  14.             </value>  
  15.         </property>  
  16.     </bean>  

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配置

  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  2. <!-- 单realm应用。如果有多个realm,使用‘realms’属性代替 -->  
  3. <property name="realm" ref="sampleRealm" />  
  4. <property name="cacheManager" ref="cacheManager" />  
  5. </bean>  

<shiro:authenticated>

登录之后

<shiro:notAuthenticated>

不在登录状态时

<shiro:guest>

用户在没有RememberMe时

<shiro:user>

用户在RememberMe时

<shiro:hasAnyRoles name="abc,123" >

在有abc或者123角色时

<shiro:hasRole name="abc">

拥有角色abc

<shiro:lacksRole name="abc">

没有角色abc

<shiro:hasPermission name="abc">

拥有权限资源abc

<shiro:lacksPermission name="abc">

没有abc权限资源

<shiro:principal>

默认显示用户名称

7.

默认,添加或删除用户的角色 或资源 ,系统不需要重启,但是需要用户重新登录。

即用户的授权是首次登录后第一次访问需要权限页面时进行加载。

但是需要进行控制的权限资源,是在启动时就进行加载,如果要新增一个权限资源需要重启系统。

8.

Springsecurity 与apache shiro差别:

a)shiro配置更加容易理解,容易上手;security配置相对比较难懂。
b)在spring的环境下,security整合性更好。Shiro对很多其他的框架兼容性更好,号称是无缝集成。
c)shiro不仅仅可以使用在web中,它可以工作在任何应用环境中。
d)在集群会话时Shiro最重要的一个好处或许就是它的会话是独立于容器的。
e)Shiro提供的密码加密使用起来非常方便。

9.

控制精度:

注解方式控制权限只能是在方法上控制,无法控制类级别访问。

过滤器方式控制是根据访问的URL进行控制。允许使用*匹配URL,所以可以进行粗粒度,也可以进行细粒度控制。


来源:http://blog.csdn.net/clj198606061111/article/details/24185023





相关文章: