1、搭建一个springboot项目
2、导入相应jar包Maven坐标
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>kgf-java-learning</artifactId> <groupId>com.kgf.learning</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../kgf-java-learning/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.kgf.security</groupId> <artifactId>spring-security-demo</artifactId> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.11.RELEASE</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
3、启动类
package com.kgf.security; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SecurityApplication { public static void main(String[] args) { SpringApplication.run(SecurityApplication.class,args); } }
4、修改配置文件
5、创建一个hello测试接口
package com.kgf.security.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("test") @RestController public class TestController { @RequestMapping("hello") public String test(){ return "hello security"; } }
6、运行项目
- 访问http://localhost:8081/test/hello,会出现如下的界面:
- 默认的用户名是:user。
- 密码在项目启动的时候会在控制台打印,
需要注意的是,每次启动密码都会发生变化
。
输入用户名和密码,就可以访问了: