細說shiro之在web應用中使用shiro

來源:互聯網
上載者:User

標籤:https   tac   ack   業務需求   許可權管理   not   nbsp   程式啟動   enabled   

官網:https://shiro.apache.org/

 

1. 下載
在Maven項目中的依賴配置如下:

<!-- shiro配置 --><dependency>  <groupId>org.apache.shiro</groupId>   <artifactId>shiro-core</artifactId>   <version>1.3.2</version>
</dependency><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.2</version></dependency><!-- 配置日誌組件 --><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jcl</artifactId> <version>1.7.25</version>
</dependency><dependency>  <groupId>commons-logging</groupId>  <artifactId>commons-logging</artifactId>
  <version>1.2</version></dependency>

特別地!Shiro使用了日誌架構slf4j,因此需要對應配置指定的日誌實現組件,如:log4j,logback等。
而且,由於shiro-web組件使用apache commons logging組件中的工具類,所以在項目中必須添加commongs logging組件。
否則,程式啟動時將會報錯:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.beanutils.ConvertUtilsBean.<init>(ConvertUtilsBean.java:157)
at org.apache.commons.beanutils.BeanUtilsBean.<init>(BeanUtilsBean.java:117)
at org.apache.commons.beanutils.BeanUtilsBean$1.initialValue(BeanUtilsBean.java:68)
at org.apache.commons.beanutils.ContextClassLoaderLocal.get(ContextClassLoaderLocal.java:153)
at org.apache.commons.beanutils.BeanUtilsBean.getInstance(BeanUtilsBean.java:80)
at org.apache.commons.beanutils.PropertyUtilsBean.getInstance(PropertyUtilsBean.java:114)
at org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptor(PropertyUtils.java:460)
at org.apache.shiro.config.ReflectionBuilder.isTypedProperty(ReflectionBuilder.java:409)
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:702)
at org.apache.shiro.config.ReflectionBuilder.applySingleProperty(ReflectionBuilder.java:364)
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:325)
at org.apache.shiro.config.ReflectionBuilder$AssignmentStatement.doExecute(ReflectionBuilder.java:955)
at org.apache.shiro.config.ReflectionBuilder$Statement.execute(ReflectionBuilder.java:887)
at org.apache.shiro.config.ReflectionBuilder$BeanConfigurationProcessor.execute(ReflectionBuilder.java:765)
at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:260)
at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:167)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:130)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:108)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:94)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:46)
at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
at org.apache.shiro.web.env.IniWebEnvironment.createWebSecurityManager(IniWebEnvironment.java:203)
....

 

2.整合Shiro
在Java Web應用中使用Shiro,需要特別的整合方式,不再像在非Web環境的獨立應用中使用Shiro那麼簡單(只需要下載Shiro並添加到項目即可)。
通常,在Java Web應用中整合架構都是從配置web.xml開始的,整合Shiro也不例外。
web.xml:

<listener>    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class></listener><filter>    <filter-name>ShiroFilter</filter-name>    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class></filter><filter-mapping>    <filter-name>ShiroFilter</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher>    <dispatcher>INCLUDE</dispatcher>    <dispatcher>ERROR</dispatcher></filter-mapping>

通常,在Java Web應用中整合第三方架構,都是從Filter開始。Shiro也是如此,即需要將所有請求都經過Shiro指定的Filter進行攔截,這樣才能完成使用者對指定資源訪問的授權驗證。
特別地,從Shiro 1.2+版本之後,在Java Web應用中整合Shiro非常簡單,甚至都不需要明確指定shiro設定檔的路徑,而是直接在web.xml中添加org.apache.shiro.web.env.EnvironmentLoaderListener即可(只需要保證在${webapp}/WEB-INF/目錄下存在檔案shiro.ini)。

 

3. 資料來源配置
在Shiro中,Realm定義了訪問資料的方式,用來串連不同的資料來源,如:LDAP,關聯式資料庫,設定檔等等。
Realm類圖:

