一、关于Spring Security
Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。
对于上面提到的两种应用情景,Spring Security 框架都有很好的支持。在用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。
二、使用Spring Security创建一个基础项目
这里主要参考了spring官网的一个指南,地址:https://spring.io/guides/gs/securing-web/。
首先引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
然后创建登录页和主页以及对应controller
login.html
<form class="form-signin" th:action="@{/login}" method="post">
<div class="text-center mb-4">
<h1 class="h3 mb-3 font-weight-normal">Welcome</h1>
</div>
<div class="form-label-group">
<input type="text" id="inputUsername" name="username" class="form-control" placeholder="Email address" required autofocus>
<label for="inputUsername">Email address</label>
</div>
<div class="form-label-group">
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
<label for="inputPassword">Password</label>
</div>
<div class="mb-1" style="color: #ff680e">
<p th:if="${param.error}">Invalid username and password.</p>
<p th:if="${param.logout}"> You have been logged out.</p>
</div>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
index.html
```