别再死记硬背了!用‘水果摊’和‘回收站’的故事,5分钟搞懂Java泛型PECS
2026/6/3 6:21:00
Shiro是一个强大且灵活的开源框架,能够非常清楚的处理认证,授权,管理会话以密码加密
SpringSecurity
首先我们需要添加maven依赖
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.guslegend</groupId> <artifactId>SpringSecurityDemo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引用spring security依赖 --> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>编写controller
@RestController public class SecurityTestController { @GetMapping("/hello") public String hello() { return "Hello World!"; } }但是当我们访问这个接口时,会自动跳转到http://localhost:8080/login。
通过分析网页结构我们可以知道其实由以下几个部分组成:
post,/login;username,password;_csrfvalue值为d4329889-796a-447a-9d08-69e56bc7c296还有一个问题,我们在此前并没设计用户名和密码,甚至连数据库都没有!
我们应该怎么样才可以登录进去呢?
我们通过查看系统的日志,发现其用户名是user,密码是909aee59-ec25-4808-bcac-f3b3a82a7aec。
将用户名和密码输入,发现访问成功,这样最基础的SpringSecurity的案例就成功了。