构建 springboot 项目
修改pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
写一个 /hello 的资源
@RestController
public class HelloController {
@GetMapping("hello")
public String helloController(){
return "hello world";
}
}
项目目录

访问/hello

整合 spring security
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
访问/hello

访问需要:用户名是user,密码在控制台
2022-08-20 22:47:35.370 INFO 24344 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-08-20 22:47:35.370 INFO 24344 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 885 ms
2022-08-20 22:47:35.489 WARN 24344 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: f58b70c1-7d96-49c5-b5c3-ef5d3ea2caa0
登录成功后访问

本文介绍了如何构建一个SpringBoot项目,从修改pom文件开始,添加spring-boot-starter-web依赖以创建/hello资源。然后,通过引入spring-boot-starter-security依赖,整合SpringSecurity进行权限管理。系统默认生成了用户名和密码,登录成功后可以访问受保护的资源。
3万+

被折叠的 条评论
为什么被折叠?



