当前位置: 首页 > news >正文

SpringSecurity -- 入门使用

文章目录

  • 什么是 SpringSesurity ?
  • 细节
  • 使用方法

什么是 SpringSesurity ?

在我们的开发中,安全还是有些必要的
用 拦截器 和 过滤器 写代码还是比较麻烦。

SpringSecuritySpringBoot 的底层安全默认选型。一般我们需要认证和授权,这也是它的核心功能。我们仅仅需要导入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理

认证:验证当前访问系统的用户 是不是本系统的用户,并且要具体哪一个用户
授权:经过认证后判断当前用户是否有权限进行某个操作

细节

登录过程

在这里插入图片描述

在这里插入图片描述

负责我们在登录页面填写的用户和密码登录的请求,入门案例主要由他负责。

使用方法

总结到代码之中。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

// 开启 web 安全
@EnableWebSecurity
public class springSecurityConfig extends WebSecurityConfigurerAdapter { // 然后我们继承 web安全配置 适配器// 链式编程@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().  // authorize 授权antMatchers("/").permitAll()  //  ant ==> 蚂蚁    matcher ==> 匹配器 permitAll ==> 允许所有.antMatchers("/test1/**").hasRole("vip1")  //  role角色.antMatchers("/test2/**").hasRole("vip2").antMatchers("/test3/**").hasRole("vip3");// 如果没有用户的情况下,则会跳转到 login 页面       // 登录加工的urlhttp.formLogin().loginPage("/toLogin").usernameParameter("name").passwordParameter("pwd").loginProcessingUrl("/Login");//                 http.formLogin()
//                   .loginPage("/toLogin") // 设置登录页面URL
//                   .loginProcessingUrl("/Login") // 设置表单提交的URL 个人感觉:应该是需要认证的模块
//                   .failureUrl("/login-error") // 登录失败后的重定向URL
//                   .defaultSuccessUrl("/home", true) // 登录成功后的默认重定向URL
//                .and()
//                  .logout()
//                   .logoutSuccessUrl("/"); // 注销成功后的重定向URL// 开启记住我功能http.rememberMe().rememberMeParameter("jiZhuWo");http.csrf().disable();  // 关闭脚本跨站攻击// 开启注销功能http.logout().logoutSuccessUrl("/index");  // 注销成功后,回到 “/index” 之中。}// 认证@Override // 最新版本需要密码加密protected void configure(AuthenticationManagerBuilder auth) throws Exception {  // Authentication 认证 Manager 管理者auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) // Memory 记忆.withUser("xj").password(new BCryptPasswordEncoder().encode("abc")).roles("vip1","vip2")  // memory ==> 记忆.and().withUser("test").password(new BCryptPasswordEncoder().encode("cba")).roles("vip3");}
}

我们可以看到,拥有这个功能,不仅提供了权限、安全等,还另外 省去了写登录后台、过滤器等大量繁杂的代码,十分的方便

http://www.lryc.cn/news/447896.html

相关文章:

  • C语言习题~day33
  • 作业报告┭┮﹏┭┮(Android反调试)
  • 在 Delphi BSD11中安装 DCU 格式的第三方组件库
  • 综合题第二题(路由器的配置)
  • 人工智能概览
  • [vulnhub] Prime 1
  • JavaSE——lombok、juint单元测试、断言
  • 商标价值如何评估与增值?
  • linux命令之firewall-cmd用法
  • 深入浅出CSS盒子模型
  • 字符编码发展史4 — Unicode与UTF-8
  • 【flink】之如何消费kafka数据并读写入redis?
  • 搜索引擎onesearch3实现解释和升级到Elasticsearch v8系列(二)-索引
  • 离散化算法
  • 基于ollama的本地RAG实践
  • 安卓开发板_MTK开发板_联发科开发评估套件Demo板接口介绍
  • 代码随想录冲冲冲 Day58 图论Part9
  • UnityHub下载任意版本的Unity包
  • 网站服务器怎么计算同时在线人数?
  • [spring]MyBatis介绍 及 用MyBatis注解操作简单数据库
  • Ks渲染做汽车动画吗?汽车本地渲染与云渲染成本分析
  • AI智能时代:哪款编程工具让你的工作效率翻倍?
  • 这五本大模型书籍,让你从大模型零基础到精通,非常详细收藏我这一篇就够了
  • 面试经典150题 堆
  • day-62 每种字符至少取 K 个
  • 免费好用!AI声音克隆神器,超级简单,10秒就能克隆任何声音!(附保姆级教程)
  • LeetCode146 LRU缓存
  • 【Java】包装类【主线学习笔记】
  • 华为HarmonyOS地图服务 11 - 如何在地图上增加点注释?
  • uniapp js怎么根据map需要显示的点位,计算自适应的缩放scale