Apache Shiro权限管理框架

Apache Shiro 和Web项目集成html

web.xml配置

<!-- Apache Shiro Filter-->
	<filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

Spring.xml配置

 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
   <property name="securityManager" ref="securityManager"/> 
   <!-- cas单点登陆地址 http://casserve/?service=http://caseclien-->
   <property name="loginUrl" value="/login"/>
   <property name="successUrl" value="/index" />   <!-- 登陆成功后跳转地址 -->
   <property name="unauthorizedUrl" value="/403"/> <!-- 未认证回调地址 -->
   <property name="filters"> 
      <util:map> 
         <entry key="authc" value-ref="formAuthenticationFilter"/> 
      </util:map> 
   </property> 
   <property name="filterChainDefinitions"> 
      <value> 
         /static/** = anon
         /userfiles/** = anon
         ${adminPath}/login = authc
         ${adminPath}/logout = logout
         ${adminPath}/** = roles[admin]
         /act/rest/service/editor/** = perms[read]
         /act/rest/service/model/** = perms[write]
         /act/rest/service/** = user
         /ReportServer/** = user
      </value> 
   </property> 
 </bean> 

 <bean id="securityManager" 
   class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
   <property name="realm" ref="myShiroRealm"/> 
 </bean> 

  <bean id="myShiroRealm" class="xxx.packagename.MyShiroRealm"> 
    <!-- businessManager 用来实现用户名密码的查询 
    <property name="businessManager" ref="businessManager"/> 
    --> 
   <property name="cacheManager" ref="shiroCacheManager"/> 
  </bean> 

   <bean id="shiroCacheManager" 
          class="org.apache.shiro.cache.ehcache.EhCacheManager"> 
        <property name="cacheManager" ref="cacheManager"/> 
   </bean> 

   <!-- 表单登陆 -->
   <bean id="formAuthenticationFilter"  
      class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"/>


   <!-- 用户受权信息Cache -->  
   <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />  
  
   <!-- 保证明现了Shiro内部lifecycle函数的bean执行 -->  
   <bean id="lifecycleBeanPostProcessor" 
      class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  
    <!-- AOP式方法级权限检查 -->  
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
        depends-on="lifecycleBeanPostProcessor">  
       <property name="proxyTargetClass" value="true" />  
    </bean>  
  
   <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 
        <property name="securityManager" ref="securityManager" />  
    </bean>

  备注:java

           loginUrl ,loginUrl ,unauthorizedUrl三个属相的配置,在使用http://ip:port/工程名/index用须要配置为  /工程名/indexweb

            若是没有工程名,则直接配置为  /indexspring

代码说明:数据库

  1. shiroFilter 中 loginUrl 为登陆页面地址,successUrl为登陆成功页面地址(若是首先访问受保护 URL 登陆成功,则跳转到实际访问页面),unauthorizedUrl 认证未经过访问的页面(前面提到的“未经受权页面”)。apache

  2. shiroFilter 中 filters 属性,formAuthenticationFilter 配置为基于表单认证的过滤器。api

  3. shiroFilter 中 filterChainDefinitions 属性,anon 表示匿名访问(不须要认证与受权),authc 表示须要认证,perms[SECURITY_ACCOUNT_VIEW] 表示用户须要提供值为“SECURITY_ACCOUNT_VIEW”Permission 信息。因而可知,链接地址配置为 authc 或 perms[XXX] 表示为受保护资源。spring-mvc

  4. securityManager 中 realm 属性,配置为咱们本身实现的 Realm。关于 Realm,参见前面“Shiro Realm”章节。缓存

  5. myShiroRealm 为咱们本身须要实现的 Realm 类,为了减少数据库压力,添加了缓存机制。session

  6. shiroCacheManager 是 Shiro 对缓存框架 EhCache 的配置。

  7. AuthorizationAttributeSourceAdvisor配置,可使用注解标注方法须要使用的权限

    须要将spring-shiro文件包含到spring-mvc文件中!!!!!

     shiro中默认的过滤器

    AuthorizingRealm自定义实现

public class MyShiroRealm extends AuthorizingRealm{ 
   
   // 用于获取用户信息及用户权限信息的业务接口
   @Resource
   private BusinessManager businessManager; 
    
    /**
     *获取受权信息,用户登录成功后,添加权限
     *
     */
   protected AuthorizationInfo doGetAuthorizationInfo( 
      PrincipalCollection principals) { 

      // 获取用户名
      String username = (String) principals.fromRealm(getName()).iterator().next(); 
      
      if( username != null ){ 
         SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); 
 
         // 查询用户受权信息
         Collection<String> pers=businessManager.queryPermissions(username); 
         if( pers != null && !pers.isEmpty() ){ 
            info.addStringPermissions(pers);
         } 
  
          //查询用户角色
         Collection<String> roles=businessManager.queryRoles(username); 
         if( roles!= null && !roles.isEmpty()){ 
            info.addRoles(roles);
         } 
       
         return info; 
      } 
      return null; 
   } 
   
   // 获取认证信息,登录使用
   protected AuthenticationInfo doGetAuthenticationInfo( 
      AuthenticationToken authcToken ) throws AuthenticationException { 
      UsernamePasswordToken token = (UsernamePasswordToken) authcToken; 
      // 经过表单接收的用户名
      String username = token.getUsername(); 
      
      if( username != null && !"".equals(username) ){ 
         LoginAccount account = businessManager.get( username ); 
         
         if( account != null ){ 
            return new SimpleAuthenticationInfo( 
               account.getLoginName(),account.getPassword(),getName() ); 
         } 
      } 
      
      return null; 
   } 

     /** 
     * 清除全部用户受权信息缓存. 
     */  
    public void clearCachedAuthorizationInfo(String principal) {  
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());  
        clearCachedAuthorizationInfo(principals);  
    }  
  
  
    /** 
     * 清除全部用户受权信息缓存. 
     */  
    public void clearAllCachedAuthorizationInfo() {  
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();  
        if (cache != null) {  
            for (Object key : cache.keys()) {  
                cache.remove(key);  
            }  
        }  
    }  
 }

 

 用户权限模型

