<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Security安全認證 | Spring Boot如何集成Security實現(xiàn)安全認證

          共 3904字,需瀏覽 8分鐘

           ·

          2021-12-18 21:54

          前面介紹了Spring Boot 使用JWT實現(xiàn)Token驗證,其實Spring Boot 有完整的安全認證框架:Spring Security。接下來我們介紹如何集成Security 實現(xiàn)安全驗證。


          一、Security簡介

          安全對于企業(yè)來說至關(guān)重要,必要的安全認證為企業(yè)阻擋了外部非正常的訪問,保證了企業(yè)內(nèi)部數(shù)據(jù)的安全。

          當前,數(shù)據(jù)安全問題越來越受到行業(yè)內(nèi)公司的重視。數(shù)據(jù)泄漏很大一部分原因是非正常權(quán)限訪問導致的,于是使用合適的安全框架保護企業(yè)服務的安全變得非常緊迫。在Java領(lǐng)域,Spring Security無疑是最佳選擇之一。

          Spring Security 是 Spring 家族中的一個安全管理框架,能夠基于 Spring 的企業(yè)應用系統(tǒng)提供聲明式的安全訪問控制解決方案。它提供了一組可以在Spring應用系統(tǒng)中靈活配置的組件,充分利用了 Spring的IoC、DI和AOP等特性,為應用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復代碼的工作。


          二、Spring Boot對Security的支持

          雖然,在Spring Boot出現(xiàn)之前,Spring Security已經(jīng)發(fā)展多年,但是使用并不廣泛。安全管理這個領(lǐng)域一直是Shiro的天下,因為相對于Shiro,在項目中集成Spring Security還是一件麻煩的事情,所以Spring Security雖然比Shiro強大,但是卻沒有Shiro受歡迎。


          隨著Spring Boot的出現(xiàn),Spring Boot 對Spring Security 提供了自動化配置方案,可以零配置使用 Spring Security。這使得Spring Security重新煥發(fā)新的活力。


          Spring Boot 提供了集成 Spring Security 的組件包 spring-boot-starter-security,方便我們在 Spring Boot 項目中使用 Spring Security進行權(quán)限控制。


          三、集成Security

          在Spring Boot 項目中集成Spring Boot Security 非常簡單,只需在項目中增加Spring Boot Security的依賴即可。下面通過示例演示Spring Boot中基礎(chǔ)Security的登錄驗證。


          1. 添加依賴

          Spring Boot 提供了集成 Spring Security 的組件包 spring-boot-starter-security,方便我們在 Spring Boot 項目中使用 Spring Security。

          org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-starter-security
          上面除了引入Security組件外,因為我們要做Web系統(tǒng)的權(quán)限驗證,所以還添加了Web和Thymeleaf組件。


          2. 配置登錄用戶名和密碼

          用戶名和密碼在application.properties中進行配置。

          # securityspring.security.user.name=adminspring.security.user.password=admin
          在application.properties配置文件中增加了管理員的用戶名和密碼。


          3. 添加Controller

          創(chuàng)建SecurityController 類,在類中添加訪問頁面的入口。

          @Controllerpublic class SecurityController {    @RequestMapping("/")    public String index() {        return "index";    }}


          4. 創(chuàng)建前端頁面

          在resources/templates 目錄下創(chuàng)建頁面 index.html,這個頁面就是具體的需要增加權(quán)限控制的頁面,只有登錄了才能進入此頁。

          <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><h1>Helloh1><p>我是登錄后才可以看的頁面p>body>html>

          5. 測試驗證

          配置完成后,重啟項目,訪問地址:http://localhost:8080/,頁面會自動彈出一個登錄框,如下圖所示。

          系統(tǒng)自動跳轉(zhuǎn)到Spring Security默認的登錄頁面,輸入之前配置的用戶名和密碼就可以登錄系統(tǒng),登錄后的頁面如下圖所示。

          ????? ??????


          通過上面的示例,我們看到Spring Security自動給所有訪問請求做了登錄保護,實現(xiàn)了頁面權(quán)限控制。


          四、登錄驗證

          前面演示了在Spring Boot項目中集成Spring Security 實現(xiàn)簡單的登錄驗證功能,在實際項目使用過程中,可能有的功能頁面不需要進行登錄驗證,而有的功能頁面只有進行登錄驗證才能訪問。下面通過完整的示例程序演示如何實現(xiàn)Security的登錄認證。

          1. 創(chuàng)建頁面content.html

          先創(chuàng)建頁面content.html,此頁面只有登錄用戶才可查看,否則會跳轉(zhuǎn)到登錄頁面,登錄成功后才能訪問。示例代碼如下:

          <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><h1>contenth1><p>我是登錄后才可以看的頁面p><form method="post" action="/logout">    <button type="submit">退出button>form>body>html>
          在上面的示例中,我們看到退出使用post請求,因為Security退出請求默認只支持 post 。


          2. 修改index.html 頁面

          修改之前的index.html頁面,增加登錄按鈕。

          <p>點擊 <a th:href="@{/content}">這里a>進入管理頁面p>
          在上面的示例中,index頁面屬于公共頁面,無權(quán)限驗證,從index頁面進入content頁面時需要登錄驗證。


          3. 修改Controller控制器

          修改之前的SecurityController控制器,增加content頁面路由地址,示例代碼如下:

          @RequestMapping("/")public String index() {    return "index";} @RequestMapping("/content")public String content() {    return "content";}


          4. 創(chuàng)建 SecurityConfig 類

          創(chuàng)建 Security的配置文件SecurityConfig類,它繼承于 WebSecurityConfigurerAdapter,現(xiàn)自定義權(quán)限驗證配置。示例代碼如下:

          @Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests()            .antMatchers("/", "/home").permitAll()            .anyRequest().authenticated()            .and()            .formLogin()            .permitAll()            .and()            .logout()            .permitAll()            .and()            .csrf()            .ignoringAntMatchers("/logout");    }}
          在上面的示例程序中,SecurityConfig類中配置 index.html 可以直接訪問,但 content.html 需要登錄后才可以查看,沒有登錄自動跳轉(zhuǎn)到登錄頁面。
          • @EnableWebSecurity:開啟 Spring Security 權(quán)限控制和認證功能。

          • antMatchers("/", "/home").permitAll():配置不用登錄可以訪問的請求。

          • anyRequest().authenticated():表示其他的請求都必須有權(quán)限認證。

          • formLogin():定制登錄信息。

          • loginPage("/login"):自定義登錄地址,若注釋掉,則使用默認登錄頁面。

          • logout():退出功能,Spring Security自動監(jiān)控了/logout。

          • ignoringAntMatchers("/logout"):Spring Security 默認啟用了同源請求控制,在這里選擇忽略退出請求的同源限制。


          5. 測試驗證

          修改完成之后重啟項目,訪問地址http://localhost:8080/可以看到 index 頁面的內(nèi)容,單擊鏈接跳轉(zhuǎn)到content頁面時會自動跳轉(zhuǎn)到登錄頁面,登錄成功后才會自動跳轉(zhuǎn)到http://localhost:8080/content,在 content 頁面單擊“退出”按鈕,會退出登錄狀態(tài),跳轉(zhuǎn)到登錄頁面并提示已經(jīng)退出。


          最后

          以上,我們就把Spring Boot如何集成Security實現(xiàn)安全認證介紹完了。


          瀏覽 54
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美精品久久 | 人人干人人模 | 大香蕉伊人免费在线 | 亚洲五月天网站 | 黄色性爱免费 |