搭建环境:

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. mysql中exists的用法简答

    前言在日常开发中,用mysql进行查询的时候,有一个比较少见的关键词exists,我们今天来学习了解一下这个exists这个sql关键词的用法,这样在工作中遇到一些特定的业务场景就可以有更加多样化的解 ...

  2. 机器学习(二):感知机+svm习题 感知机手工推导参数更新 svm手推求解二维坐标超平面直线方程

    作业1: 输入: 训练数据集 \(T = {(x1; y1); (x2; y2),..., (xN; yN)}\) 其中,\(x \in R^n\), \(y \in Y = \{+1, -1\}\) ...

  3. 熹乐科技范维肖CC:基于开源 YoMo 框架构建“全球同服”的 Realtime Metaverse Application

    前言 在「RTE2022 实时互联网大会」中,熹乐科技创始人 & CEO @范维肖CC 以<基于开源 YoMo 框架构建"全球同服"的 Realtime Metave ...

  4. 微软开源了一个 助力开发LLM 加持的应用的 工具包 semantic-kernel

    在首席执行官萨蒂亚·纳德拉(Satya Nadella)的支持下,微软似乎正在迅速转变为一家以人工智能为中心的公司.最近微软的众多产品线都采用GPT-4加持,从Microsoft 365等商业产品到& ...

  5. day96:flask:flask-migrate&flask-session&蓝图Blueprint&蓝图的运行机制&基于flask仿照django进行项目架构

    目录 1.flask-migrate 2.flask-session 3.蓝图:Blueprint 4.蓝图的运行机制 5.基于flask仿照django进行项目架构 1.准备工作 2.加载配置文件 ...

  6. python:模拟购票的小程序

    问题描述:小白学习python的第N天,继续练习.做一个模拟购票的小程序,没有用数据库和文件来存储数据,只是能够单词选择. # hzh 每天进步一点点 # 2022/5/13 17:24 import ...

  7. PHP的WAMP的安装

    WAMP独立安装 软件官网下载: Apache:http://httpd.apache.org/download.cgi MySQL:http://dev.mysql.com/downloads/ P ...

  8. numpy常用的操作

    以下是NumPy中一些常用的操作及其相应的代码示例: 创建NumPy数组: import numpy as np # 从Python列表创建一维数组 a = np.array([1, 2, 3, 4, ...

  9. 帝国ECMS静态生成为一行代码/静态页面打乱教程

    一.内容页变成一行修改1.打开文件e/class/functions.php2.找到以下函数 function GetHtml($classid,$id,$add,$ecms=0,$doall=0) ...

  10. 在Vue中使用键盘事件,回调函数被执行两次

    暂时先不考虑v-for的key,查看下面的代码 <template> <div> <form @submit.prevent="addTask"> ...