NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
**1. pom中引入spring-boot-starter-security依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` **2. controller层** ```java @RestController public class IndexController { @RequestMapping("/index") public String index() { return "Hello World!"; } } ``` **3. 启动项目并访问:http://localhost:8080/index** 如果没有登录,Spring Security 会自动重定向到默认的登录页面 http://localhost:8080/login ,如下。 ![](https://img.kancloud.cn/51/3f/513f5a28acfad272a414afbcf178b19e_2064x436.png) 输入用户名和密码登录。注意,每次启动项目密码都会发生变化,密码会打印到控制台中,用户名默认为`user`。 ``` Using generated security password: be53ad37-3a68-4ca0-8c73-32a774661e9e ```