權限管理的兩大核心是:認證和授權,前面我們已經介紹完了認證的内容,本文就給大家來介紹下SpringSecurity的授權管理
一、注解操作
我們在控制器或者service中實作授權操作比較理想的方式就是通過相應的注解來實作。SpringSecurity可以通過注解的方式來控制類或者方法的通路權限。注解需要對應的注解支援,若注解放在controller類中,對應注解支援應該放在mvc配置檔案中,因為controller類是有mvc配置檔案掃描并建立的,同理,注解放在service類中,對應注解支援應該放在spring配置檔案中。由于我們現在是模拟業務操作,并沒有service業務代碼,是以就把注解放在controller類中了。
1.1開啟授權的注解支援
這裡給大家示範三類注解,但實際開發中,用一類即可!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd
">
<!-- 配置掃描路徑-->
<context:component-scan base-package="com.dpb.security.controller"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!--
開啟權限控制注解支援
jsr250-annotations="enabled"表示支援jsr250-api的注解,需要jsr250-api的jar包
pre-post-annotations="enabled"表示支援spring表達式注解
secured-annotations="enabled"這才是SpringSecurity提供的注解
-->
<security:global-method-security jsr250-annotations="enabled"
pre-post-annotations="enabled"
secured-annotations="enabled"
/>
</beans>
1.2在注解支援對應類或者方法上添加注解
建立相關的控制器

Jsr250注解的使用
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
效果
Spring表達式注解使用
SpringSecurity提供的注解使用
1.3 權限異常處理
對于沒有通路權限的操作,我們直接給一個403的系統錯誤頁面,使用者體驗也太差了,這時我們可以通過自定義異常處理來解決
自定義錯誤頁面
方式一:在spring-security.xml配置檔案中處理
方式二:編寫通過SpringMVC的異常處理機制
也可以通過我們在介紹SpringMVC時介紹的5中異常處理方式,此處大家自行完善。
方式三:web.xml檔案中設定
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
二、标簽操作
上面介紹的注解方式可以控制伺服器的通路,但是我們在前端頁面上也需要把使用者沒有權限通路的資訊給隐藏起來,這時我們需要通過SpringSecurity的标簽庫來實作,具體如下
<%--
Created by IntelliJ IDEA.
User: dengp
Date: 2019/12/1
Time: 20:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>home界面</h1><br>
目前登入賬号:<br>
<security:authentication property="principal.username"/><br>
<security:authentication property="name"/><br>
<form action="/logout" method="post">
<security:csrfInput/>
<input type="submit"value="登出">
</form>
<security:authorize access="hasAnyRole('ROLE_ADMIN')" >
<a href="#">系統管理</a>
</security:authorize>
<security:authorize access="hasAnyRole('ROLE_USER')" >
<a href="#">使用者管理</a>
</security:authorize>
</body>
</html>
用起來和我們前面介紹shiro的标簽庫差不多
大家要注意,标簽管理僅僅是隐藏了頁面,但并沒有做權限管理,是以背景權限管理是必須的!