场景:

有3个页面:首页、登录页、登录成功后的主页面,如下图:

springboot11-01-security入门

 

springboot11-01-security入门

 

 
springboot11-01-security入门

 

 

 

 

 

 

 

 

 

 

如果没有登录,点击“去主页”,会跳转到登录页

如果已经登录,点击“去主页”,跳转到主页,显示“hello 用户名”

 

下面用springboot + spring security简单实现:

1.新建maven项目,添加pom支持:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.mlxs.springboot11.security01</groupId>
 8     <artifactId>springboot11-security01</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <!--父依赖包-->
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>1.4.2.RELEASE</version>
16         <relativePath/>
17     </parent>
18 
19     <properties>
20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter</artifactId>
28         </dependency>
29         <!--测试-->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35         <!--mvc-->
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-web</artifactId>
39         </dependency>
40         <!-- security -->
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-starter-security</artifactId>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-starter-thymeleaf</artifactId>
48         </dependency>
49     </dependencies>
50 </project>
View Code

相关文章:

  • 2021-04-29
  • 2021-09-26
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2021-05-01
  • 2021-04-16
  • 2021-07-01
猜你喜欢
  • 2022-12-23
  • 2022-01-21
  • 2021-12-07
  • 2022-02-13
  • 2021-09-23
相关资源
相似解决方案