初识SpringSecurity
Spring Security
准备工作
- 创建一个 Web 工程,需要引入
spring-boot-starter-web
的包 - 引入
spring-boot-starter-security
的包 - 引入 thymeleaf 的包
spring-boot-starter-thymeleaf
(实测,如果不引入 thymeleaf,则无法从控制器请求到 html 视图)
Web 层实现请求映射
@Controller
public class HelloController {@RequestMapping("/")public String index() {return "index"; // 映射到 index.html}@RequestMapping("/hello")public String hello() {return "hello"; // 映射到 hello.html}}
复制代码
实现映射的页面
映射的页面统一放在 src/main/resources/teamplates
下
- index.html
在该页面中,通过链接直接请求到 hello 方法,并通过 hello 映射到 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>Spring Security入门</title></head><body><h1>欢迎使用Spring Security!</h1><p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p></body>
</html>
复制代码
- 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>
复制代码
整合 Spring Security
这里会对 /hello
请求进行权限控制,必须是授权用户才能访问。如果是未授权用户,则直接跳转到登录页面。
配置 Spring Security
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll() // "/"、"/home" 不需要被验证.anyRequest().authenticated() // 其他路径的请求都需要验证.and().formLogin().loginPage("/login") // 指定登录页面,谁都可以访问.permitAll().and().logout() // 登出页面不需要验证.permitAll();}@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}}
复制代码
- 通过 @EnableWebSecurity 注解开启 Spring Security
- 继承 WebSecurityConfigurerAdapter ,并通过重写方法来设置一些安全细节
- configure(HttpSecurity http) 方法
- 通过 authorizeRequests() 定义哪些URL需要被保护、哪些不需要被保护。
- 通过 formLogin() 定义当需要用户登录时候,转到的登录页面。并且通过 loginPage(String) 的方法指定登录页面
- configureGlobal(AuthenticationManagerBuilder auth) 方法,在内存中创建了一个用户,该用户的名称为 user,密码为 password,用户角色为 USER。
新增登录请求与页面
- 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
@Controller
public class HelloController {// 省略之前的内容...@RequestMapping("/login")public String login() {return "login";}}
复制代码
根据配置,Spring Security 提供了一个过滤器来拦截请求并验证用户身份。
如果用户身份验证失败,页面就会重定向到 /login?error
,并且页面中会展现相应的错误信息。
若用户想要注销登录,可以通过访问 /login?logout
请求,在完成注销之后,页面展现相应的成功消息。
因为 /hello
请求需要验证用户信息,因此在未登录状态下被重定向到 /login
方法中,通过用户名与密码登录之后,就会跳转到 /hello
页面。
Spring Security 官方文档 ,本文来源'程序员DD'的博客
转载于:https://juejin.im/post/5b7d2e5d6fb9a019e97677d2
初识SpringSecurity相关推荐
- SpringSecurity相关jar包的介绍
初识SpringSecurity Spring Security是spring采用AOP思想,基于servlet过滤器实现的安全框架.它提供了完善的认证机制和方法级的 授权功能.是一款非常优秀的权限管 ...
- SpringBoot集成SpringSecurity(二) 个性化登录配置(remember-me mongodb)
前言 本文件所记录的是使用SpringSecurity实现remember me功能,有兴趣的朋友可以继续阅读,有何不足之处还请各位指出(本文未对用户 - 角色 - 权限三者的关系进行详细介绍详情见 ...
- SpringSecurity系列(一) 初识 Spring Security
1. 写在前面 Java 领域老牌的权限管理框架当属 Shiro 了,Shiro 有着众多的优点,例如:轻量.简单.易于集成等.当然 Shiro 也有不足,例如对 OAuth2 支持不够,在 Spri ...
- SpringSecurity系列(四) Spring Security 实现权限树形菜单
SpringSecurity系列(一) 初识 Spring Security SpringSecurity系列(二) Spring Security入门 SpringSecurity系列(三) Spr ...
- SpringSecurity系列(三) Spring Security 表单登录
SpringSecurity系列(一) 初识 Spring Security SpringSecurity系列(二) Spring Security入门 1. 服务端 1.1 依赖 <?xml ...
- 十年阿里巴巴资深架构师整理分享的SpringSecurity实战文档
前言 SpringSecurity是一个强大且高度可定制的安全框架,致力于为Java应用提供身份认证和授权. Spring Security 的前身是 Acegi Security,在被收纳为Spri ...
- SpringSecurity超详细入门介绍
权限管理是我们项目中必不可少的一环,实际项目中我们可以自己设计权限管理模块,也可以使用市面上成熟的权限管理框架,比如 shiro或者 SpringSecurity等,前面已经详细的介绍过了 shi ...
- SpringBoot整合SpringSecurity
目录 1.spring security初识 (1)spring security简介 (2)认证与授权的概念 2.测试环境搭建 2.1 准备html页面 2.2 准备controller 3.sp ...
- SpringSecurity系列——密码存储加密策略day7-1(源于官网5.7.2版本)
SpringSecurity系列--密码存储加密策略day7-1(源于官网5.7.2版本) Password Storage(密码存储) 密码存储策略历史 委托密码编码器 创建默认 Delegatin ...
最新文章
- 32页,10米长的《BI建设地图》强在哪?我整理分享出来
- pg数据库有雷锋?用户已有权限为何无故消失?
- ORACLE中的自治事务
- 高级语言程序设计C试卷答案,2018高级语言程序设计考卷B及参考答案.docx
- Mac 切换仓库地址后每次都要重新输入密码
- 14届数独-真题标准数独-Day 8-20220123
- 1 如何制定购车计划
- Android——实时显示系统时间
- 华为HCNA学习笔记----第一天
- 原生实现freeCodeCamp上的Build a Personal Portfolio Webpage
- itunes store服务中断_终于明白为什么那么多人吐槽apple的态度了.临时工一样的中国区itunes store客服......
- spyder汉化方法
- Axure RP9基本操作
- 【小结】南京大学软件工程专硕2021二战小结
- 基于OpenCV的人脸 考勤系统
- Xilinx-Z7K7启动模式
- android手机必备app,5款安卓手机必备的黑科技APP!各个都是精品,一定要低调收藏使用...
- Jersey 的使用详解
- 德国的吃--一篇很有意思的文章
- CockRoachDB配置跨域集群
热门文章
- 计算机新增桌面的路径,自动更改Windows新建用户桌面文件默认路径修改
- Mac笔记本Postman升级为新版本后,打开时一直转圈,无法使用
- mysql连接量设置_mysql连接数设置操作方法(Too many connections)
- node.js超过php,在nodejs中如何解决超出最大的调用栈错误
- SMPL: A Skinned Multi-Person Linear Model
- lesson 2.4 - Converting MEL Commands to Python
- python语言接收信息的内置函数_python接收信息的内置函数是
- 计算机系统 cpu课程,计算机操作系统
- 计算机主机名在哪里看win7,大师教您win7系统查看win7系统电脑主机名的图文步骤...
- 【新书】python+tensorflow机器学习实战,详解19种机器学习经典算法