官方主页:https://spring.io/projects/spring-security
Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架。 它是保护基于 Spring 的应用程序的事实标准。
Spring Security 是一个专注于为 Java 应用程序提供身份验证和授权的框架。像所有 Spring 项目一样,Spring Security 的真正强大之处在于它可以轻松扩展以满足自定义需求。
一个 pom,一个 Application 启动类,相信难不倒你,跳过。
版本号继承自SpringBoot。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
8080;增加了一些debug日志,便于定位问题。server:
port: 8080
logging:
level:
root: info
# org.springframework.security.web: debug
org.springframework.security.web.FilterChainProxy: debug #过滤器执行顺序
org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource: info #权限配置
org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource: info #权限配置
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration: debug
src/resources/static/index.html<html>
<head>
<meta charset="utf-8">
<title>首页title>
head>
<body>
<h5>登录成功h5>
<div>
<form method="get" action="/logout">
<button type="submit">Logoutbutton>
form>
div>
body>
html>
运行 启动类

默认用户为:user,密码则时会打印在启动日志中,如图:

输入用户信息登录,登录成功后会重定向到首页。
2. 首页
首页就是我们最开始写的 index.html 页面。

3. 登出
点击 Logout 按钮,会跳转到一个登出确认页面

点击 Log Out,此时才是真正的退出。退出后,会重定向登录页,也会有相应的提示。

前面在登录时,使用的用户密码是框架默认设置的,并不是很友好,实际上,可以在 application.yml 自定义,如下,指定用户为user,密码为123456。
spring:
security:
user:
name: user
password: 123456
end
后续会继续深挖框架的各种配置和功能,有兴趣可以订阅一下,会持续更新