国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Spring Boot如何使用Spring Security進行安全控制

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

2020-09-20 14:25Joker_Ye JAVA教程

要實現訪問控制的方法多種多樣,可以通過Aop、攔截器實現,也可以通過框架實現,本文將具體介紹在Spring Boot中如何使用Spring Security進行安全控制。

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

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

準備工作

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

Web層實現請求映射

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class HelloController {
 
  @RequestMapping("/")
  public String index() {
    return "index";
  }
 
  @RequestMapping("/hello")
  public String hello() {
    return "hello";
  }
 
}
  1. / :映射到index.html
  2. /hello :映射到hello.html

實現映射的頁面

src/main/resources/templates/index.html

?
1
2
3
4
5
6
7
8
9
10
<!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

?
1
2
3
4
5
6
7
8
9
10
<!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

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

添加依賴

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

?
1
2
3
4
5
6
7
8
<dependencies>
  ...
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  ...
</dependencies>

Spring Security配置

創建Spring Security的配置類 WebSecurityConfig ,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@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安全的細節
  • configure(HttpSecurity http) 方法
    • 通過 authorizeRequests() 定義哪些URL需要被保護、哪些不需要被保護。例如以上代碼指定了 / 和 /home 不需要任何認證就可以訪問,其他的路徑都必須通過身份驗證。
    • 通過 formLogin() 定義當需要用戶登錄時候,轉到的登錄頁面。
  • configureGlobal(AuthenticationManagerBuilder auth) 方法,在內存中創建了一個用戶,該用戶的名稱為user,密碼為password,用戶角色為USER。

新增登錄請求與頁面

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

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

?
1
2
3
4
5
6
7
8
9
10
11
@Controller
public class HelloController {
 
  // 省略之前的內容...
 
  @RequestMapping("/login")
  public String login() {
    return "login";
  }
 
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!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>

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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

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

原文鏈接:http://blog.csdn.net/hj7jay/article/details/51730284

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品欧美一区二区三区久久久 | 一区二区精品在线 | 亚洲精品久久久久久动漫 | 日韩欧美一区二区三区免费观看 | 欧美 日韩 成人 | 在线成人av| 日韩久久久久久 | 欧美成人精品一区二区 | 国产精品网站在线观看 | 日韩成人在线播放 | 一二三精品区 | 欧美一区二区三区在线观看视频 | 国产在线a | 国产精品视频播放 | 粉嫩欧美一区二区三区高清影视 | 久色网| 精品国产一区二区在线 | 成人在线一区二区 | 成人日日夜夜 | 一级片| 国产精品一级毛片在线 | av毛片| 午夜免费| 亚洲一区中文字幕在线观看 | 欧美三区| 欧美一级二级三级视频 | 国产玖玖 | 在线观看成人 | 国产高清一区二区 | 欧美不卡一区二区三区 | 国产精品亚洲视频 | 中文在线а√在线8 | av中文字幕在线观看 | 午夜草民福利电影 | 偷拍第一页 | 韩国精品一区 | 日本黄a三级三级三级 | 羞羞的视频在线免费观看 | 久久久一区二区三区 | 自拍偷拍视频网站 | 在线亚洲免费 |