Apache Shiro學習----授權

來源:互聯網
上載者:User

標籤:require   user   mis   security   use   asp   tag   class   logo   

Shiro支援三種方式的授權:

1.編程式:通過寫if/else授權碼塊完成: 

Subject subject = SecurityUtils.getSubject();  if(subject.hasRole(“admin”)) {      //有許可權  } else {      //無許可權  } 

2.註解式:通過在執行的Java方法上放置相應的註解完成: 

@RequiresRoles("admin")  public void hello() {      //有許可權  }  

3.JSP/GSP標籤:在JSP/GSP頁面通過相應的標籤完成: 

<shiro:hasRole name="admin">  <!— 有許可權 —>  </shiro:hasRole>  

這裡我們重點講下第三種方式---jsp標籤

1.瞭解一些標籤的定義

 <shiro:guest>歡迎遊客訪問,<a href="${pageContext.request.contextPath}/login.jsp">登入</a>  </shiro:guest>  使用者沒有身分識別驗證時顯示相應資訊,即遊客訪問資訊。

 <shiro:principal type="java.lang.String"  property="username"/>  顯示使用者身份資訊

 <shiro:user>歡迎[<shiro:principal/>]登入,<a href="${pageContext.request.contextPath}/logout">退出</a>  </shiro:user>  使用者已經身分識別驗證/記住我登入後顯示相應的資訊。

<shiro:hasRole name="admin">     使用者擁有角色admin </shiro:hasRole>   

<shiro:hasAnyRoles name="admin,user">    使用者擁有角色admin或user  </shiro:hasAnyRoles>   

<shiro:lacksRole name="abc">    使用者沒有角色abc </shiro:lacksRole>  

<shiro:hasPermission name="user:create">    使用者擁有許可權user:create </shiro:hasPermission> 

<shiro:lacksPermission name="org:create">      使用者沒有許可權org:create  </shiro:lacksPermission>  

2.想使用Shiro標籤首先引用標籤庫<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>

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