欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot如何使用Spring Security進行安全控制

 更新時間:2017年04月27日 16:34:22   作者:Joker_Ye  
要實現(xiàn)訪問控制的方法多種多樣,可以通過Aop、攔截器實現(xiàn),也可以通過框架實現(xiàn),本文將具體介紹在Spring Boot中如何使用Spring Security進行安全控制。

我們在編寫Web應用時,經常需要對頁面做一些安全控制,比如:對于沒有訪問權限的用戶需要轉到登錄表單頁面。要實現(xiàn)訪問控制的方法多種多樣,可以通過Aop、攔截器實現(xiàn),也可以通過框架實現(xiàn)(如:Apache Shiro、spring Security)。

本文將具體介紹在Spring Boot中如何使用Spring Security進行安全控制。

準備工作

首先,構建一個簡單的Web工程,以用于后續(xù)添加安全控制,也可以用之前Chapter3-1-2 做為基礎工程。若對如何使用Spring Boot構建Web應用,可以先閱讀 《Spring Boot開發(fā)Web應用》 一文。

Web層實現(xiàn)請求映射

@Controller
public class HelloController {

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

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

}
  1. / :映射到index.html
  2. /hello :映射到hello.html

實現(xiàn)映射的頁面

src/main/resources/templates/index.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 
  <head>
    <title>Spring Security入門</title>
  </head>
  <body>
    <h1>歡迎使用Spring Security!</h1>
    <p>點擊 <a th:href="@{/hello}" rel="external nofollow" >這里</a> 打個招呼吧</p>
  </body>
</html> 

src/main/resources/templates/hello.html

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

可以看到在index.html中提供到 /hello 的鏈接,顯然在這里沒有任何安全控制,所以點擊鏈接后就可以直接跳轉到hello.html頁面。

整合Spring Security

在這一節(jié),我們將對 /hello 頁面進行權限控制,必須是授權用戶才能訪問。當沒有權限的用戶訪問后,跳轉到登錄頁面。

添加依賴

在pom.xml中添加如下配置,引入對Spring Security的依賴。

<dependencies> 
  ...
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  ...
</dependencies> 

Spring Security配置

創(chuàng)建Spring Security的配置類 WebSecurityConfig ,具體如下:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/", "/home").permitAll()
        .anyRequest().authenticated()
        .and()
      .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
      .logout()
        .permitAll();
  }

  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");
  }

}

  • 通過 @EnableWebMvcSecurity 注解開啟Spring Security的功能
  • 繼承 WebSecurityConfigurerAdapter ,并重寫它的方法來設置一些web安全的細節(jié)
  • configure(HttpSecurity http) 方法
    • 通過 authorizeRequests() 定義哪些URL需要被保護、哪些不需要被保護。例如以上代碼指定了 / 和 /home 不需要任何認證就可以訪問,其他的路徑都必須通過身份驗證。
    • 通過 formLogin() 定義當需要用戶登錄時候,轉到的登錄頁面。
  • configureGlobal(AuthenticationManagerBuilder auth) 方法,在內存中創(chuàng)建了一個用戶,該用戶的名稱為user,密碼為password,用戶角色為USER。

新增登錄請求與頁面

在完成了Spring Security配置之后,我們還缺少登錄的相關內容。

HelloController中新增 /login 請求映射至 login.html

@Controller
public class HelloController {

  // 省略之前的內容...

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

}

新增登錄頁面: src/main/resources/templates/login.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
   xmlns:th="http://www.thymeleaf.org"
   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  <head>
    <title>Spring Security Example </title>
  </head>
  <body>
    <div th:if="${param.error}">
      用戶名或密碼錯
    </div>
    <div th:if="${param.logout}">
      您已注銷成功
    </div>
    <form th:action="@{/login}" method="post">
      <div><label> 用戶名 : <input type="text" name="username"/> </label></div>
      <div><label> 密 碼 : <input type="password" name="password"/> </label></div>
      <div><input type="submit" value="登錄"/></div>
    </form>
  </body>
</html> 

可以看到,實現(xiàn)了一個簡單的通過用戶名和密碼提交到 /login 的登錄方式。

根據(jù)配置,Spring Security提供了一個過濾器來攔截請求并驗證用戶身份。如果用戶身份認證失敗,頁面就重定向到 /login?error ,并且頁面中會展現(xiàn)相應的錯誤信息。若用戶想要注銷登錄,可以通過訪問 /login?logout 請求,在完成注銷之后,頁面展現(xiàn)相應的成功消息。

