SpringSecurity---簡介以及HelloWorld環境搭建(所需資料文末免費下載)

第一章 SpringSecurity-簡介

1.簡介

https://docs.spring.io/spring-security/site/docs/4.2.10.RELEASE/guides/html5/helloworld-xml.html

SpringSecurity融合Spring技術棧,提供JavaEE應     用的整體安全解決方案

Spring Security爲基於Java EE的企業軟件應用提供全面的安全服務

Spring Security只需要少量配置,就能構建一個強大的安全的應用系統。 

目前市面上受歡迎的兩個安全框架:Apache Shiro、SpringSecurity;

SpringSecurity可以無縫整合Spring應用,具有強大的自動化web安全管控功能。而Shiro是一個輕量級強大的安全框架,可以脫離web應用來提供安全管控,但是對於web的一些定製安全需要手動編寫;SpringBoot底層默認整合SpringSecurity作爲安全框架,所以我們推薦web應用使用SpringSecurity來控制安全;

2.概念

 authentication:身份驗證

“身份驗證”是指建立主體(principal)的過程,主體就是他們聲稱是誰(“主體”通常指用戶設備或在應用程序中可以執行動作的其他系統)。也就是“證明你是誰

authorization:授權

“授權”是指確定主體(principal是否被允許執行系統中某個動作的過程。 也就是“你能做什麼!”

爲了達到“授權”決策(安全框架決定你是否有權限做此事),“身份驗證”(authentication)過程已經建立了主體的身份(Principal)

3.文檔

Hello Spring Security <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/helloworld > 基於Java配置整合示例

Hello Spring Security Boot <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/boot/helloworld> 與SpringBoot整合案例

Hello Spring Security XML <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/xml/helloworld> 基於XML方式整合示例

Hello Spring MVC Security <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/hellomvc> SpringMVC集成示例

Custom Login Form <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/form> 自定義登錄表單示例

4.支持的身份認證模式

在身份驗證級別,Spring Security支持廣泛的認證模型。這些認證模型中的大部分要麼由第三方提供,要麼由相關標準機構(如互聯網工程任務組)開發。此外,Spring Security提供了自己的一套身份驗證功能。具體而言,Spring Security當前支持與所有這些技術的身份驗證集成;

1.HTTP BASIC身份驗證標頭

HTTP BASIC authentication headers (an IETF RFC-based standard)

參考:https://blog.csdn.net/lvxinzhi/article/details/49000003

2.HTTP Digest身份驗證標頭

HTTP Digest authentication headers (an IETF RFC-based standard)

3.HTTP X.509客戶端證書交換

HTTP X.509 client certificate exchange (an IETF RFC-based standard)

4.LDAP(一種非常常見的跨平臺身份驗證方法,特別是在大型環境中)

LDAP (a very common approach to cross-platform authentication needs, especially in large environments)

5.基於表單的身份驗證(用於簡單的用戶界面需求)

Form-based authentication (for simple user interface needs)

6.OpenID身份驗證

OpenID authentication   第三方登陸如:微信,qq。

7.基於預先建立的請求標頭的身份驗證

Authentication based on pre-established request headers (such as Computer Associates Siteminder)

8.Jasig中央認證服務(也稱爲CAS,是一種流行的開源單點登錄系統)

Jasig Central Authentication Service (otherwise known as CAS, which is a popular open source single sign-on system)

9.遠程方法調用(RMI)和HttpInvoker(Spring遠程協議)的透明身份驗證上下文傳播

Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (a Spring remoting protocol)

10.自動“記住我”身份驗證

Automatic "remember-me" authentication (so you can tick a box to avoid re-authentication for a predetermined period of time)

11.匿名身份驗證(允許每個未經身份驗證自動承擔特定的安全身份)

Anonymous authentication (allowing every unauthenticated call to automatically assume a particular security identity)

12.Runas身份驗證(如果一個調用應繼續使用不同的安全標識,則非常有用)

Run-as authentication (which is useful if one call should proceed with a different security identity)

13.Java身份驗證和授權服務(JAAS)

Java Authentication and Authorization Service (JAAS)

14.JavaEE容器身份驗證(如果需要,您仍然可以使用容器管理身份驗證)

Java EE container authentication (so you can still use Container Managed Authentication if desired)

15.Java開源單點登錄(JOSSO

Java Open Source Single Sign-On (JOSSO) *

16.OpenNMS網絡管理平臺*

OpenNMS Network Management Platform *

17.您自己的身份驗證系統

Your own authentication systems (see below)

18.其他

Kerberos

AppFuse *

AndroMDA *

Mule ESB *

Direct Web Request (DWR) *

Grails *

Tapestry *

JTrac *

Jasypt *

Roller *

Elastic Path *

Atlassian Crowd

5.模塊劃分

Core - spring-security-core.jar

核心模塊

核心認證、授權功能、支持jdbc-user功能、支持獨立的Spring應用

Remoting - spring-security-remoting.jar

遠程交互模塊

一般不需要,可以使用Spring Remoting功能簡化遠程客戶端交互

Web - spring-security-web.jar

web安全模塊

web項目使用,基於URL的訪問控制(access-control)

Config - spring-security-config.jar

java配置模塊

必須依賴包,包含解析xml方式和java 註解方式來使用SpringSecurity功能

LDAP - spring-security-ldap.jar

ldap(輕量目錄訪問協議)支持模塊

可選依賴包,LDAP功能支持

ACL - spring-security-acl.jar

ACL支持

ACLAccess-Control-List訪問控制列表

細粒度的資源訪問控制(RBAC+ACL)

CAS - spring-security-cas.jar

CAS整合支持

CASCentral Authentication Service)中央認證服務。開源ApereoCAS整合

OpenID - spring-security-openid.jar

OpenID認證方式支持

OpenID Web身份驗證支持。 用於針對外部OpenID服務器對用戶進行身份驗證

(微信,qq,新浪微博等第三方登錄)

Test - spring-security-test.jar

測試模塊

快速的測試SpringSecurity應用

 

6.4種使用方式

  1. 一種是全部利用配置文件,將用戶、權限、資源(url)硬編碼在xml文件中
  2. 二種是用戶和權限用數據庫存儲,而資源(url)和權限的對應採用硬編碼配置
  3. 三種是細分角色和權限,並將用戶、角色、權限和資源均採用數據庫存儲,並且自定義過濾器,代替原有的FilterSecurityInterceptor過濾器, 並分別實現AccessDecisionManagerInvocationSecurityMetadataSourceServiceUserDetailsService,並在配置文件中進行相應配置。
  4. 四是修改springsecurity的源代碼,主要是修改InvocationSecurityMetadataSourceService和UserDetailsService兩個類。
    1. InvocationSecurityMetadataSourceService
      1. 將配置文件或數據庫中存儲的資源(url)提取出來加工成爲url和權限列表的Map供Security使用
    2. UserDetailsService
      1. 提取用戶名和權限組成一個完整的(UserDetails)User對象,該對象可以提供用戶的詳細信息供AuthentationManager進行認證與授權使用

第二章 SpringSecurity-HelloWorld

2.1 測試環境搭建

2.1.1 創建普通maven-war工程:spring-security-helloworld

  1. pom文件增加依賴

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>4.3.20.RELEASE</version>

              </dependency>

              <dependency>

                     <groupId>javax.servlet.jsp</groupId>

                     <artifactId>jsp-api</artifactId>

                     <version>2.2</version>

                     <scope>provided</scope>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>servlet-api</artifactId>

                     <version>2.5</version>

                     <scope>provided</scope>

              </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

              </dependency>

2.1.2 web.xml配置

<servlet>

<servlet-name>springDispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

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

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

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springDispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

2..1.3 spring配置:spring.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:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

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-4.3.xsd">

<context:component-scan base-package="com.atguigu.security"></context:component-scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<mvc:annotation-driven />

<mvc:default-servlet-handler />

</beans>

2.1.4 導入實驗資源

  1. 導入頁面

  1. 導入controller

2.1.5 運行測試

2.2 引入SpringSecurity框架

2.2.1 添加security-pom依賴

<dependency>

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

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

<version>4.2.10.RELEASE</version>

</dependency>

<dependency>

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

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

<version>4.2.10.RELEASE</version>

</dependency>

<!-- 標籤庫 -->

<dependency>

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

<artifactId>spring-security-taglibs</artifactId>

<version>4.2.10.RELEASE</version>

</dependency>

2.2.2 web.xml中添加SpringSecurity的Filter進行安全控制

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

2.2.3 加入SpringSecurity配置類

  1. @Configuration@Bean 註解作用

@Configuration

@EnableWebSecurity

public class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {

}

 

2.2.4 啓動測試效果

  1. 所有資源訪問受限(包括靜態資源)
  2. 需要一個默認的登錄頁面(框架自帶的)
  3. 賬號密碼錯誤會有提示
  4. 查看登錄頁面的源碼,發現有個hidden-input;name="_csrf" 這是SpringSecurity幫我們防止“跨站請求僞造”攻擊;還可以防止表單重複提交
  5. 登陸失敗後跳轉到框架自帶的登陸界面,並有失敗信息報出。

http://localhost:8080/spring-security-helloworld/login?error

 

 

導入的模板已經上傳到csdn,供大家學習時免費下載,下載後導入即可。

https://download.csdn.net/download/m2606707610/12122449

發佈了241 篇原創文章 · 獲贊 87 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章