使用Spring Security安全控制

準備工作

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

Web層實現請求映射

@Controller
public class HelloController {

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

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

}

/:映射到index.html
/hello:映射到hello.html
實現映射的頁面

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}">這裏</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頁面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章