news 2026/6/9 16:38:51

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/6/9 21:10:42

【Java毕设全套源码+文档】基于springboot的少数民族音乐网站的设计与实现(丰富项目+远程调试+讲解+定制)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/6/10 12:25:42

基于SpringBoot和Vue的公司文档档案借阅管理系统设计与开发

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

作者头像 李华
网站建设 2026/6/9 15:47:59

揭秘Docker Offload任务状态不同步难题:3步实现高效精准同步

第一章&#xff1a;Docker Offload任务状态同步概述在分布式计算与边缘协同场景中&#xff0c;Docker Offload 技术被广泛用于将计算任务从资源受限设备卸载至性能更强的边缘或云端节点。任务状态同步是保障卸载过程可靠性和一致性的核心机制&#xff0c;确保主控端能够实时感知…

作者头像 李华
网站建设 2026/6/10 12:27:51

AI模型持续交付最佳实践(Docker动态更新全解析)

第一章&#xff1a;AI模型的 Docker 更新机制在持续集成与交付&#xff08;CI/CD&#xff09;流程中&#xff0c;AI模型的部署更新频繁依赖Docker容器化技术。通过封装模型、推理代码及依赖环境&#xff0c;Docker确保了跨平台一致性&#xff0c;同时简化了版本迭代过程。镜像构…

作者头像 李华