### 搭建一个基础的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`即可登录成功。