1 文档
官方文档:
https://spring.io/projects/spring-security/
Github:https://github.com/spring-projects/spring-security-samples
2 项目结构
3 主要代码
3.1 pom依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
org.example
spring_security
1.0-SNAPSHOT
8
8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-thymeleaf
3.2 启动类
package com.zydgbbs.springsecurity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringSecurityApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityApplication.class);
}
}
3.3 请求路由类
package com.zydgbbs.springsecurity.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/")
public String index(){
return "index";
}
}
3.4 index.html页面
Hello Security!
Hello Security
Log Out
3.5 application.properties配置
# 默认端口8080
#server.port=8090
# 可以修改访问路径前缀
#server.servlet.context-path=/demo
# 自定义用户名和密码
#spring.security.user.name=admin
#spring.security.user.password=admin
4 访问测试
4.1 启动
# 启动时会把默认用户【user】的密码输出出来,方便登录
Using generated security password: bfbe3cdd-ade7-42b4-afe5-56cd8a35e553
4.2 登录
# 访问IP
http://localhost:8080/login
# 输入用户名和密码即可
Username:user
Password:bfbe3cdd-ade7-42b4-afe5-56cd8a35e553
4.3 资源页面
4.4 登出页面
5 坑
当我们访问登录页面时,会非常慢,甚至加载不出来,因为登录页面引用了一个国外的地址。
https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css,需要配置代理才能下载下来。
【温馨提示】
点赞+收藏文章,关注我并私信回复【面试题解析】,即可100%免费领取楼主的所有面试题资料!