SpringSecurity安全框架
SpringSecurity也是Spring框架中的一员,他可以很好的跟SpringBoot合作,主要用来对用户登录的认证和授权。所谓认证也就是哪些用户可以登录,登录之后扮演哪些角色,在同一个用户下扮演不同的角色(不管扮演哪种角色,该用户还是该用户),例如在root用户下,可以有vip1、vip2和vip3三个角色,当然也可以在hzy用户下有vip1这一个角色,都可以自定义。 所谓授权,也就是控制不同的角色可以访问不同的内容,例如我们想要vip1可以访问level1下的内容,vip2可以访问level2下的内容,vip3可以访问level3下的内容,所以我们的root用户就可以访问level1、level2和level3下的内容,因为他扮演着这三种角色,而hzy只能访问level1下的内容,因为他只扮演了vip1这一个角色(跟现实中的我们很像,我们在不同的场合扮演着不同的角色,但是不管扮演哪种角色,我还是我!)
对上面的描述,我们给出对应的配置类
package com.hzy.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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {// 认证// 密码编码 BCryptPasswordEncoder@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {// 这些数据应该从数据库中读取auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) // 设置密码加密.withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1","vip2","vip3").and().withUser("hzy").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");}// 授权@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/*").hasRole("vip1").antMatchers("/level2/*").hasRole("vip2").antMatchers("/level3/*").hasRole("vip3");// 没有权限会默认到登录页面,定制登录页http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin");// 开启注销功能,跳到首页http.logout().logoutSuccessUrl("/");// 开启记住我功能,自定义接受前端参数http.rememberMe().rememberMeParameter("remember");}
}
之所以对密码进行了加密是因为不加密会报下面这个错
在上面的配置类中,授权的时候,对登录和登出进行了操作,对登录进行了账号和密码的获取以及页面的指定跳转,对登出进行了页面的指定跳转,前端核心代码如下
<div class="right menu"><!--未登录--><a class="item" th:href="@{/toLogin}"><i class="address card icon"></i> 登录</a><!--注销--><a class="item" th:href="@{/logout}"><i class="sign-out icon"></i> 注销</a></div>
另外在授权配置类中,还对登录页面的记住我进行了操作,其核心前端代码如下
<input type="checkbox" name="remember"> 记住我
SpringSecurity安全框架相关推荐
- Spring Boot 框架学习笔记(五)( SpringSecurity安全框架 )
Spring Boot 框架学习笔记(五) SpringSecurity安全框架 概述 作用 开发示例: 1. 新建项目 2. 引入依赖 3. 编写`SecurityConfig`类,实现认证,授权, ...
- SpringSecurity 安全框架
SpringSecurity 安全框架 入门案例 创建基于maven环境的springboot2项目.并引入相关的依赖. <dependency><groupId>org.sp ...
- 根据Spring-Security安全框架搭建问答论坛系统(更新中.....)
论坛问答系统系统设计与实现 什么是Spring安全框架 为什么需要Spring-Security 启动Spring-Security 访问控制器方法 密码加密 Spring-Security的权限管理 ...
- SpringSecurity安全框架学习心得,避免踩坑。
最近有一个项目需要用到SpringSecurity安全框架,特意简单学习了一下,中间也踩了很多坑,在这里记录一下避免以后踩坑. 1.引入: 在很多的项目中,都会遇到认证问题,就是管理员与用户等不同角色 ...
- 一篇文章轻松搞定SpringSecurity权限框架!
目录 前言 一.引入依赖 二.提供正常的业务接口 三.自定义用户认证 3.1 编写配置类 3.2 编写UserDetailsService实现类 3.3 启动项目,完成认证功能的验证 3.4 小说明 ...
- springboot整合springsecurity安全框架(后端spring_security模块代码可直接使用,根据需求自定义修改)
SpringSecurity简介 最下面有与springboot整合的模块代码 用户认证和用户授权 主要包含两部分:用户认证和用户授权 用户认证:进入用户登录时候,输入用户名密码,查询数据库查看是否正 ...
- SpringSecurity安全框架简介
框架介绍 Spring Security是Spring全家桶的成员,官方对它的介绍是: Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架. 它是保护基于 Spring ...
- SpringSecurity权限框架实战
pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- 使用SpringBoot框架和SpringSecurity框架整合出现because its MIME type ('text/html') is not executable
前端页面出现: Refused to execute script from 'http://localhost:8080/login' because its MIME type ('text/ht ...
最新文章
- Linux下利用backtrace追踪函数调用堆栈以及定位段错误【转】
- 用OpenCV实现图像的水平镜像(翻转)变换和竖直镜像(翻转)变换(垂直镜像变换)的源码
- gui显示文本动态框
- 基于网络索引树的异常轨迹检测算法
- Ext.data.GroupingStore详解
- 变位齿轮重合度计算公式_渐开线圆柱齿轮传动的重合度计算.pdf
- OpenSSL 1.1.1 新特性: 全面支持国密SM2/SM3/SM4加密算法
- OSError: [WinError 126] 找不到指定的模块。【专治疑难杂症】
- 关于websocket长链接的使用
- Java网络编程学习汇总
- 最新护网/重保网络安全应急演练方案[精华]
- scheme 语言概述
- 中国超级计算机之父 孟,晚霞未必逊晨曦——记“中国巨型计算机之父”金怡濂院士...
- 计算机控制实验总结电机调速,控制步进电机调速系统实验报告
- JAVA将上传的PPT/PPTX转为图片
- Pycharm Debugger - Frames Not Available
- 在python中怎么定义一个范围_如何定义范围
- 无线蓝牙耳机哪款性价比最高?2022蓝牙耳机品牌排行榜前十名
- clickhouse源码:函数分析和自定义函数UDF
- Unity 第三人称 摄像机