我们在编写Web应用时,经常需要对页面做一些安全控制,比如:对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过Aop、拦截器实现,也可以通过框架实现(如:Apache Shiro、spring Security)。

本文将具体介绍在Spring Boot中如何使用Spring Security进行安全控制。
准备工作
首先,构建一个简单的Web工程,以用于后续添加安全控制,也可以用之前Chapter3-1-2 做为基础工程。若对如何使用Spring Boot构建Web应用,可以先阅读 《Spring Boot开发Web应用》 一文。
Web层实现请求映射
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
实现映射的页面
src/main/resources/templates/index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security入门</title>
</head>
<body>
<h1>欢迎使用Spring Security!</h1>
<p>点击 <a th:href="@{/hello}" rel="external nofollow" >这里</a> 打个招呼吧</p>
</body>
</html>
src/main/resources/templates/hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
可以看到在index.html中提供到 /hello 的链接,显然在这里没有任何安全控制,所以点击链接后就可以直接跳转到hello.html页面。
整合Spring Security
在这一节,我们将对 /hello 页面进行权限控制,必须是授权用户才能访问。当没有权限的用户访问后,跳转到登录页面。
添加依赖
在pom.xml中添加如下配置,引入对Spring Security的依赖。
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
...
</dependencies>
Spring Security配置
创建Spring Security的配置类 WebSecurityConfig ,具体如下:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
新增登录请求与页面
在完成了Spring Security配置之后,我们还缺少登录的相关内容。
HelloController中新增 /login 请求映射至 login.html
@Controller
public class HelloController {
// 省略之前的内容...
@RequestMapping("/login")
public String login() {
return "login";
}
}
新增登录页面: src/main/resources/templates/login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
用户名或密码错
</div>
<div th:if="${param.logout}">
您已注销成功
</div>
<form th:action="@{/login}" method="post">
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
可以看到,实现了一个简单的通过用户名和密码提交到 /login 的登录方式。
根据配置,Spring Security提供了一个过滤器来拦截请求并验证用户身份。如果用户身份认证失败,页面就重定向到 /login?error ,并且页面中会展现相应的错误信息。若用户想要注销登录,可以通过访问 /login?logout 请求,在完成注销之后,页面展现相应的成功消息。
到这里,我们启用应用,并访问 http://localhost:8080/ ,可以正常访问。但是访问 http://localhost:8080/hello 的时候被重定向到了 http://localhost:8080/login 页面,因为没有登录,用户没有访问权限,通过输入用户名user和密码password进行登录后,跳转到了Hello World页面,再也通过访问 http://localhost:8080/login?logout ,就可以完成注销操作。
为了让整个过程更完成,我们可以修改 hello.html ,让它输出一些内容,并提供“注销”的链接。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
</body>
</html>
本文通过一个最简单的示例完成了对Web应用的安全控制,Spring Security提供的功能还远不止于此,更多Spring Security的使用可参见 Spring Security Reference 。
完整示例: Chapter4-3-1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# spring
# boot
# security
# springboot
# security4
# 安全
# SpringBoot + Spring Security 基本使用及个性化登录配置详解
# 详解如何在spring boot中使用spring security防止CSRF攻击
# SpringBoot+Vue前后端分离
# 使用SpringSecurity完美处理权限问题的解决方法
# Spring Boot中使用 Spring Security 构建权限系统的示例代码
# 详解Spring Boot 使用Spring security 集成CAS
# Spring Boot(四)之使用JWT和Spring Security保护REST API
# Spring Security 在 Spring Boot 中的使用详解【集中式】
# 可以通过
# 就可以
# 转到
# 可以看到
# 如何使用
# 跳转到
# 访问权限
# 重定向
# 完成了
# 在这里
# 相关内容
# 在这
# 不需要
# 没有任何
# 可以用
# 其他的
# 我们可以
# 将对
# 重写
# 多种多样
相关文章:
宝塔面板创建网站无法访问?如何快速排查修复?
股票网站制作软件,网上股票怎么开户?
免费视频制作网站,更新又快又好的免费电影网站?
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
如何快速生成橙子建站落地页链接?
招贴海报怎么做,什么是海报招贴?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
如何制作算命网站,怎么注册算命网站?
简单实现Android验证码
如何选择PHP开源工具快速搭建网站?
Swift开发中switch语句值绑定模式
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
建站之星安装路径如何正确选择及配置?
宝塔建站后网页无法访问如何解决?
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
如何选择美橙互联多站合一建站方案?
自助网站制作软件,个人如何自助建网站?
如何获取上海专业网站定制建站电话?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?
Python文件管理规范_工程实践说明【指导】
如何用AWS免费套餐快速搭建高效网站?
建站之星后台密码遗忘如何找回?
建站主机如何选?高性价比方案全解析
如何通过智能用户系统一键生成高效建站方案?
python的本地网站制作,如何创建本地站点?
一键网站制作软件,义乌购一件代发流程?
建站主机是否属于云主机类型?
建站之星导航如何优化提升用户体验?
建站之星在线客服如何快速接入解答?
建站之星后台密码遗忘或太弱?如何重置与强化?
广东专业制作网站有哪些,广东省能源集团有限公司官网?
建站之星安全性能如何?防护体系能否抵御黑客入侵?
制作国外网站的软件,国外有哪些比较优质的网站推荐?
网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?
网站制作公司哪里好做,成都网站制作公司哪家做得比较好,更正规?
公司网站制作费用多少,为公司建立一个网站需要哪些费用?
网页设计网站制作软件,microsoft office哪个可以创建网页?
,网页ppt怎么弄成自己的ppt?
网站制作价目表怎么做,珍爱网婚介费用多少?
如何实现建站之星域名转发设置?
如何在局域网内绑定自建网站域名?
潮流网站制作头像软件下载,适合母子的网名有哪些?
Python路径拼接规范_跨平台处理说明【指导】
如何通过WDCP绑定主域名及创建子域名站点?
高性能网站服务器部署指南:稳定运行与安全配置优化方案
专业网站制作服务公司,有哪些网站可以免费发布招聘信息?
简历在线制作网站免费,免费下载个人简历的网站是哪些?
建站中国官网:模板定制+SEO优化+建站流程一站式指南
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
*请认真填写需求信息,我们会在24小时内与您取得联系。