搭建环境:

1.在创建springboot时选择组件web,thymeleaf,spring-security

2.导入静态资源,导入后测试一下环境

 认证和授权

继承类WebSecurityConfigurerAdapter,重写方法configure

若遇到报错:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:289) ~[spring-security-crypto-5.7.8.jar:5.7.8]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:237) ~[spring-security-crypto-5.7.8.jar:5.7.8]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$LazyPasswordEncoder.matches(WebSecurityConfigurerAdapter.java:616) ~[spring-security-config-5.7.8.jar:5.7.8]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:77) ~[spring-security-core-5.7.8.jar:5.7.8]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:147) ~[spring-security-core-5.7.8.jar:5.7.8]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182) ~[spring-security-core-5.7.8.jar:5.7.8]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:201) ~[spring-security-core-5.7.8.jar:5.7.8]
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(

是因为设置的密码没有加密啊,springsecurity5.0+要求对密码加密,因为不加密很容易被反编译,造成信息泄露

代码:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
//首页all可以访问,功能页对应vip能访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认跳到登录页面,默认开启登录的页面
http.formLogin(); }
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
.and()
.withUser("ROOT").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("GUEST").password(new BCryptPasswordEncoder().encode("12")).roles("vip3");
}
}

注销

要求实现:未登录:只显示登录按钮,登录时 显示登陆角色和注销按钮

前端代码要做的事情是验证,需要导入整合thymeleaf和springsecurity的依赖

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

前端代码:

前端判断:如果用户未登录(!isAuthenticated()),那要跳到登录页面,如果用户是登录的,定制首页要展示出用户的角色(vip1?vip2?vip3?),和注销图标

运行结果:

动态菜单

动态菜单:不同角色登录看到的内容不同

前端代码:

只需要在每个功能模块前面添加判断是什么角色

运行结果:

可以看到用户kuangshen角色是vip2和vip3,因此可以看到对应的两个功能模块Level2和Level3

 记住我

记住我的本质是在用户访问时增加cookie

在前端添加:

在secruityconfig开启:

首页定制

我们不想继续使用springsecurity5提供的登录页面了,因此我们自定义了一个login.html,我们希望之后按登录按钮时跳转到自己定制的登录页面

在RouterController中,"views/login"对应的是/toLogin,在index.html页面请求的/toLogin经过这条语句会跳转到“views/login",在login.html面请求的/toLogin经过这条语句表单提交到index.html; 因此这里的/toLogin和index.html的/toLogin和login。html的/toLogin要完全一致,不然会报404

login.html

index.html

springsecurity 认证,授权,注销,动态菜单,记住我和首页定制的更多相关文章

  1. 【NET CORE微服务一条龙应用】第三章 认证授权与动态权限配置

    介绍 系列目录:[NET CORE微服务一条龙应用]开始篇与目录 在微服务的应用中,统一的认证授权是必不可少的组件,本文将介绍微服务中网关和子服务如何使用统一的权限认证 主要介绍内容为: 1.子服务如 ...

  2. Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案

    一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...

  3. 使用SpringSecurity搭建授权认证服务(1) -- 基本demo认证原理

    使用SpringSecurity搭建授权认证服务(1) -- 基本demo 登录认证是做后台开发的最基本的能力,初学就知道一个interceptor或者filter拦截所有请求,然后判断参数是否合理, ...

  4. 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据

    一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...

  5. SpringBoot--- 使用SpringSecurity进行授权认证

    SpringBoot--- 使用SpringSecurity进行授权认证 前言 在未接触 SpringSecurity .Shiro 等安全认证框架之前,如果有页面权限需求需要满足,通常可以用拦截器, ...

  6. SpringSecurity(1)---认证+授权代码实现

    认证+授权代码实现 Spring Security是 一种基于 Spring AOP 和 Servlet 过滤器的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(二十三):Gateway+OAuth2+JWT实现微服务统一认证授权

      OAuth2是一个关于授权的开放标准,核心思路是通过各类认证手段(具体什么手段OAuth2不关心)认证用户身份,并颁发token(令牌),使得第三方应用可以使用该token(令牌)在限定时间.限定 ...

  8. [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权

    一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...

  9. 【Spring Cloud & Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权

    一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证授权.鉴权的逻辑,结合 ...

  10. [认证授权] 3.基于OAuth2的认证(译)

    OAuth 2.0 规范定义了一个授权(delegation)协议,对于使用Web的应用程序和API在网络上传递授权决策非常有用.OAuth被用在各钟各样的应用程序中,包括提供用户认证的机制.这导致许 ...

随机推荐

  1. 17.explicit关键字

    c++提供了关键字explicit,禁止通过构造函数进行的隐式转换.声明为explicit的构造函数不能在隐式转换中使用. [explicit注意] ● explicit用于修饰构造函数,防止隐式转化 ...

  2. [C++STL教程]4.map超强的容器,它终于来了!零基础都能理解的入门教程

    之前我们介绍过vector, queue, stack,他们都有一个共同的特点,就是都可以用线性表来模拟.今天我们来学习一个全新且高封装性的容器:map. 作者:Eriktse 简介:19岁,211计 ...

  3. Windows的压缩文件夹(zip/cab)

    https://weibo.com/1114096665/DtHXgvnva #windows10# 硬要把zip.cab文件当文件夹,不爽怎么解决? 删除注册表 "HKEY_CLASSES ...

  4. Django笔记十五之in查询及date日期相关过滤操作

    这一篇介绍关于范围,日期的筛选 in range date year week weekday quarter hour 1.in in 对应于 MySQL 中的 in 操作,可以接受数组.元组等类型 ...

  5. 如何申请 Azure OpenAI

    一.前言 众所周知 OpenAI ChatGPT 是不对中国开放的,包括香港.就最近一个月的情况来看,陆续有 API 调用被限制.大规模账号封禁.关闭注册.无法直接使用银联支付(国内信用卡)等等,使用 ...

  6. 单机最快的队列Disruptor解析和使用

    前言 介绍高性能队列Disruptor原理以及使用例子. Disruptor是什么? Disruptor是外汇和加密货币交易所运营商 LMAX group 建立高性能的金融交易所的结果.用于解决生产者 ...

  7. TOP使用参数

    TOP使用参数top是检查机器当前运行状况的第一个命令,就好比是机器体检时的第一张报告单.先了解一下TOP命令的使用 [root@localhost /]# top -help top: procps ...

  8. python之opencv库

    关于OpenCV简介  OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效--由一系列 C ...

  9. day8:列表相关函数&深浅拷贝&字典相关函数&集合相关操作/函数

    字符串/列表/字典/集合 目录 字符串相关操作: 拼接 重复 跨行拼接 索引 切片字符串相关函数:常规11+is系列3+填充去除6+最重要3字符串拓展:字符串的格式化format 列表的相关操作:拼接 ...

  10. 【深度学习】【图像分类网络】(二)VisionTransformer

    Transformer简介 ![1png](file:///D:/资料/学习笔记/深度学习/图像分类/transformer/Self-Attention以及Multi-Head Attention/ ...