news 2026/7/12 12:50:32

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/11 10:38:01

物联网平台二开

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

作者头像 李华
网站建设 2026/7/12 3:05:40

基于springboot和vue的厨房菜谱美食分享网站的设计与实现

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

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

Android设备日志分析与电源管理问题

这份日志显示了一个Android设备从唤醒&#xff08;Resume&#xff09;到再次休眠&#xff08;Suspend&#xff09; 的完整过程&#xff0c;其中出现了两个关键错误&#xff1a; 主要问题分析&#xff1a; 1. MSDC/eMMC通信失败&#xff08;核心问题&#xff09; [149013.510436…

作者头像 李华
网站建设 2026/7/12 20:36:17

【dz-982】基于单片机的篮球计分器

摘 要 随着体育赛事的不断发展&#xff0c;篮球比赛的计分和记录方式逐渐向自动化、智能化方向发展。传统的计分方式存在人工操作繁琐、容易出错等问题&#xff0c;因此开发一种高效、准确的篮球比赛自动积分设备具有重要的现实意义。 本设计旨在通过现代电子技术和物联网技…

作者头像 李华
网站建设 2026/7/11 17:24:54

如何利用动环监控系统提升数据中心的能效与安全性?

随着数据中心需求的不断增加&#xff0c;动力环境监控系统成为了提升能效和安全性的关键工具。该系统通过实时监测各种环境因素&#xff0c;如温湿度、电能消耗及设备运行状态&#xff0c;帮助运维人员有效掌握数据中心的健康状况。运维团队可以根据系统提供的数据&#xff0c;…

作者头像 李华
网站建设 2026/7/11 13:50:35

MobaXterm高效运维实战:Linux运维高级技巧与自动化脚本万字详解

第一章&#xff1a;MobaXterm核心优势与架构解析1.1 为什么选择MobaXterm进行Linux运维&#xff1f;MobaXterm作为Windows平台下最强大的远程计算工具箱&#xff0c;为Linux运维工程师提供了无可比拟的便利性&#xff1a;核心优势对比&#xff1a;一体化集成&#xff1a;SSH、X…

作者头像 李华