許可權模組設計,許可權模組
管理背景許可權管理模組搭建中使用了shiro架構。
shiro的優點是:相對Spring Security較為輕巧,使用起來自由度大,和Spring架構結合的方式也很成熟。缺點是:shiro本身沒實現緩衝,需要自己定義緩衝實現,更新比較慢,有的功能需要自己拓展。
shiro文檔:http://shiro.apache.org/static/1.2.3/apidocs/
十分鐘入門:http://shiro.apache.org/10-minute-tutorial.html
以下總結在項目中使用shiro的方法和管理後台項目中對shiro的拓展。
一、使用shiro系統管理權限
1. 引入shiro需要的包。使用maven的項目中,在pom.xml增加以下依賴:
<!-- shiro許可權管理 --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.1.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-web</artifactId><version>1.1.0</version></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.1.0</version></dependency><!-- shiro許可權管理 end -->
2. 在項目中增加shiro配置。
在spring設定檔目錄下建立spring-shiro.xml。內容如下:
<!-- Shiro許可權管理配置 --><bean id="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- shiro通過一個filter控制許可權--> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login/execute.do" /> <!-- 登陸頁 --> <property name="successUrl" value="/index.jsp" /> <!-- 登陸成功之後跳轉的頁面 --> <property name="unauthorizedUrl" value="/login/execute.do" /> <!-- 使用者在請求無許可權的資源時,跳轉到這個url --> <property name="filters"> <util:map> <entry key="authc"> <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /> </entry> </util:map> </property> <property name="filterChainDefinitions"> <!-- 配置訪問url資源需要使用者擁有什麼許可權 配置的優先順序由上至下--> <value>/=anon/template/main.jsp=user<!-- api使用者資訊 --> /api/createApiUser**=perms[api:user:create] <!-- 許可權可以用":"分級,如擁有api許可權相當於擁有api*許可權(父許可權涵蓋子許可權) -->/api/updateApiUser**=perms[api:user:update] /api/*User*=perms[api:user:view] /template/apiUserManage/**=perms[api:user:view] <!-- api介面管理 --> /api/*Interface*=perms[api:user:interface] <!-- api統計資料 --> /api/querySummaryData**=perms[api:data] /template/apiSumData/**=perms[api:data] /api/**=perms[api:*] ... /**=anon </value> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realm" ref="permissionsRealm" /> <!-- 自訂登陸及擷取許可權的源 --> </bean> <!-- shiro許可權管理配置 end -->
在web.xml中增加shiro filter的配置:
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml,classpath*:spring-*.xml //此處引入了spring-shiro</param-value> </context-param> ... <filter> <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3. 定義登陸及擷取許可權的源。
/** * 認證實作類別 * * @author kexm * */@Service("permissionsRealm")public class PermissionsRealm extends AuthorizingRealm { @Autowired private AccountDao accountDao; @Autowired private GroupDao groupDao; private Account acc; private static LogUtil log = LogUtil.getLogger(PermissionsRealm.class); /** * 使用者權限源(shiro調用此方法擷取使用者權限,至於從何處擷取許可權項,由我們定義。) */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); log.info("method[doGetAuthorizationInfo] begin."); if (acc != null) { if(acc.getAdminType() == 2){//超級管理員 始終擁有所有許可權 info.addStringPermission("*"); return info; } try { List<UserGroup> gList = accountDao.getUserGroups(acc.getLoginName()); for (UserGroup g: gList) { //擷取使用者的組 log.info("method[doGetAuthorizationInfo] group<" + g.getName() + ">"); List<Permission> pList = groupDao.getGroupPerms(g.getId()); for (Permission p: pList) { //擷取組內許可權 log.info("method[doGetAuthorizationInfo] perm<" + p.getName() + "," + p.getPermList() + ">"); String permList = p.getPermList(); if (permList != null && !"".equals(permList)) { String[] perms = p.getPermList().split(","); for (String perm: perms) {//分別放入容器 (許可權以字串形式呈現,如"api:data"等,和spring-shiro.xml中的配置相對應) log.info("method[doGetAuthorizationInfo] add perm<" + perm + ">"); info.addStringPermission(perm); } } } } return info;//將使用者權限返回給shiro } catch (Exception e) { log.error("method[doGetAuthorizationInfo] e.message<" + e.getMessage() + "> e<" + e + ">", e); } } return null; } /** * 使用者登入驗證源(shiro調用此方法執行認證) */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authtoken) throws AuthenticationException { log.info("method[doGetAuthenticationInfo] begin."); UsernamePasswordToken token = (UsernamePasswordToken) authtoken; SimpleAuthenticationInfo authenticationInfo = null; String userName = token.getUsername(); String password = new String(token.getPassword()); Login conf = DefaultConfigure.config.getLogin(); String MD5pwd = MD5Util.generateSignature(conf.getSalt(), password); try { if (userName != null && !"".equals(userName)) { acc = accountDao.login(userName, MD5pwd); } if (acc != null) { doGetAuthorizationInfo(SecurityUtils.getSubject().getPrincipals()); authenticationInfo = new SimpleAuthenticationInfo(token.getUsername(), token.getPassword(), getName()); return authenticationInfo; } } catch (Exception e) { log.error("method[doGetAuthenticationInfo] acc<" + acc + "> message<" + e.getMessage() + "> e<" + e + ">", e); } return null; }}
3. shiro中,使用subject系統管理使用者。可以把subject理解為shiro儲存使用者資訊的容器和操縱使用者的工具。有了前幾步的配置,便可以使用以下代碼登入登出,並享受shiro的url許可權控制了。
//登入 UsernamePasswordToken token = new UsernamePasswordToken(loginName, password); Subject user = SecurityUtils.getSubject(); user.login(token); //使用shiro內建的session儲存使用者資訊 獨立於httpSession Session ss = user.getSession().setAttribute("userInfo", acc); //登出 SecurityUtils.getSubject().logout();
4. 在頁面中使用shiro標籤。假如我們要讓有許可權的使用者看到某些菜單或按鈕,可以用以下方式。
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %><shiro:hasPermission name="api:data"> who has permission can see</shiro:hasPermission>
以上只使用了shiro的permission管理,shiro還支援對role的管理,如有進一步抽象的需求可以使用。
二、分布式環境,讓shiro與redis結合
假如我們的web項目是分布式部署的,則需要讓shiro把session和使用者權限到放到集中緩衝上去。Shiro本身不實現Cache,但是提供了介面,方便更換不同的底層Cache實現。
shiro提供的cache介面:
public interface Cache<K, V> { //根據Key擷取緩衝中的值 public V get(K key) throws CacheException; //往緩衝中放入key-value,返回緩衝中之前的值 public V put(K key, V value) throws CacheException; //移除緩衝中key對應的值,返回該值 public V remove(K key) throws CacheException; //清空整個緩衝 public void clear() throws CacheException; //返回緩衝大小 public int size(); //擷取緩衝中所有的key public Set<K> keys(); //擷取緩衝中所有的value public Collection<V> values(); }
觀察介面可以發現,我們需要實現一個keys方法。這個方法限制了shiro不能使用緩衝叢集(SharedRedis不提供這個方法,只有單台redis可用keys方法,望找到解決方案)。
我們的項目使用redis作為集中緩衝,shiro和redis結合的方式可以使用一個現成的工具——shiro-redis。
shiro-redis的github:https://github.com/alexxiyang/shiro-redis
目前管理後台項目正在使用此工具。這個工具有一處問題:讀取緩衝時沒對讀取的對象延長有效期間,修複這個BUG之後還挺好用。
三、對shiro頁面標籤拓展,增加and or not 邏輯符
參考:http://jinnianshilongnian.iteye.com/blog/1864800
使用過shiro的朋友應該都知道在要想實現any permission的驗證是比較麻煩。
很多朋友剛開始接觸時以為如<shiro:hasPermission name="showcase:tree:*"> 代表驗證使用者是否擁有tree下的任何許可權,但這是錯誤的。如果我們把showcase:tree:*授權給使用者,那麼此時表示使用者具有showcase:tree資源的任意許可權,如<shiro:hasPermission name="showcase:tree:*">或shiro:hasPermission name="showcase:tree:create">都能驗證成功。
還有朋友認為<shiro:hasPermission name="showcase:tree:create,update"> 是或的關係,也不是,預設是且的關係。
下載了最新的shiro1.3.0-SNAPSHOT 發現並沒有增加新的標籤或其他支援。
因此我們需要簡單的擴充下shiro來支援像spring security 3那樣的@Secured支援運算式的強大註解。我們擴充AuthorizingRealm,並修改:
private static final String OR_OPERATOR = " or "; private static final String AND_OPERATOR = " and "; private static final String NOT_OPERATOR = "not "; /** * 支援or and not 關鍵詞 不支援and or混用 * @param principals * @param permission * @return */ public boolean isPermitted(PrincipalCollection principals, String permission) { if(permission.contains(OR_OPERATOR)) { String[] permissions = permission.split(OR_OPERATOR); for(String orPermission : permissions) { if(isPermittedWithNotOperator(principals, orPermission)) { return true; } } return false; } else if(permission.contains(AND_OPERATOR)) { String[] permissions = permission.split(AND_OPERATOR); for(String orPermission : permissions) { if(!isPermittedWithNotOperator(principals, orPermission)) { return false; } } return true; } else { return isPermittedWithNotOperator(principals, permission); } } private boolean isPermittedWithNotOperator(PrincipalCollection principals, String permission) { if(permission.startsWith(NOT_OPERATOR)) { return !super.isPermitted(principals, permission.substring(NOT_OPERATOR.length())); } else { return super.isPermitted(principals, permission); } }
如上代碼即可以實現簡單的NOT、AND、OR支援,不過缺點是不支援複雜的如AND、OR組合。
如下標籤在拓展後可以生效:
<shiro:hasPermission name="api:data or api:user"></shiro:hasPermission><pre name="code" class="html"><shiro:hasPermission name="api:data and api:user"></shiro:hasPermission>
<shiro:hasPermission name="not api:data"></shiro:hasPermission>