入门案例
创建SpringBoot项目
在pom中加入web、security的依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
编写一个测试的Controller
@RestControllerpublic class TestController {@GetMapping("/add")public String add() {return "hello security";}}
启动该项目,在启动时会在控制台打印一句话:
Using generated security password: 9042005d-094d-45a1-87ef-a840378f5e76
此时直接在浏览器访问:http://localhost:8080/add ,并不会直接返回预期的字符串,而是跳转到了一个登录页面。
spring security登录页面的默认用户名为 user,密码为控制台打印出来的那句UUID。
登录之后即可看到 hello security。
