news 2026/5/29 23:30:18

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/5/29 17:14:46

低代码物联网平台

物联网平台 - Thinglinks-iot ## &#x1f31f; 项目简介 一个功能完备、高可扩展的物联网平台&#xff0c;提供完整的设备接入、管理和数据处理解决方案。支持多种网络协议&#xff0c;具备强大的消息解析和实时告警能力&#xff0c;帮助企业快速构建物联网应用。 该项目现已纳…

作者头像 李华
网站建设 2026/5/30 14:26:37

智慧校园平台用户体验评估:如何做好师生满意度调查

✅作者简介&#xff1a;合肥自友科技 &#x1f4cc;核心产品&#xff1a;智慧校园平台(包括教工管理、学工管理、教务管理、考务管理、后勤管理、德育管理、资产管理、公寓管理、实习管理、就业管理、离校管理、科研平台、档案管理、学生平台等26个子平台) 。公司所有人员均有多…

作者头像 李华
网站建设 2026/5/30 15:18:57

基于springboot和vue的电影视在线订票选座观看分享系统的设计与实现

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

作者头像 李华
网站建设 2026/5/24 1:09:45

基于STM32的智能养老实时监控系统的设计(有完整资料)

资料查找方式&#xff1a;特纳斯电子&#xff08;电子校园网&#xff09;&#xff1a;搜索下面编号即可编号&#xff1a;T4552310M设计简介&#xff1a;本设计是基于STM32的智能养老实时监控系统&#xff0c;主要实现以下功能&#xff1a;通过心率血氧传感器检测心率、血氧 通过…

作者头像 李华
网站建设 2026/5/27 13:16:05

实在干不过中国电车,欧盟将取消禁售燃油车,德国车企开心了

在拥有诸多车企的德国、法国等的游说下欧盟对于禁售燃油车的目标已有所动摇&#xff0c;毕竟德国、法国是欧盟的两大支柱&#xff0c;外媒传出的消息指欧盟很可能会修改碳排放目标&#xff0c;将从原来的要求碳排放降低100%改为降低90%&#xff0c;而有了开始就有可能进一步降低…

作者头像 李华