也就是說,可以根據實際需求及應用的許可權管理複雜度靈活選擇指定資料來源。
在此,以org.apache.shiro.realm.text.IniRealm為例,具體配置如下:
shiro.ini:

[main]# 自訂過濾器
sessionFilter
= org.chench.test.shiroweb.filter.SessionFilterauthc.loginUrl = /indexssl.enabled = false# -----------------------------------------------------------------------------# Users and their (optional) assigned roles# username = password, role1, role2, ..., roleN# -----------------------------------------------------------------------------[users]root = secret, adminguest = guest, guestpresidentskroob = 12345, presidentdarkhelmet = ludicrousspeed, darklord, schwartzlonestarr = vespa, goodguy, schwartz# -----------------------------------------------------------------------------# Roles with assigned permissions# roleName = perm1, perm2, ..., permN# -----------------------------------------------------------------------------[roles]admin = *schwartz = lightsaber:*goodguy = winnebago:drive:eagle5# -----------------------------------------------------------------------------# The format of each line in the urls section is as follows:# _URL_Ant_Path_Expression_ = _Path_Specific_Filter_Chain_# -----------------------------------------------------------------------------[urls]/index = anon, sessionFilter/user/signin = anon/user/login = anon/user/** = authc/home/** = authc#/admin/** = authc, roles[administrator]#/rest/** = authc, rest#/remoting/rpc/** = authc, perms["remote:invoke"]

 

4. 認證
在Shiro中,認證即執行使用者登入,讀取指定Realm串連的資料來源,以驗證使用者身份的有效性與合法性。
關於Shiro在Web應用中的認證流程,與Shiro在非Web環境的獨立應用中的認證流程一樣,都需要執行使用者登入,即:

Subject currentUser = SecurityUtils.getSubject();if(!currentUser.isAuthenticated()) {  UsernamePasswordToken token = new UsernamePasswordToken(name, password);  try {    currentUser.login(token);
  }
catch (UnknownAccountException e) { exception = e; logger.error(String.format("user not found: %s", name), e); } catch(IncorrectCredentialsException e) { exception = e; logger.error(String.format("user: %s pwd: %s error", name, password), e); } catch (ConcurrentAccessException e) { exception = e; logger.error(String.format("user has been authenticated: %s", name), e); } catch (AuthenticationException e) { exception = e; logger.error(String.format("account except: %s", name), e); }}

唯一的區別就是,在Java Web環境中,使用者名稱和密碼參數是通過前端頁面進行傳遞。

 

5. 授權
需要再三強調!!!Shiro作為許可權架構,僅僅只能控制對資源的操作許可權,並不能完成對資料許可權的業務需求。
而對於Java Web環境下Shiro授權,包含個方面的含義。
其一,對於前端來說,使用者只能看到他對應存取權限的元素。
其二,當使用者執行指定操作(即:訪問某個uri資源)時,需要驗證使用者是否具備對應許可權。

對於第一點,在Java Web環境下,通過Shiro提供的JSP標籤實現。

<shiro:hasRole name="admin">    <a>使用者管理</a></shiro:hasRole><shiro:hasPermission name="winnebago:drive:eagle5">    <a>Action Trail</a></shiro:hasPermission>

必須在jsp頁面中引入shiro標籤庫:

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

 

對於第二點,與在非Java Web環境下一樣,需要在後端調用API進行許可權(或者角色)檢驗。如果使用Spring架構,還直接通過Java註解方式實現。
api調用:

String roleAdmin = "admin";Subject currentUser = SecurityUtils.getSubject();if(!currentUser.hasRole(roleAdmin)) {    //todo something}

java註解:

@Controllerpublic class HomeController {    @RequestMapping("/home")    @RequiresPermissions(value={"log:manage:*"})    public ModelAndView home(HttpServletRequest req) {        ModelAndView mv = new ModelAndView("home");        return mv;    }}

 

6. 完整樣本
詳見://TODO

細說shiro之在web應用中使用shiro

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.