news 2026/7/7 6:33:06

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/7 12:55:11

JAVA物联网平台

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

作者头像 李华
网站建设 2026/7/7 9:42:02

基于springboot和vue的茶叶商城论坛系统

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

作者头像 李华
网站建设 2026/7/7 10:07:40

拍口播视频必备神器——芦笋提词器,助你流畅自然不走神

在自媒体时代&#xff0c;口播视频成为内容创作的主流形式之一。 但很多创作者都会遇到一个难题——面对镜头容易忘词、紧张卡壳&#xff0c;影响表现效果。 芦笋提词器正是为解决这一痛点量身打造的专业提词工具&#xff0c;助力你拍摄流畅自然的口播视频。 为什么芦笋提词…

作者头像 李华
网站建设 2026/7/6 12:18:32

MySQL数据导入两个问题解决

点击标题下「蓝色微信名」可快速关注最近做个测试数据导入的工作&#xff0c;碰到两个问题&#xff0c;还是能引申出一些知识点。&#xff08;1&#xff09;导入数据出现类型转换错误我们通过dbeaver客户端导入csv文件格式的数据&#xff0c;可能会碰到这个问题&#xff0c;提示…

作者头像 李华
网站建设 2026/7/6 7:11:55

2025年TED最热门演讲:一个调查记者对“硅谷诸神”的抗争

2025年将尽&#xff0c;TED发布了年内十个最热门的演讲&#xff0c;排在第一的是英国调查记者卡罗尔・卡德瓦拉德&#xff08;Carole Cadwalladr&#xff09;的演说&#xff0c;主题是This is what a digital coup looks like&#xff0c;暂译为“这就是一场数字政变的模样”。…

作者头像 李华