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

springboot综合案例第三课

SpringSecurity入门

什么是SpringSecurity

Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。

(https://projects.spring.io/spring-security/) Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别

是使用领先的J2EE解决方案-Spring框架开发的企业软件项目。人们使用Spring Security有很多种原因,不过通常吸

引他们的是在J2EE Servlet规范或EJB规范中找不到典型企业应用场景的解决方案。特别要指出的是他们不能再

WAR 或 EAR 级别进行移植。这样,如果你更换服务器环境,就要,在新的目标环境进行大量的工作,对你的应用

系统进行重新配置安全。使用Spring Security 解决了这些问题,也为你提供很多有用的,完全可以指定的其他安

全特性。安全包括两个主要操作。

  • “认证”,是为用户建立一个他所声明的主体。主题一般式指用户,设备或可以在你系统中执行动作的其他系

统。(简单来说:系统认为用户是否能登录)

  • “授权”,指的是一个用户能否在你的应用中执行某个操作,在到达授权判断之前,身份的主题已经由身份验证

过程建立了。(简单来说:系统判断用户是否有权限去做某些事情)

这些概念是通用的,不是Spring Security特有的。在身份验证层面,Spring Security广泛支持各种身份验证模式,

这些验证模型绝大多数都由第三方提供,或则正在开发的有关标准机构提供的,例如 Internet Engineering Task

Force.作为补充,Spring Security 也提供了自己的一套验证功能。

Spring Security 目前支持认证一体化如下认证技术: HTTP BASIC authentication headers (一个基于IEFT RFC 的

标准) HTTP Digest authentication headers (一个基于IEFT RFC 的标准) HTTP X.509 client certi?cate exchange

(一个基于IEFT RFC 的标准) LDAP (一个非常常见的跨平台认证需要做法,特别是在大环境) Form-based

authentication (提供简单用户接口的需求) OpenID authentication Computer Associates Siteminder JA-SIG

Central Authentication Service (CAS,这是一个流行的开源单点登录系统) Transparent authentication context

propagation for Remote Method Invocation and HttpInvoker (一个Spring远程调用协议)

用户登录认证

表结构分析与创建

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GnUk2Dvg-1692278828416)(day03_springboot综合案例.assets/image-20210516220645141.png)]

-- 用户表
CREATE TABLE users(id VARCHAR(32) PRIMARY KEY,email VARCHAR(50) UNIQUE NOT NULL,username VARCHAR(50),PASSWORD VARCHAR(100),phoneNum VARCHAR(20),STATUS INT
);-- 角色表
CREATE TABLE role(id VARCHAR(32) PRIMARY KEY,roleName VARCHAR(50) ,roleDesc VARCHAR(50)
);-- 用户角色关联表
CREATE TABLE users_role(userId VARCHAR(32),roleId VARCHAR(32),PRIMARY KEY(userId,roleId),FOREIGN KEY (userId) REFERENCES users(id),FOREIGN KEY (roleId) REFERENCES role(id)
);-- 资源权限表
CREATE TABLE permission(id VARCHAR(32)  PRIMARY KEY,permissionName VARCHAR(50) ,url VARCHAR(50)
);-- 角色权限关联表
CREATE TABLE role_permission(permissionId VARCHAR(32),roleId VARCHAR(32),PRIMARY KEY(permissionId,roleId),FOREIGN KEY (permissionId) REFERENCES permission(id),FOREIGN KEY (roleId) REFERENCES role(id)
);

创建类

创建UserInfo


@Data
public class UserInfo {private String id;private String username;private String email;private String password;private String phoneNum;private int status;private String statusStr;private List<Role> roles;
}

创建Role


@Data
public class Role {private String id;private String roleName;private String roleDesc;private List<Permission> permissions;private List<UserInfo> users;
}

创建Permission


@Data
public class Permission {private String id;private String permissionName;private String url;private List<Role> roles;
}

Spring Security数据库认证底层

在Spring Security中如果想要使用数据进行认证操作,有很多种操作方式,这里我们介绍使用UserDetails、

UserDetailsService来完成操作。

  • UserDetails

    public interface UserDetails extends Serializable {Collection<? extends GrantedAuthority> getAuthorities();String getPassword();String getUsername();boolean isAccountNonExpired();boolean isAccountNonLocked();boolean isCredentialsNonExpired();boolean isEnabled();
    }
    

    UserDatails是一个接口,我们可以认为UserDetails作用是于封装当前进行认证的用户信息,但由于其是一个

    接口,所以我们可以对其进行实现,也可以使用Spring Security提供的一个UserDetails的实现类User来完成

    操作,Ctrl+Alt+B 查找接口实现类

    以下是User类的部分代码

    public class User implements UserDetails, CredentialsContainer {private String password;private final String username;private final Set<GrantedAuthority> authorities;private final boolean accountNonExpired; //帐户是否过期private final boolean accountNonLocked; //帐户是否锁定private final boolean credentialsNonExpired; //认证是否过期 private final boolean enabled; //帐户是否可用 
  • UserDetailsService

    public interface UserDetailsService {UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
    }
    

    上面将UserDetails与UserDetailsService做了一个简单的介绍,那么我们具体如何完成Spring Security的数据库认

    证操作,我们通过用户管理中用户登录来完成Spring Security的认证操作。

用户登录流程分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KG7Pu1wO-1692278828417)(assets/image-20201012180237425.png)]

登录认证

添加依赖

    <!--SpringSecurity--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

Spring Security配置类

package cn.yanqi.config;import cn.yanqi.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.annotation.Resource;// @EnableGlobalMethodSecurity(jsr250Enabled = true)   //开启jsr250注解
// @EnableGlobalMethodSecurity(securedEnabled = true)  //开启secured注解
// @EnableGlobalMethodSecurity(prePostEnabled = true)  //开启表达式注解
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Resourceprivate UserService userService;@Overrideprotected void configure(HttpSecurity http) throws Exception {//自定义表单登录页面http.formLogin()//指定登录页面.loginPage("/to/login")//指定登录请求.loginProcessingUrl("/login").usernameParameter("username").passwordParameter("password").successForwardUrl("/to/index").failureUrl("/to/failer").and().logout().logoutUrl("/logout").logoutSuccessUrl("/to/login").invalidateHttpSession(true) //是否清除session.and()//权限配置.authorizeRequests()//放行 登录页面.antMatchers("/to/login","/to/failer").permitAll()//放开 静态资源.antMatchers("/css/**","/img/**","/js/**","/plugins/**").permitAll()//其他 资源需要登录后访问.anyRequest().authenticated().and()//禁用csrf.csrf().disable();//没有权限http.exceptionHandling().accessDeniedPage("/to/403");}//认证的数据需要使用自定义的UserDetailsService@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userService).passwordEncoder(passwordEncoder());}@Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}
}

编写UserService

package cn.yanqi.service;import org.springframework.security.core.userdetails.UserDetailsService;
//继承 UserDetailsService 重写loadUserByUsername 完成认证
public interface UserService extends UserDetailsService {}

import cn.yanqi.travel.mapper.UserMapper;
import cn.yanqi.travel.pojo.Role;
import cn.yanqi.travel.pojo.UserInfo;
import cn.yanqi.travel.service.UserService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;@Service
public class UserServiceImpl implements UserService {@Resourceprivate UserMapper userMapper;@Resourceprivate PasswordEncoder passwordEncoder;/*** 认证--查询用户* @param s* @return* @throws UsernameNotFoundException*/@Overridepublic UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {UserInfo userInfo =  this.userMapper.findUserByUserName(s);User user = new User(userInfo.getUsername(),userInfo.getPassword(),userInfo.getStatus() == 0 ? false : true,//校验用户是否开启true, //帐号是否过期 不过期true, //证号 不过期true, //帐号 不锁定getAuthority(userInfo.getRoles()));System.out.println("用户:"+userInfo.getUsername());System.out.println("=======================");return user;}/*** 认证--查询用户对应的角色*/private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {List<SimpleGrantedAuthority> list = new ArrayList<>();for(Role role : roles){System.out.println("对应角色:"+role.getRoleName());list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));}return list;}
}    

编写UserMapper

public interface UserMapper {/*** 通过用户名查询用户* @param s* @return*/UserInfo findUserByUserName(String s);
}    

编写UserMapper.xml

  <!--登录认证:通过用户名查询用户--><resultMap id="userresultMap" type="UserInfo" autoMapping="true"><id property="id" column="id"/><collection property="roles"   ofType="Role" javaType="List" autoMapping="true"><id property="id" column="rid"/></collection></resultMap><select id="findUserByUserName" resultMap="userresultMap">SELECT*,r.id ridFROMusers u,role r,users_role urWHEREu.id = ur.userId ANDr.id = ur.roleId ANDu.username = #{s}</select>

注意事项:如果登录认证提交出现405,是因为通用页面跳转是@GetMapping, Security的登录后台跳转需要post请求

把通用页面跳转改为@RequestMapping(“{page}”)即可

测试

登录认证-把users表中的status状态修改 0和1进行测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-64TQouaV-1692278828419)(day03_springboot综合案例.assets/image-20210517223706124.png)]

权限控制

Spring Security配置类

// @EnableGlobalMethodSecurity(jsr250Enabled = true)   //开启jsr250注解
// @EnableGlobalMethodSecurity(securedEnabled = true)  //开启secured注解
// @EnableGlobalMethodSecurity(prePostEnabled = true)  //开启表达式注解
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {//Spring Security配置类
}

代码实现

基于方法级别权限控制,有三种方式

    /*** 查询所有产品* @param page* @param size* @return*/// @RolesAllowed({"ADMIN","USER"}) // JSR-250注解// @RolesAllowed("ADMIN") // JSR-250注解// @Secured("ROLE_ADMIN") // Secured注解// @PreAuthorize("authentication.principal.username == 'jack'")//只有jack才可以访问@RequestMapping("findAll")public String findAll( Model model,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "5") Integer size){PageHelper.startPage(page,size);List<Product> productList =  this.productService.findAll();PageInfo pageInfo = new PageInfo(productList);model.addAttribute("pageInfo", pageInfo);return "product-list";}

uestMapping(“findAll”)
public String findAll( Model model,
@RequestParam(value = “page”,defaultValue = “1”) Integer page,
@RequestParam(value = “size”,defaultValue = “5”) Integer size){

    PageHelper.startPage(page,size);List<Product> productList =  this.productService.findAll();PageInfo pageInfo = new PageInfo(productList);model.addAttribute("pageInfo", pageInfo);return "product-list";
}

相关文章:

springboot综合案例第三课

SpringSecurity入门 什么是SpringSecurity Spring Security 的前身是 Acegi Security &#xff0c;是 Spring 项目组中用来提供安全认证服务的框架。 (https://projects.spring.io/spring-security/) Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别 是使…...

面试经典150题——罗马数字转整数

罗马数字包含以下七种字符: I&#xff0c; V&#xff0c; X&#xff0c; L&#xff0c;C&#xff0c;D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如&#x…...

第三篇|金融人数据来源有哪些

数据对于金融行业真的很重要&#xff0c;那么金融人有哪些途径查数据呢&#xff1f; 国内&#xff1a; 1. 国家统计局 这个应该是无论什么行业都使用最频繁的网站&#xff0c;每个月都会固定发上个月资产投资数据 、工业增加值和利润数据等常规数据&#xff0c;其他数据也会…...

爬虫逆向实战(二)--某某观察城市排行榜

一、数据接口分析 主页地址&#xff1a;某某观察 1、抓包 通过抓包可以发现数据接口是multi 2、判断是否有加密参数 请求参数是否加密&#xff1f; 无请求头是否加密&#xff1f; 无cookie是否加密&#xff1f; 无响应数据是否加密&#xff1f; 通过查看“响应”板块可以…...

Grafana Prometheus 通过JMX监控kafka 【2023最新方式】

第三方kafka exporter方案 目前网上关于使用Prometheus 监控kafka的大部分资料都是使用一个第三方的 kafka exporter&#xff0c;他的原理大概就是启动一个kafka客户端&#xff0c;获取kafka服务器的信息&#xff0c;然后提供一些metric接口供Prometheus使用&#xff0c;随意它…...

发布游戏,进行打包。(Unity)

做到这里&#xff0c;我们的项目基本功能已经完成了&#xff0c;如果你还想使项目功能更加完善&#xff0c;可以自己思考如何补充&#xff0c;充分发挥并进行优化使效果达到更加美好。 首先呢&#xff0c;我们这里是说打包Window电脑游戏&#xff0c;我们直接点击菜单栏文件-&…...

我的C++待办事项

2023年8月17日 内存管理部分 学习智能指针 写一篇关于怎么在Linux中安装和使用vclgrind的博客&#xff08;2023年8月17日下午完成&#xff09; 拍一个关于在Linux中安装和使用vclgrind的视频 在Windows上怎么检测内存泄漏 怎么使用Address Sanitizer 在Linux上如何使用gc…...

浙大数据结构第七周之Saving James Bond - Hard Version

题目详情&#xff1a; This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the worlds most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake f…...

线程同步条件变量

为何要线程同步 在线程互斥中外面解决了多线程访问共享资源所会造成的问题。 这篇文章主要是解决当多线程互斥后引发的新的问题&#xff1a;线程饥饿的问题。 什么是线程饥饿&#xff1f;互斥导致了多线程对临界区访问只能改变为串行&#xff0c;这样访问临界资源的代码只能…...

jeecgboot-vue3 查询区 label 文字居左实现

以系统管理中的系统角色界面为例 操作步骤 1. 通过路由或者工具找到当前代码所在的文件 src/views/system/role/index.vue 2. 找到 useListPage 调用&#xff0c;fromConfig 对象加入 labelWidth 和 rowProps 属性 formConfig: {labelWidth: 65, // 设置所有的label宽rowPr…...

CentOS系统环境搭建(五)——Centos7安装maven

centos系统环境搭建专栏&#x1f517;点击跳转 Centos7安装maven 下载压缩包 maven下载官网 解压 压缩包放置到/usr/local tar -xvf apache-maven-3.9.2-bin.tar.gz配置环境变量 vim /etc/profile在最下面追加 MAVEN_HOME/usr/local/apache-maven-3.9.2 export PATH${MAV…...

.eslintrc配置

ESLint 标准规则 /*** AlloyTeam ESLint 规则** 包含所有 ESLint 规则* 使用 babel-eslint 作为解析器** fixable 表示此配置支持 --fix* off 表示此配置被关闭了&#xff0c;并且后面说明了关闭的原因*/module.exports {parser: babel-eslint,parserOptions: {ecmaVersion: 2…...

LangChain手记 Models,Prompts and Parsers

整理并翻译自DeepLearning.AILangChain的官方课程&#xff1a;Models,Prompts and Parsers&#xff08;源码可见&#xff09; 模型&#xff0c;提示词和解析器&#xff08;Models, Prompts and Parsers&#xff09; 模型&#xff1a;大语言模型提示词&#xff1a;构建传递给模…...

Cannot resolve plugin ... maven插件和依赖无法下载解决方法

将 maven/conf/settings.xml 配置文件中的mirror修改成如下即可 <?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"…...

【skynet】skynet 服务间通信

写在前面 skynet 服务之间有自己的一套高效通信 API 。本文给出简单的示例。 文章目录 写在前面准备工作编写代码运行结果 准备工作 首先要有一个编译好&#xff0c;而且工作正常的 skynet 。 编写代码 在 skynet/example 目录编写一个配置文件&#xff0c;两个代码文件。 …...

Flink的Standalone部署实战

在Flink是通用的框架&#xff0c;以混合和匹配的方式支持部署不同场景&#xff0c;而Standalone单机部署方便快速部署&#xff0c;记录本地部署过程&#xff0c;方便备查。 环境要求 1&#xff09;JDK1.8及以上 2&#xff09;flink-1.14.3 3&#xff09;CentOS7 Flink相关信…...

open cv学习 (一)像素的操作

open cv 入门 像素的操作 demo1 import cv2 import os import numpy as np# 1、读取图像 # imread()方法# 设置图像的路径 Path "./img.png" # 设置读取颜色类型默认是1代表彩色图 0 代表灰度图 # 彩色图 flag 1 # 灰度图 #flag 0# 读取图像&#xff0c;返回值…...

基于C#的消息处理的应用程序 - 开源研究系列文章

今天讲讲基于C#里的基于消息处理的应用程序的一个例子。 我们知道&#xff0c;Windows操作系统的程序是基于消息处理的。也就是说&#xff0c;程序接收到消息代码定义&#xff0c;然后根据消息代码定义去处理对应的操作。前面有一个博文例子( C#程序的启动显示方案(无窗口进程发…...

C语言刷题指南(一)

&#x1f4d9;作者简介&#xff1a; 清水加冰&#xff0c;目前大二在读&#xff0c;正在学习C/C、Python、操作系统、数据库等。 &#x1f4d8;相关专栏&#xff1a;C语言初阶、C语言进阶、数据结构刷题训练营、有感兴趣的可以看一看。 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &am…...

VMware虚拟机Ubuntu无法连接网络的解决方法

一、解决办法 网络适配器设置 终端依次执行下面命令即可 sudo nmcli networking off sudo nmcli networking onsudo service network-manager start #或者 sudo service NetworkManager start成功出现这个图标&#xff0c;即代表网络连接成功。...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

2024年赣州旅游投资集团社会招聘笔试真

2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...

蓝桥杯 2024 15届国赛 A组 儿童节快乐

P10576 [蓝桥杯 2024 国 A] 儿童节快乐 题目描述 五彩斑斓的气球在蓝天下悠然飘荡&#xff0c;轻快的音乐在耳边持续回荡&#xff0c;小朋友们手牵着手一同畅快欢笑。在这样一片安乐祥和的氛围下&#xff0c;六一来了。 今天是六一儿童节&#xff0c;小蓝老师为了让大家在节…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...

elementUI点击浏览table所选行数据查看文档

项目场景&#xff1a; table按照要求特定的数据变成按钮可以点击 解决方案&#xff1a; <el-table-columnprop"mlname"label"名称"align"center"width"180"><template slot-scope"scope"><el-buttonv-if&qu…...

AI语音助手的Python实现

引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]

报错信息&#xff1a;libc.so.6: cannot open shared object file: No such file or directory&#xff1a; #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...