權限控制

1. 頁面標籤:<security:authorize/> 能夠嵌套
2. 註解方式, 只能用其中一種:
* JSR250
引入依賴
開啓jsr250註解 security
@RoleAllowed
* Secured註解, security自帶的
開啓Secured註解註解 security
@Secured
* Spring的註解
開啓Spring註解 security
@PreAuthorizejava

 
1、
一、這裏須要先開啓SpELl表達式,不然不能使用SpringSecurity頁面對應的標籤

二、

<security:http auto-config="true" use-expressions="true">
<!-- 配置攔截的請求地址,任何請求地址都必須有ROLE_USER的權限 -->
<security:intercept-url pattern="/index.jsp" access="ROLE_USER" />
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />

//略....
</security:http>spring

 

三、express

<security:authorize access="hasAnyRole('ROLE_ADMIN','ROLE_USER')">
<li class="treeview"><a href="#"> <i class="fa fa-cogs"></i>
<span>系統管理</span> <span class="pull-right-container"> <i
class="fa fa-angle-left pull-right"></i>
</span>


</a>
<ul class="treeview-menu">

<li id="system-setting"><a
href="${pageContext.request.contextPath}/user/list"> <i
class="fa fa-circle-o"></i> 用戶管理
</a></li>
此標籤表示下面的內容只有管理員權限才能夠訪問

<security:authorize access="hasAnyRole('ROLE_ADMIN')">
<li id="system-setting"><a
href="${pageContext.request.contextPath}/role/list"> <i
class="fa fa-circle-o"></i> 角色管理
</a></li>
<li id="system-setting"><a
href="${pageContext.request.contextPath}/permission/list">
<i class="fa fa-circle-o"></i> 權限管理
</a></li>
<li id="system-setting"><a
href="${pageContext.request.contextPath}/pages/syslog-list.jsp"> <i
class="fa fa-circle-o"></i> 訪問日誌
</a></li>
</security:authorize>
</ul></li>
</security:authorize>

四、經過上面的控制,咱們發現一個問題,用戶只是頁面看不到菜單按鈕,但仍然能訪問原來地址,咱們須要作後臺權限控制。這裏提供了多種註解方式實現,咱們着重講講其中3種註解實現方式。
但不管哪一種方式,都須要首先開啓AOP支持,而且不能多種註解同時使用。修改SpringMVC配置文件以下:

<!--開啓AOP-->
<aop:aspectj-autoproxy proxy-target-class="true" />api

 

@RolesAllowed

 

JSR 250做爲一個Java規範請求,它的目標是定義一組註解,這些註釋解決常見的語義概念,所以能夠被許多JavaEE和JavaSE組件使用。這是爲了不這些組件之間的冗餘註釋。隨着聲明式註釋驅動的配置愈來愈多地應用於Java框架和應用程序中,例如:spring經過註釋使其框架的更多組件可配置,JSR 250的重要性在未來可能會增長。框架

 

使用該註解須要首先引入依賴:jsp

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>post

在spring-security.xml配置文件中開啓JSR-250的註解支持url

<security:global-method-security jsr250-annotations="enabled"/>spa

 

@RolesAllowed("ROLE_ADMIN")
public class RoleController {}日誌

 

security註解方式權限攔截:@Secured

 

在spring-security.xml配置文件中開啓註解支持

<security:global-method-security secured-annotations="enabled"/>

在Controller類或者方法添加註解

@Secured("ROLE_ADMIN")

 

Spring表達式的方式

在spring-security.xml配置文件中開啓註解支持

<security:global-method-security pre-post-annotations="enabled"/>

在Controller類或者方法添加註解

@PreAuthorize("hasAuthority('ROLE_ADMIN')")

 

頁面403處理

在用戶沒有權限訪問時,常常會出現403頁面,不是很友好。SpringSecurity對這個也有控制,只須要在spring-security.xml中加上以下配置便可。

 

<security:access-denied-handler error-page="/403.jsp"/>

相關文章
相關標籤/搜索