Spring Security-從入門到精通-入門Demo
阿新 • • 發佈:2022-03-27
1 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mangoubiubiu</groupId> <artifactId>security</artifactId> <version>0.0.1-SNAPSHOT</version> <name>security_demo1</name> <description>安全框架學習</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2 controller
package com.mangoubiubiu.security.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginController { @RequestMapping("/main") public String login(){ return "redirect:main.html"; } }
3 自定義靜態頁
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>註冊頁</title>
</head>
<body>
歡迎註冊,請輸入使用者名稱和密碼
<form action="/login" method="post">
<p>姓名:<input type="text" name="name" size="10"></p>
<p>密碼:<input type="password" name="password" size="10"></p>
<p><input type="submit" value="確定">
<input type="reset" value="取消"></p>
</form>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登入成功</title> </head> <body> <h1 style="color: red">登入成功</h1> </body> </html>
4 測試
啟動 輸入
還是轉到了SpringSecurity自己自帶 登入頁,也就是說請求被攔截了
輸入密碼後發現才可以訪問
輸入登入後請求轉發到了