一、引入主要pom依赖

  <!--根据不同的用户展现不同的模块--><!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 --><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId><version>3.0.4.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

二、编写springsecurity的配置类 编辑MySecurityConfig继承WebSecurityConfigurerAdapter类 加入@EnableWebSecurity注解 //开启web的安全模式

@EnableWebSecurity    //开启web的安全模式
public class MySecurityConfig extends WebSecurityConfigurerAdapter {}

三、授权规则 控制请求访问权限和开启自动配置的登录功能 如果没有权限就会来到登录页面
重写configure(HttpSecurity http)方法

 @Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//定制请求的授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登录功能  如果没有权限就会来到登录页面http.formLogin();}

四、 定义认证规则 重写configure(AuthenticationManagerBuilder auth)方法
(在此我没有连建数据库)
首先说明一下Spring Security 官方推荐的是使用 BCrypt 加密方式。在这里我不得不说一下 MD5 加密现在已经弱爆了,目前 最新版的 Spring Security 中已经把 MD5 剔除了,MD5 太不安全了,更推荐用 BCrypt 加密,而且什么盐值加密也很少用,因为 BCrypt 中已经将 salt 加进去了。

    @Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("张三").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP2").and().passwordEncoder(new BCryptPasswordEncoder()).withUser("李四").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2","VIP3").and().passwordEncoder(new BCryptPasswordEncoder()).withUser("王五").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP3");}

五、注销功能
在configure(HttpSecurity http)方法中加入
http.logout().logoutSuccessUrl("/");//注销成功回到首页

//开启自动配置的注销功
//http.logout();
//1.访问/logout表示用户注销,清空session
//2.注销成功会返回/login?logout页面 如果不想返回此页面可以改为如下配置
http.logout().logoutSuccessUrl("/");//注销成功回到首页

六、开启记住我功能
在configure(HttpSecurity http)方法中加入

http.rememberMe();//登录成功后将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆//点击注销会删除cookie

七、根据登没登录展示不同的页面
引入的pom是(第一步里面已经有了再次再提示一下)

 <!--根据不同的用户展现不同的模块--><!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 --><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId><version>3.0.4.RELEASE</version></dependency>

在要进行此功能的页面引入

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/extras/spring-security">  <!-- 主要是这行代码 官方建议使用 --><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<div sec:authorize="!isAuthenticated()"><h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
<div sec:authorize="isAuthenticated()"><h2> <span sec:authentication="name"></span>,您好,您的角色有:<span sec:authentication="principal.authorities"></span> </h2><form th:action="@{/logout}" method="post"><input type="submit" value="注销"></form>
</div>

八、根据不同的人的权限展示不同的页面
sec:authorize=“hasRole(‘VIP1’)”

<div sec:authorize="hasRole('VIP1')"><h3>普通武功秘籍</h3><ul><li><a th:href="@{/level1/1}">罗汉拳</a></li><li><a th:href="@{/level1/2}">武当长拳</a></li><li><a th:href="@{/level1/3}">全真剑法</a></li></ul>
</div><div sec:authorize="hasRole('VIP2')"><h3>高级武功秘籍</h3><ul><li><a th:href="@{/level2/1}">太极拳</a></li><li><a th:href="@{/level2/2}">七伤拳</a></li><li><a th:href="@{/level2/3}">梯云纵</a></li></ul>
</div><div sec:authorize="hasRole('VIP3')"><h3>绝世武功秘籍</h3><ul><li><a th:href="@{/level3/1}">葵花宝典</a></li><li><a th:href="@{/level3/2}">龟派气功</a></li><li><a th:href="@{/level3/3}">独孤九剑</a></li></ul>
</div>

九、自定义登录页面
在在configure(HttpSecurity http)方法中加入

http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");

登录页面如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1 align="center">欢迎登陆武林秘籍管理系统</h1><hr><div align="center"><form th:action="@{/userlogin}" method="post">用户名:<input name="user"/><br>密码:<input name="pwd"><br/><input type="checkbox" name="remeber">记住我<br/><input type="submit" value="登陆"></form></div>
</body>
</html>