在揭开 Shiro 面纱以前,咱们须要认知用户权限模型。本文所提到用户权限模型,指的是用来表达用户信息及用户权限信息的数据模型。即能证实“你是谁?”、“你能访问多少受保护资源?”。为实现一个较为灵活的用户权限数据模型,一般把用户信息单独用一个实体表示,用户权限信息用两个实体表示。

  1. 用户信息用 LoginAccount 表示,最简单的用户信息可能只包含用户名 loginName 及密码 password 两个属性。实际应用中可能会包含用户是否被禁用,用户信息是否过时等信息。

  2. 用户权限信息用 Role 与 Permission 表示,Role 与 Permission 之间构成多对多关系。Permission 能够理解为对一个资源的操做,Role 能够简单理解为 Permission 的集合。

  3. 用户信息与 Role 之间构成多对多关系。表示同一个用户能够拥有多个 Role,一个 Role 能够被多个用户所拥有。

图 1. 用户权限模型

图 1. 用户权限模型

Apache权限数据模型

 

t_user用户表:

id ------username-------password

1           tom                00000

2           jack               00000

3           rose               00000

 

t_role

id    +    rolename

1    --      admin

2    --      manager

3    --      normal

 

t_permission权限表

id   ---   permissionname  --  role_id

1     --      add           ----          2

2     --      del             ----         1

3    --       update        ----       2

4     --      query         ----        3

 

t_user_role

tom是admin和normal角色

user_id    +    role_id

 1                      1

 1                      3

 2                      2

 2                      3

 3                      3

 

登陆接口调用

调用shrio方法    

/** 
     * 验证用户名和密码 
     * @param request 
     * @return 
     */  
    @RequestMapping("/login")  
    public String login(HttpServletRequest request) {  
        String result = "/login";  
        // 取得用户名  
        String username = request.getParameter("username");  
        //取得 密码,并用MD5加密  
        String password = CipherUtil.generatePassword(request.getParameter("password"));  
       //String password = request.getParameter("password");  
        UsernamePasswordToken token = new UsernamePasswordToken(username, password); 

        //设置session
        //SecurityUtils.getSubject().getSession().setAttribute("deployEnv", deployEnv);    

        //若是用户已登陆,先踢出
	    //ShiroSecurityHelper.kickOutUser(username));

        Subject currentUser = SecurityUtils.getSubject();  
        try {  
           
            if (!currentUser.isAuthenticated()){//使用shiro来验证  
                token.setRememberMe(true);//记住用户的登陆状态
                currentUser.login(token);//验证角色和权限,登陆成功会直接跳转到 successUrl地址 
            }  
            System.out.println("result: " + result);  
            result = "index";//验证成功  
         } catch(AuthorizationException ae){
            result = "/login";//验证失败  
         } catch (Exception e) {  
            logger.error(e.getMessage());  
            result = "/login";//验证失败  
        }  
        return result;  
    }

   
    /** 
     * 退出 
     * @return 
     */  
    @RequestMapping(value = "/logout")    
    @ResponseBody    
    public String logout() {
        Subject currentUser = SecurityUtils.getSubject();    
        String result = "logout";    
        currentUser.logout();    
        return result;    
    }

    

Shiro基本html tag标签

jsp页面

<%@ tagliburi="http://shiro.apache.org/tags" prefix="shiro" %>

标签名称

标签条件(均是显示标签内容)

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

显示用户身份名称

 <shiro:principal property="username"/>     显示用户身份中的属性值

<shiro:hasRole name="admin">
   权限显示内容
</shiro:hasRole>

 

Shiro基本注解

@RequiresAuthentication

验证用户是否登陆,等同于方法subject.isAuthenticated() 结果为true时。

@RequiresUser

验证用户是否被记忆,user有两种含义:

一种是成功登陆的(subject.isAuthenticated() 结果为true);

另一种是被记忆的(subject.isRemembered()结果为true)。

@RequiresGuest

验证是不是一个guest的请求,与@RequiresUser彻底相反。

 换言之,RequiresUser  == !RequiresGuest。

此时subject.getPrincipal() 结果为null.

@RequiresRoles

例如:@RequiresRoles("aRoleName");

  void someMethod();

若是subject中有aRoleName角色才能够访问方法someMethod。若是没有这个权限则会抛出异常AuthorizationException

@RequiresPermissions

例如: @RequiresPermissions({"read", "write"} )
  void someMethod();

要求subject中必须同时含有read和write的权限才能执行方法someMethod()。不然抛出异常AuthorizationException

 

结尾

相关文章

http://www.ibm.com/developerworks/cn/java/j-lo-shiro/

相关文章
相关标签/搜索