使用Spring Security安全控制

來源:互聯網
上載者:User

標籤:Spring   Java   Spring Boot   

準備工作

首先,構建一個簡單的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頁面。

使用Spring Security安全控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.