news 2026/7/22 4:23:05

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/7/21 6:20:26

高盐高铵根工业废水去除重金属

随着新能源产业的快速发展&#xff0c;锂离子电池凭借能量密度高、自放电低、循环寿命长等优势&#xff0c;已广泛应用于电子设备、电动汽车等领域。作为锂电池核心组成部分的三元正极材料&#xff0c;其前驱体生产过程中会产生含镍、钴等有价金属的工业废水。在 “缺芯贵电” …

作者头像 李华
网站建设 2026/7/16 15:08:03

某211高校讲师晒工资条,网友:公积金数额令人瞩目...

教师的工资一直具有争议性&#xff0c;大家的认知两极分化&#xff0c;有人说教师收入特别高&#xff0c;也有人说教师收入堪堪够生活&#xff0c;不存在谁说假话&#xff0c;而是因为各地区教师薪资水平差异较大。学校属于事业单位&#xff0c;薪资受当地经济水平影响&#xf…

作者头像 李华
网站建设 2026/7/20 18:06:13

小鼠T细胞激活:如何系统解析其发育分化与免疫功能表征?

一、T淋巴细胞发育经历哪些关键阶段&#xff1f;T淋巴细胞作为适应性免疫的核心组成部分&#xff0c;其发育过程呈现高度程序化特征。起源于骨髓的T细胞前体迁移至胸腺后&#xff0c;经历双阴性&#xff08;DN&#xff09;阶段的分化&#xff1a;根据CD44、CD25和CD117的表达差…

作者头像 李华
网站建设 2026/7/20 19:13:58

基于springboot和vue的民航飞机票务管理系统设计与实现

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

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

2025年12月-2026年4月,计算机领域涵盖的前言学术会议推荐!

关注星标RDLink公众号&#xff0c;获取更多学术会议资讯IEEE&#xff08;电气与电子工程师学会&#xff09;是全球最大的技术专业协会&#xff0c;每年举办超过1300场学术会议&#xff0c;覆盖电气电子、计算机、通信等领域。其会议论文通常通过IEEE Xplore数字图书馆出版&…

作者头像 李华