具体config如下

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@EnableWebSecurity    //开启web的安全模式
public class MySecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//定制请求的授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登录功能  如果没有权限就会来到登录页面// http.formLogin();//1./login来到登录页//2.重定向到/login?error//3.//回到自己的登录页//http.formLogin().loginPage("/userlogin");//4.默认post形式的/login请求代表处理登录http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");////开启自动配置的注销功能//http.logout();//1.访问/logout表示用户注销,清空session//2.注销成功会返回/login?logout页面 如果不想返回此页面可以改为如下配置http.logout().logoutSuccessUrl("/");//注销成功回到首页//开启记住我功能http.rememberMe();//登录成功后将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆//点击注销会删除cookie}
//定义认证规则Spring Security 官方推荐的是使用 BCrypt 加密方式。
// 在这里我不得不说一下 MD5 加密现在已经弱爆了,目前 最新版的 Spring Security 中已经把 MD5 剔除了,MD5 太不安全了,
// 更推荐用 BCrypt 加密,而且什么盐值加密也很少用,因为 BCrypt 中已经将 salt 加进去了。
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {//       // super.configure(auth);
//        auth.inMemoryAuthentication()
//                .withUser("zhangsan").password("123456").roles("VIP1","VIP2")
//                .and()
//                .withUser("lisi").password("123456").roles("VIP2","VIP3")
//                .and()
//                .withUser("wangwu").password("123456").roles("VIP1","VIP3");
//    }//定义认证规则@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("张三").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP2").and().passwordEncoder(new BCryptPasswordEncoder()).withUser("李四").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2","VIP3").and().passwordEncoder(new BCryptPasswordEncoder()).withUser("王五").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP3");}}

springboot2.3.3+springsecurity相关推荐

  1. SpringBoot2.0 整合 SpringSecurity 框架,实现用户权限安全管理

    一.Security简介 1.基础概念 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配 ...

  2. springBoot+thymeleaf框架实现cms内容管理和商城系统

    springBoot+thymeleaf框架实现cms内容管理和商城系统 概要介绍 基于开源框架springBoot2.3+thymeleaf+springSecurity+lucene8.5.2 是 ...

  3. PUSHmall 推贴订货商城系统 — — B2B/B2C批发零售采销模式,商贸流通企业最佳电商解决方案

    PUSHmall推贴 订货商城系统:B2B/B2C兼营商城系统 基于当前流行技术组合的前后端分离批零订货商城系统: SpringBoot2+SpringBoot Jpa+SpringSecurity+ ...

  4. 电商数字化解决方案趋势——订货商城系统+进销存财务系统+CRM客户管理系统

    PUSHmall 推贴数字化电商解决方案设计: 订货商城(线下线上交易)+进销存财务(管理支撑层面)+CRM客户管理(业务拓客推广) 所谓的数字化电商解决方案的设计不是一个功能模块的表现,而是重视渠道 ...

  5. 青锋开源架构-springboot2.6.x+vue3-antdesign-vite之springsecurity实现访问权限控制

    框架开源地址: 青锋开源架构-springboot2.6.x+vue3-antdesign-vite: 青锋-springboot2.6.x+vue3-antdesign-vite开源架构,实现了系统 ...

  6. springboot2.4跨域配置的方法

    这篇文章主要介绍了springboot2.4跨域配置的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 1.如果只是一个简单的springbo ...

  7. echarts前后端交互数据_SpringBoot2.0实战(26)整合SpringSecurity前后端分离JSON交互...

    在前端的文章中,我们实现了使用 SpringSecurity 实现登录鉴权,并使用数据库存储用户信息,实现登录鉴权 SpringBoot2.0实战(24)整合SpringSecurity之最简登录方法 ...

  8. 这是一篇优雅的Springboot2.0使用手册

    最近再研究springboot的原理?颇有收获,现在让我分享一下springboot如何使用吧~ 想要解锁更多新姿势?请访问我的博客 啥是Springboot 和书上理解的不同,我认为Springbo ...

  9. 基于 SpringBoot2 + MybatisPlus 的商城管理系统(附源码)

    项目源码 推荐 7 个牛哄哄 Spring Cloud 实战项目 推荐一个非常火爆的电商开源项目! 推荐两个项目! 重磅推荐:一套开源的网校系统,无论是自建网校还是接副业都很方便 推荐几个支付项目! ...

最新文章

  1. Entity Freamwork 6连接PostgreSql数据库
  2. 用java解决国王的金币问题_国王赏赐金币问题(减治法)
  3. Gartner:到2020年人工智能将创造出230万个工作岗位
  4. JAVA程序设计----多线程(下)
  5. VS2013+VSVIM
  6. Apache Tika源码研究(七)
  7. 计算机可用内存分配失败,你们都被忽悠了! 其实可用内存大才有用
  8. python 字符串删除重复_leetcode No.1047 删除字符串中的所有相邻重复项
  9. 网卡流量监控工具vnstat的使用
  10. Python实现恋爱AA公式
  11. 数据分析学习笔记—python_word处理及邮件发送
  12. asp.net mvc mysql 开源项目_【开源项目SugarSite】ASP.NET MVC+ Layui+ SqlSugar+RestSharp项目讲解...
  13. 关于母板页中runnat=server 窗体标记的问题
  14. visio业务流程图教学_Visio流程图入门
  15. UniWebView插件的使用
  16. (大数据方向)分布式实验七:HBase数据库搭建以及常用命令
  17. 关于如何修复任务栏图标变白色的问题
  18. Word 分节符插入与删除方法
  19. boost::asio::io_service的stop()和reset()和stopped()函数
  20. python调用PHP方法

热门文章

  1. 设置火狐浏览器firefox模拟微信浏览器客户端,调试网站
  2. 李开复:长尾效应带给媒体的不是威胁
  3. C# 在图片上写字
  4. Element ui Dialog 对话框遮罩层挡住对话框问题
  5. 【厚积薄发系列】C++项目总结17—《WHY C++ ? 王者归来》读后感
  6. MATLAB调用python获得股票数据并构建交易策略
  7. 自动化测试框架(从robotframework到hyrobot(黑羽robot) python语言)
  8. ios java语言_iOS平台的App主要使用哪种语言进行开发? Java|C++|Python|Swift
  9. 新闻管理的删除功能+新闻首页+详情页
  10. 一文带你彻底弄懂ES中的doc_values和fielddata