Spring Security 安全框架

来源:https://www.cnblogs.com/superbc/archive/2018/12/13/10117260.html
-Advertisement-
Play Games

一 簡介:Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency ...


一 簡介:Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoCDI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴註入)和AOP(面向切麵編程)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重覆代碼的工作.

 

 Spring Security入門小Demo

1.創建工程spring-security-demo 引入pom依賴

<--spring相關依賴  略-->
<--security相關依賴-->

<dependency>

  <groupId>org.springframework.security</groupId>

  <artifactId>spring-security-web</artifactId>

  <version>4.1.0.RELEASE</version>

</dependency>

 <dependency>

  <groupId>org.springframework.security</groupId>

  <artifactId>spring-security-config</artifactId>

  <version>4.1.0.RELEASE</version>

</dependency>

2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

    <context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-security.xml</param-value>

 </context-param>

 <listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

 </listener>

 <filter>  

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

</web-app>

3.創建index.xml

4.配置spring-security.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/security"

xmlns:beans="http://www.springframework.org/schema/beans" 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.xsd

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

 

<!-- 頁面攔截規則 -->

<http use-expressions="false">

<intercept-url pattern="/**" access="ROLE_USER" />

<form-login/>

</http>

 

<!-- 認證管理器 -->

<authentication-manager>

<authentication-provider>

<user-service>

<user name="admin" password="123456" authorities="ROLE_USER"/>

</user-service>

</authentication-provider>

</authentication-manager>

</beans:beans>

use-expressions 為是否使用使用 Spring 表達式語言( SpEL ,預設為true ,如果開啟,則

 

攔截的配置應該寫成以下形式

 

<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

 

 

 

這樣就可以實現攔截了  不過預設有登陸界面   也可以自己定義登陸界面

1.首先自定義一個登陸界面(一個成功界面,一個失敗界面)

2.修改spring-security.xml

<!-- 以下頁面不被攔截 -->

<http pattern="/login.html" security="none"></http>

<http pattern="/login_error.html" security="none"></http>

<!-- 頁面攔截規則 -->

<http use-expressions="false">

<intercept-url pattern="/*" access="ROLE_USER" />

<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>

<csrf disabled="true"/>

</http>

security="none"  設置此資源不被攔截.

login-page:指定登錄頁面。

 

authentication-failure-url:指定了身份驗證失敗時跳轉到的頁面。

 

default-target-url:指定了成功進行身份驗證和授權後預設呈現給用戶的頁面。

 

csrf disabled="true"  關閉csrf ,如果不加會出現錯誤

ok.

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • activemq配置jmx 配置activemq中的jmx可以用於監控activemq信息。 activemq.xml配置 修改broker屬性 添加節點managementContext <managementContext> <managementContext createConnector= ...
  • 更新Composer依賴報錯處理 Fatal error: Declaration of Fxp\Composer\AssetPlugin\Repository\AbstractAssetsRe pository::search() must be compatible with Composer\... ...
  • 在 WEB 項目中返回 JSON 數據是常見的交互形式,在 Spring Boot 中這一切都變得十分簡單。So easy!!! 你所需具備的基礎 "什麼是 Spring Boot?" "Spring Boot 核心配置文件詳解" "Spring Boot 開啟的 2 種方式" "Spring Bo ...
  • 簡介 從今天開始,我們嘗試用2篇博客的內容量,搞定一個網站叫做“美空網”網址為:http://www.moko.cc/, 這個網站我分析了一下,我們要爬取的圖片在 下麵這個網址 http://www.moko.cc/post/1302075.html 然後在去分析一下,我需要找到一個圖片列表頁面是最 ...
  • Python 中可以讀取 word 文件的庫有 python-docx 和 pywin32。 pywin32 這個庫很強大,不僅僅可以讀取 word,但是網上介紹用 pywin32 讀取 .doc 的文章真不多,因為,真心不好用。 以下是 pywin32 讀取 .doc 的代碼示例,但是讀取表格有問 ...
  • 在我刻板的印象里,西游記里的那段孫悟空和二郎神的精彩對戰就能很好的解釋“多態”這個詞:一個孫悟空,能七十二變;一個二郎神,也能七十二變;他們都可以變成不同的形態,但只需要悄悄地喊一聲“變”。 ...
  • 題目內容: 設計一個表示分數的類Fraction。這個類用兩個int類型的變數分別表示分子和分母。 這個類的構造函數是: Fraction(int a, int b) 構造一個a/b的分數。 這個類要提供以下的功能: double toDouble(); 將分數轉換為double Fraction ...
  • Onenet控制繼電器教程 Onenet控制繼電器教程 本文基於STM32物聯網開發版:https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.29e71debNLqzWg&id=583890254748 12-18日有大額優惠券哦!!! 內 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...