### 搭建一個基礎的Web項目
1. 在一個基礎的Maven項目上添加POM文件
```xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
```
2. 添加一個主程序類
```java
@SpringBootApplication
public class SpringSecurityApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityApplication.class, args);
}
}
```
3. 添加一個配置文件`application.yml`
```yaml
server:
port: 80
```
4. 添加一個控制器
```java
@RestController
public class TestController {
@GetMapping("hello")
public String hello() {
return "hello spring security";
}
}
```
5. 啟動程序并訪問http://www.zhangpn.com/hello
> 因為我在本機配置了域名映射,同學可訪問http://localhost/hello,接下來的操作同理更換。
### 為基礎的Web項目添加安全防護
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
啟動項目查看控制臺有個密碼:
```
Using generated security password: 6ceeb963-b850-411a-ae98-27ac1f737531
```
再次訪問http://www.zhangpn.com/hello,系統重定向至登陸頁。輸入用戶名`user`,密碼`6ceeb963-b850-411a-ae98-27ac1f737531`即可登錄成功。