到這里,我們啟用應用,并訪問 http://localhost:8080/ ,可以正常訪問。但是訪問 http://localhost:8080/hello 的時候被重定向到了 http://localhost:8080/login 頁面,因為沒有登錄,用戶沒有訪問權限,通過輸入用戶名user和密碼password進行登錄后,跳轉到了Hello World頁面,再也通過訪問 http://localhost:8080/login?logout ,就可以完成注銷操作。

為了讓整個過程更完成,我們可以修改 hello.html ,讓它輸出一些內容,并提供“注銷”的鏈接。

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  <head>
    <title>Hello World!</title>
  </head>
  <body>
    <h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
    <form th:action="@{/logout}" method="post">
      <input type="submit" value="注銷"/>
    </form>
  </body>
</html>

本文通過一個最簡單的示例完成了對Web應用的安全控制,Spring Security提供的功能還遠不止于此,更多Spring Security的使用可參見 Spring Security Reference 。

完整示例: Chapter4-3-1

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 修改maven項目端口號的方法

    修改maven項目端口號的方法

    今天小編就為大家分享一篇修改maven項目端口號的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • C++排序算法之桶排序原理及實現(xiàn)詳解

    C++排序算法之桶排序原理及實現(xiàn)詳解

    這篇文章主要介紹了C++排序算法之桶排序原理及實現(xiàn)詳解, C++ 桶排序是一種線性時間復雜度的排序算法,它通過將待排序元素分配到不同的桶中,然后對每個桶中的元素進行排序,最后將所有桶中的元素按順序合并得到有序序列,需要的朋友可以參考下
    2023-10-10
  • SpringBoot實現(xiàn)熱部署的三種方式

    SpringBoot實現(xiàn)熱部署的三種方式

    本文主要介紹了SpringBoot實現(xiàn)熱部署的三種方式,主要包括配置pom.xml文件,使用插件的執(zhí)行命令mvn spring-boot:run啟動項,使用springloader本地啟動修改jvm參數(shù),使用devtools工具包,感興趣的可以了解一下
    2023-12-12
  • 詳解Kotlin中的面向對象(二)

    詳解Kotlin中的面向對象(二)

    這篇文章主要介紹了詳解Kotlin中的面向對象(二)的相關資料,需要的朋友可以參考下
    2017-06-06
  • Java基于JDBC實現(xiàn)事務,銀行轉賬及貨物進出庫功能示例

    Java基于JDBC實現(xiàn)事務,銀行轉賬及貨物進出庫功能示例

    這篇文章主要介紹了Java基于JDBC實現(xiàn)事務,銀行轉賬及貨物進出庫功能,較為詳細的分析了事務操作的原理、實現(xiàn)方法及java基于jdbc連接數(shù)據(jù)庫實現(xiàn)銀行事務操作的相關技巧,需要的朋友可以參考下
    2017-12-12
  • Java的回調機制實例詳解

    Java的回調機制實例詳解

    這篇文章主要介紹了Java的回調機制,結合實例形式詳細分析了java回調機制相關原理、用法及操作注意事項,需要的朋友可以參考下
    2019-08-08
  • Spring復雜對象創(chuàng)建的方式小結

    Spring復雜對象創(chuàng)建的方式小結

    這篇文章主要介紹了Spring復雜對象創(chuàng)建的三種方式,現(xiàn)在使用Spring如何創(chuàng)建這種類型的對象?Spring中提供了三種方法來創(chuàng)建復雜對象,需要的朋友可以參考下
    2022-01-01
  • Java 中synchronize函數(shù)的實例詳解

    Java 中synchronize函數(shù)的實例詳解

    這篇文章主要介紹了Java 中synchronize函數(shù)的實例詳解的相關資料,希望通過本文能幫助到大家理解使用synchronize函數(shù)的使用方法,需要的朋友可以參考下
    2017-09-09
  • Javaweb實現(xiàn)上傳下載文件的多種方法

    Javaweb實現(xiàn)上傳下載文件的多種方法

    本篇文章主要介紹了Javaweb實現(xiàn)上傳下載文件,有多種實現(xiàn)方式,需要的朋友可以參考下。
    2016-10-10
  • SpringCloud微服務集成Dubbo的詳細過程

    SpringCloud微服務集成Dubbo的詳細過程

    Apache?Dubbo?是一款易用、高性能的?WEB?和?RPC?框架,同時為構建企業(yè)級微服務提供服務發(fā)現(xiàn)、流量治理、可觀測、認證鑒權等能力、工具與最佳實踐,這篇文章主要介紹了SpringCloud微服務集成Dubbo,需要的朋友可以參考下
    2024-03-03

最新評論