日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security 基本介绍,初窥路径

發(fā)布時間:2024/4/14 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security 基本介绍,初窥路径 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring Security 基本介紹和環(huán)境搭建

實(shí)驗(yàn)介紹

Spring Security 是一個非常強(qiáng)大的身份驗(yàn)證和授權(quán)控制框架。為了滿足企業(yè)項(xiàng)目的不同需求,它提供了很多定制化開發(fā)的解決方案,通過簡單的調(diào)整配置,就能為我們的應(yīng)用提供一套可靠的安全保障。本節(jié)課程主要任務(wù)就是搞清楚 Spring Security 是干什么的,以及它的基本用法。

知識點(diǎn)

  • 系統(tǒng)安全和系統(tǒng)保護(hù)設(shè)計(jì)
  • Spring Security 核心功能介紹
  • Spring Security 項(xiàng)目搭建
  • Spring Security 的基本操作

系統(tǒng)安全和系統(tǒng)保護(hù)設(shè)計(jì)

在實(shí)際開發(fā)過程中,為了保證我們的系統(tǒng)能夠安全穩(wěn)定的運(yùn)行下去,一般都要從下面兩點(diǎn)來考慮:

  • 系統(tǒng)安全性:防止非法入侵、非法請求、非法攔截等。我們需要阻止和屏蔽不信任的請求源訪問,保證數(shù)據(jù)的安全可靠,不被人竊取。
  • 系統(tǒng)健壯性:也就是系統(tǒng)可用性,最常見的解決方案就是做服務(wù) “冗余”。當(dāng)然量級夠大的話,要做的事情會很多很多,比如限流、熔斷、降級等等。
  • 這里只簡單的談一談系統(tǒng)的安全性,在項(xiàng)目的開發(fā)中需要從全方位、多角度做工作,以確保整個業(yè)務(wù)鏈路、整個體系范圍都能保證安全。下面就大致介紹下在實(shí)際開發(fā)過程中,開發(fā)者經(jīng)常用到的一些方法:

    • 數(shù)據(jù)校驗(yàn),包括前端 js 校驗(yàn)和后端校驗(yàn),其實(shí)前端校驗(yàn)主要是為了體驗(yàn),也就是盡可能降低出錯率,提高一次性提交的成功率。也可以說前端校驗(yàn)規(guī)則是后端校驗(yàn)的子集。

    • 防止命令注入,比如最常見的 SQL 注入,它不是利用操作系統(tǒng)的 BUG 來實(shí)現(xiàn)攻擊,而是針對程序員編程時的疏忽,通過 SQL 語句,實(shí)現(xiàn)無帳號登錄,甚至篡改數(shù)據(jù)庫。

    • 認(rèn)證安全,對于使用應(yīng)用的實(shí)體,無論是人還是系統(tǒng)程序,都應(yīng)當(dāng)做到對每個請求都能找到對應(yīng)的責(zé)任實(shí)體。因此,在處理請求前,要先對認(rèn)證信息進(jìn)行檢測。

    • 登錄鑒權(quán),即要控制這個用戶登錄后能在系統(tǒng)中做什么,比如一般要把用戶分為外部用戶、員工等。

    • 數(shù)據(jù)加密,對于敏感數(shù)據(jù),不得明文傳輸和明文存儲。如數(shù)據(jù)存儲中,密碼等信息我們可以加密后再存儲;數(shù)據(jù)傳輸中,對密文使用 DES3/RSA 加密。

    • 請求簽名,在外部請求時也是常見的處理方式,只有通過接口簽名驗(yàn)證的請求,才信任為合法的請求。

    在系統(tǒng)的安全方面,我們的 Spring Security 框架,解決的最主要的問題就是?認(rèn)證安全?和?登錄鑒權(quán)。

    Spring Security 核心功能介紹

    Spring Security 其核心就是一組過濾器鏈,項(xiàng)目啟動后將會自動配置。最核心的就是 Basic Authentication Filter 用來認(rèn)證用戶的身份,一個在 Spring Security 中一種過濾器處理一種認(rèn)證方式。比如,對于 username password 認(rèn)證過濾器來說:

    • 會檢查是否是一個登錄請求;
    • 是否包含 username 和 password (也就是該過濾器需要的一些認(rèn)證信息);
    • 如果不滿足則放行給下一個。

    然后下一個認(rèn)證過濾器,再次按照自身職責(zé)判定是否是自身需要的信息。中間可能還有更多的認(rèn)證過濾器,只要有一個認(rèn)證過濾器通過了,就是用戶登錄成功。

    在整個過濾器中的最后一環(huán)是?FilterSecurityInterceptor,這里會判定該請求是否能進(jìn)行訪問 REST 服務(wù),如果被拒絕了就會拋出不同的異常(根據(jù)具體的原因)。

    Exception Translation Filter?會捕獲拋出的錯誤,然后根據(jù)不同的認(rèn)證方式進(jìn)行信息的返回提示。

    項(xiàng)目搭建

    概念一下說多了也未必好理解,還是先上手,后面再慢慢熟悉比較好。打開實(shí)驗(yàn)樓環(huán)境,依次點(diǎn)擊 Terminal --> New Terminal(打開命令行),可以看到下方出現(xiàn)命令行工具,創(chuàng)建我們的第一個項(xiàng)目(hello_security),直接輸入下面的命令:

    mvn archetype:generate -DgroupId=com.shiyanlou -DartifactId=hello_security -DarchetypeArtifactId=maven-archetype-webapp

    輸入完,然后直接回車即可自動創(chuàng)建項(xiàng)目。我們創(chuàng)建一個基于 Spring Boot 的 Web 項(xiàng)目,maven 全部依賴如下:
    hello_security/pom.xml

    <dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.8.RELEASE</version><scope>import</scope><type>pom</type></dependency></dependencies> </dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies>

    這個項(xiàng)目就是個簡單的 Spring Boot 項(xiàng)目,先把需要的目錄創(chuàng)建好,按照下面的目錄結(jié)構(gòu)創(chuàng)建出來:

    也就是在?src/main/?下創(chuàng)建?java?目錄,再在其下面創(chuàng)建?com/shiyanlou?包目錄,同時在該目錄下創(chuàng)建我們的 Spring Boot 啟動類:
    hello_security/src/main/java/com/shiyanlou/Application.java

    package com.shiyanlou;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}

    再在?src/main/resources/?下創(chuàng)建?templates?目錄,然后在?src/main/resources/templates?目錄下創(chuàng)建兩個頁面:
    home.html

    <!DOCTYPE html> <htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" ><head><title>home.html</title></head><body><h1>Welcome!</h1><p>Click<a th:href="@{/hello}">here</a>to see a greeting.</p></body> </html>

    hello.html

    <!DOCTYPE html> <htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" ><head><title>hello.html</title></head><body><h1>Hello world!</h1></body> </html>

    頁面很簡單,要做的事也很簡單:點(diǎn)擊?home?視圖中的?“/hello”?鏈接跳轉(zhuǎn)到?hello?頁面。首先配置視圖控制器來暴露這些頁面, 如下是一個典型的 Spring MVC 配置類:

    hello_security/src/main/java/com/shiyanlou/MvcConfig.java

    package com.shiyanlou;import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration public class MvcConfig implements WebMvcConfigurer {public void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}

    第四個視圖控制器引用另一個名為?login?的視圖,后面會用上,先不管。現(xiàn)在可以啟動項(xiàng)目了,這里注意,在實(shí)驗(yàn)樓 WebIDE 中運(yùn)行項(xiàng)目需要命令行啟動,必須在 pom.xml 中添加 maven 插件:

    hello_security/pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" ><modelVersion>4.0.0</modelVersion><groupId>com.shiyanlou</groupId><artifactId>hello_security</artifactId><version>1.0-SNAPSHOT</version><build><plugins><!--spring boot 打包插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.shiyanlou.Application</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build> </project>

    同時命令行中切換到自己的項(xiàng)目目錄下(和 pom.xml 同級),再執(zhí)行?mvn spring-boot:run?命令:

    等項(xiàng)目啟動完成后,點(diǎn)擊右側(cè)菜單的?Web 服務(wù)?按鈕,將會自動打開瀏覽器,看到下面的運(yùn)行效果:

    本文內(nèi)容來自藍(lán)橋云課《玩轉(zhuǎn) Spring Security 從入門到實(shí)戰(zhàn)》 ,點(diǎn)擊可查看全部內(nèi)容。

    總結(jié)

    以上是生活随笔為你收集整理的Spring Security 基本介绍,初窥路径的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。