Spring Boot教程(二十六)使用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頁面。

  • 源碼來源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM