關於Spring Security權限控制


maven坐標

<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
</dependencies>

 

web.xml : Delegating(委托)Filter(過濾器) Proxy(代理) DelegatingFilterProxy委托過濾器代理

作用:攔截用戶的請求,並把請求交給SpringSecurity來處理。

<filter>
<!-- filter-name的值是springSecurityFilterChain,不允許修改-->
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

springsecurity.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">

<!--認證:who are you? 登陸
(1)數據庫用戶表存入的用戶名和密碼
(2)用戶在登陸頁面輸入的用戶名和密碼
(3)判斷
授權 what can you do?
(4)訪問某一個資源需要的角色/權限
(5)當前登陸用戶具有的角色/權限
(6)判斷-->

<!--
auto-config="true"
SpringSecurity就會自動提供一個登陸頁面
(2)用戶在登陸頁面輸入的用戶名和密碼
-->
<security:http use-expressions="true" auto-config="true">
<!--(4)訪問某一個資源需要的角色/權限
hasRole('') :訪問該資源需要什么角色
hasAnyRole('','','',..): 訪問該資源需要指定所有角色中的任何一個
hasAuthority('') 訪問該資源需要什么權限
hasAnyAuthority('','','',...) 訪問該資源需要指定所有權限中的任何一個
isAnonymous(): 游客訪問權限
isAuthenticated(): 要認證通過后才能訪問
hasIpAddress() : 指定具體的IP才能訪問該資源
-->
<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
</security:http>

<!--
authentication:認證
authorization: 授權
-->

<security:authentication-manager>
<security:authentication-provider>
<!--
(1)數據庫用戶表存入的用戶名和密碼
(5)當前登陸用戶具有的角色/權限
-->
<security:user-service>
<security:user name="admin" password="{noop}admin" authorities="ROME_ADMIN"/>
<security:user name="root" password="{noop}root" authorities="ROME_ROOT"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>


<!--
1.項目啟動
2.訪問項目中的資源
3.web.xml(DelegatingFilerProxy)給攔截,攔截以后交給SpringSecurity來處理
4. 判斷:資源l和 <security:intercept-url pattern="/**"/>是否匹配
4.1不匹配:直接放行
4.2匹配: <security:intercept-url access="hasRole('ROLE_ADMIN')"/>
5.獲取當前登陸的用戶是誰?-auto-config="true"為用戶提供一個登陸頁面 [username,password]
6.用戶在登陸頁面輸入用戶名和密碼,
點擊登陸,springSecurity判斷用戶輸入的用戶名和密碼和后台的用戶名密碼是否一致
<security:user-service> [username,password]
7.判斷,兩個用戶名和密碼是否一致
不一致: 返回登陸頁面提示錯誤信息,讓用戶重新輸入
一致: 獲取到當前登陸的用戶,並從<security:user>獲取到當前用戶具有的角色/權限
8.判斷當前用戶具有的角色/權限中是否包含訪問/index.html資源所需要的角色/權限
無:報錯,403 無權限訪問的
有 :放行,繼續訪問。
-->
</beans>

 

<!--白名單-->
<security:http security="none" pattern="要放行的資源路徑"/>

 

 

我的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">

<!--配置哪些資源匿名可以訪問(不登錄也可以訪問)-->
<!--<security:http security="none" pattern="/pages/a.html"></security:http>
<security:http security="none" pattern="/pages/b.html"></security:http>-->
<!--<security:http security="none" pattern="/pages/**"></security:http>-->
<security:http security="none" pattern="/login.html"></security:http>
<security:http security="none" pattern="/css/**"></security:http>
<security:http security="none" pattern="/img/**"></security:http>
<security:http security="none" pattern="/js/**"></security:http>
<security:http security="none" pattern="/plugins/**"></security:http>
<!--
auto-config:自動配置,如果設置為true,表示自動應用一些默認配置,比如框架會提供一個默認的登錄頁面
use-expressions:是否使用spring security提供的表達式來描述權限
-->
<security:http auto-config="true" use-expressions="true">
<security:headers>
<!--設置在頁面可以通過iframe訪問受保護的頁面,默認為不允許訪問-->
<security:frame-options policy="SAMEORIGIN"></security:frame-options>
</security:headers>
<!--配置攔截規則,/** 表示攔截所有請求-->
<!--
pattern:描述攔截規則
asscess:指定所需的訪問角色或者訪問權限
-->
<!--只要認證通過就可以訪問-->
<security:intercept-url pattern="../pages/**" access="isAuthenticated()" />

<!--如果我們要使用自己指定的頁面作為登錄頁面,必須配置登錄表單.頁面提交的登錄表單請求是由框架負責處理-->
<!--
login-page:指定登錄頁面訪問URL
-->
<security:form-login
login-page="/login.html"
username-parameter="username"
password-parameter="password"
login-processing-url="/login.do"
default-target-url="/pages/main.html"
authentication-failure-url="/login.html"></security:form-login>

<!--
csrf:對應CsrfFilter過濾器
disabled:是否啟用CsrfFilter過濾器,如果使用自定義登錄頁面需要關閉此項,否則登錄操作會被禁用(403)
-->
<security:csrf disabled="true"></security:csrf>

<!--
logout:退出登錄
logout-url:退出登錄操作對應的請求路徑
logout-success-url:退出登錄后的跳轉頁面
-->
<security:logout logout-url="/logout.do"
logout-success-url="/login.html" invalidate-session="true"/>

</security:http>

<!--配置認證管理器-->
<security:authentication-manager>
<!--配置認證提供者-->
<security:authentication-provider user-service-ref="springSecurityUserService">
<!--
配置一個具體的用戶,后期需要從數據庫查詢用戶
<security:user-service>
<security:user name="admin" password="{noop}1234" authorities="ROLE_ADMIN"/>
</security:user-service>
-->
<!--指定度密碼進行加密的對象-->
<security:password-encoder ref="passwordEncoder"></security:password-encoder>
</security:authentication-provider>
</security:authentication-manager>

<!--配置密碼加密對象-->
<bean id="passwordEncoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

<!--開啟注解方式權限控制-->
<security:global-method-security pre-post-annotations="enabled" />
</beans>

最后一定要在controller中加入權限校驗的注解
 @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")

項目中的數據之類的調用如此,成功的話我們會接收到403錯誤,這樣的話直接可以在前端頁面接收進行判斷
catch((error)=>{
if(error=="Error: Request failed with status code 403"){
this.$message.error("權限不足");
}else{
this.$message.error("網絡異常,請重試");
}
(我的前端使用的是Vue)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM