news 2026/4/18 11:55:49

SpringBoot 使用SpringSecurity

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot 使用SpringSecurity

引入依赖

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

重启项目默认会对所有接口增加拦截

用户名密码可以在yml中指定

spring.security.user.name=user spring.security.user.password=123456

一般需要在数据库中查找

需要实现UserDetailsService

loadUserByUsername返回的用户名密码会与提交的密码对比

package com.example.demo.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.Query; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.ArrayList; @Service public class UserDetailServiceImpl implements UserDetailsService { @Autowired private UserMapper userMapper; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getUsername,username)); if(user == null) throw new UsernameNotFoundException("用户不存在"); return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),new ArrayList<>()); } }

所以需要指定密码加密方式,测试使用不加密

package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class SecurityConfig { // 配置无密码加密策略 @Bean public PasswordEncoder passwordEncoder() { return NoOpPasswordEncoder.getInstance(); } }

增加test方法

@GetMapping("/test") public String test(){ Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); return authentication.getName(); }

在测试一下 ,登录成功后可以成功输出当前登录用户

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 7:02:59

深度学习图像分类实战 - 从零开始构建CNN模型

目录引言CNN基础原理卷积操作池化操作典型CNN架构构建CNN图像分类器环境准备数据加载与预处理CNN模型定义模型训练模型评估结果可视化预测示例模型优化技巧数据增强学习率调度正则化技术总结与展望进一步探索引言 图像分类是计算机视觉领域最基础且重要的任务之一。它旨在将输…

作者头像 李华
网站建设 2026/4/17 8:15:54

血浆多组学网络如何揭示急性早幼粒细胞白血病的病理机制?

一、急性早幼粒细胞白血病的治疗现状与挑战是什么&#xff1f;急性早幼粒细胞白血病&#xff08;APL&#xff09;是一种特殊类型的急性髓系白血病&#xff0c;其发病与PML/RARA融合基因的形成密切相关。目前&#xff0c;全反式维甲酸联合三氧化二砷&#xff08;ATRA/ATO&#x…

作者头像 李华
网站建设 2026/4/17 23:50:03

基于springboot和vue的家庭记账收支理财管理系统的设计与实现

目录已开发项目效果实现截图开发技术系统开发工具&#xff1a;核心代码参考示例1.建立用户稀疏矩阵&#xff0c;用于用户相似度计算【相似度矩阵】2.计算目标用户与其他用户的相似度系统测试总结源码文档获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&…

作者头像 李华
网站建设 2026/4/17 21:49:20

基于zigbee的抄表系统(有完整资料)

资料查找方式&#xff1a;特纳斯电子&#xff08;电子校园网&#xff09;&#xff1a;搜索下面编号即可编号&#xff1a;T4532310M设计简介&#xff1a;本设计是基于zigbee的抄表系统&#xff0c;主要实现以下功能&#xff1a;从机通过温湿度传感器检测温湿度 从机通过空气质量…

作者头像 李华
网站建设 2026/4/18 3:23:35

基于32单片机矿井安全检测系统(有完整资料)

资料查找方式&#xff1a;特纳斯电子&#xff08;电子校园网&#xff09;&#xff1a;搜索下面编号即可编号&#xff1a;T4562309M设计简介&#xff1a;本设计是基于32单片机矿井安全检测系统&#xff0c;主要实现以下功能&#xff1a; 通过对温度、湿度、可燃气体、甲醛含量等…

作者头像 李华
网站建设 2026/4/18 7:56:23

APM 性能监控是什么?从应用监控与网站监控了解基础概念

在2025年经济下行周期中&#xff0c;企业IT支出策略正经历深刻变革。最高检数据显示&#xff0c;前三季度起诉电信网络诈骗犯罪4.9万人&#xff0c;凸显网络安全威胁的严峻性&#xff1b;而中研网报告指出&#xff0c;中国信息化运维行业已进入智能化与自动化深度融合新阶段&am…

作者头像 李华