5.Spring Security-web权限方案

设置登录的用户名和密码

1.通过配置文件设置用户名密码

spring:
  security:
    user:
      name: xiankejin
      password: 123456

如果没有以上配置,那么就会在后台生成一个随机密码,用户名固定位user。

2.通过配置类设置用户名密码

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //将密码加密处理
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");
        auth.inMemoryAuthentication().withUser("Lucy").password(encode).roles("admin");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

3.自定义实现类设置用户名密码

spring security中的用户接口UserDetails和实现类User

User类中的构造器(用户名,密码,权限集合) 

定义配置类继承WebSecurityConfigurerAdapter

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

定义类实现UserDetailsService接口

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        List<GrantedAuthority> authorities =
                AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User("Marry", new BCryptPasswordEncoder().encode("123456"), authorities);
    }
}

这里就可以写入查询数据库的验证用户的逻辑

引入mybatisplus + mysql 坐标:

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
spring:
  datasource:
    username: root
    driver-class-name: com.mysql.jdbc.Driver
    password: 123456
    url: jdbc:mysql://localhost:3306/demo
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Repository
public interface UsersMapper extends BaseMapper<Users> {


}
@Data
public class Users {

    private Long id;
    private String username;
    private String password;

}
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<Users> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Users::getUsername, username);
        Users users = usersMapper.selectOne(wrapper);
        if(null == users) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<GrantedAuthority> authorities =
                AuthorityUtils.commaSeparatedStringToAuthorityList("role");
        return new User(users.getUsername(), new BCryptPasswordEncoder().encode(users.getPassword()), authorities);
    }
}
@SpringBootApplication
@MapperScan("com.xkj.org.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/594959.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【AIGC】深入探索AIGC技术在文本生成与音频生成领域的应用

&#x1f680;文章标题 &#x1f680;AIGC之文本生成&#x1f680;应用型文本生成&#x1f680;创作型文本生成&#x1f680;文本辅助生成&#x1f680;重点关注场景 &#x1f680;音频及文字—音频生成&#x1f680;TTS(Text-to-speech)场景&#x1f680;乐曲/歌曲生成&#x…

给股东送酱的公司值得关注吗?仲景食品-300908 年报分析(20240505)

仲景食品-300908 基本情况 公司名称&#xff1a;仲景食品股份有限公司 A股简称&#xff1a;仲景食品 成立日期&#xff1a;2002-09-29 上市日期&#xff1a;2020-11-23 所属行业&#xff1a;食品制造业 周期性&#xff1a;0 主营业务&#xff1a;调味配料和调味食品的研发、生产…

Android 14 变更及适配攻略

准备工作 首先将我们项目中的 targetSdkVersion和compileSdkVersion 升至 34。 影响Android 14上所有应用 1.最低可安装的目标 API 级别 从 Android 14 开始&#xff0c;targetSdkVersion 低于 23 的应用无法安装。要求应用满足这些最低目标 API 级别要求有助于提高用户的安…

跟TED演讲学英文:Is your partner “the one?“ Wrong question by George Blair-West

Is your partner “the one?” Wrong question Link: https://www.ted.com/talks/george_blair_west_is_your_partner_the_one_wrong_question Speaker: George Blair-West Date: December 2022 文章目录 Is your partner "the one?" Wrong questionIntroduction…

【Unity 组件思想-预制体】

【Unity 组件思想-预制体】 预制体&#xff08;Prefab&#xff09;是Unity中一种特殊的组件 特点和用途&#xff1a; 重用性&#xff1a; 预制体允许开发者创建可重复使用的自定义游戏对象。这意味着你可以创建一个预制体&#xff0c;然后在场景中多次实例化它&#xff0c;…

快速上手RabbitMQ

安装RabbitMQ 首先将镜像包上传到虚拟机&#xff0c;使用命令加载镜像 docker load -i mq.tar 运行MQ容器 docker run \-e RABBITMQ_DEFAULT_USERitcast \-e RABBITMQ_DEFAULT_PASS123321 \-v mq-plugins:/plugins \--name mq \--hostname mq \-p 15672:15672 \-p 5672:5672 …

图像识别——玩转YOLO网络

图像识别——玩转YOLO网络 YOLO&#xff0c;全称“You Only Look Once”&#xff0c;意为你只需要看一次&#xff0c;是一种快速、准确的目标检测算法。它由Joseph Redmon等人在2016年提出&#xff0c;其核心思想是将输入图像划分为SS个网格单元&#xff0c;每个网格预测B个边…

什么是脏读?幻读?不可重复读?

脏读(Drity Read)&#xff1a;某个事务 A 已更新一份数据&#xff0c;另一个事务 B 在此时读取了同一份数据&#xff0c;由于某些原因&#xff0c;事务 A 回滚&#xff0c;而事务B读取到事务 A 回滚前的数据。 例子:小明读取到小红提交的100数据.但是小红异常回滚了数据,100变…

STM32单片机实战开发笔记-PWM波输出频率及占空比配置【wulianjishu666】

单片机物联网开发资料&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1XzodQuML7CqZ4ZKinDGKkg?pwdbgep 提取码&#xff1a;bgep PWM模块测试 功能描述 脉冲宽度调制模式&#xff1a; PWM边沿对齐模式&#xff1a; 向上计数配置 当TIMX_CR1寄存器中的DIR为低的时…

Video2Game:革新游戏开发,重塑虚拟世界的未来

Video2Game&#xff1a;革新游戏开发&#xff0c;重塑虚拟世界的未来 一、Video2Game的提出与意义二、Video2Game的核心技术三、Video2Game的实现与应用四、代码实例与未来展望 在数字化和虚拟化日益盛行的今天&#xff0c;高质量的交互式虚拟环境&#xff0c;如游戏和模拟器&a…

TinTin Web3 Bounty 挑战杯第二期再启程,NEAR 生态邀请你来找 Bug!

对 Web3 来说&#xff0c;Bounty 任务应该是普通人获得行业“一杯羹”的重要捷径&#xff01; 通过深入学习各类 Web3 公链技术&#xff0c;凭借实战锻炼开发创新项目&#xff0c;或完善已有网络运行中出现的问题&#xff0c;就有机会更加快速了解其底层技术逻辑&#xff0c;更…

基于YOLOv8+PyQt5复杂场景下船舶目标检测系统

1. 应用场景 复杂场景下船舶目标检测系统的应用场景包括&#xff1a; 港口管理和安全&#xff1a;监控港口区域&#xff0c;确保船舶安全地进出港口&#xff0c;预防相撞事故的发生。 海洋交通监控&#xff1a;实时追踪海上交通流&#xff0c;并识别违规或异常航行行为&#x…

Python ValueError: bad transparency mask

修改前 修复后 运行正常 from PIL import Image# 读取图片 #报错信息解决ValueError: bad transparency mask--相关文档地址https://blog.csdn.net/kalath_aiur/article/details/103945309 #1. 检查 alpha 通道是否是一个有效的掩码。如果不是&#xff0c;则需要对 alpha 通道…

《Boundary Smooth for NER》

来源&#xff1a; ACL2022, 作者&#xff1a;中科院 命名实体识别(NER)模型很容易遇到over-confidence的问题&#xff0c;从而降低了性能。 基于边界存在的问题&#xff0c;参考 Label Smoothing&#xff0c;作者提出了 boundary smoothing 的训练方法&#xff0c;即使用 biaff…

苍穹外卖,接入redis cache后,新增套餐有问题

终端报错&#xff1a; java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public com.sky.result.Result com.sky.controller.admin.SetmealController.save(com.sky.d…

Stable Diffusion WebUI 中文提示词插件 sd-webui-prompt-all-in-one

本文收录于《AI绘画从入门到精通》专栏,订阅后可阅读专栏内所有文章,专栏总目录:点这里。 大家好,我是水滴~~ 今天为大家介绍 Stable Diffusion WebUI 的一款中文提示词插件 sd-webui-prompt-all-in-one,就像它的名字一样,该插件几乎涵盖了提示词相关的所有功能。 文章内…

3D模型格式转换工具HOOPS Exchange如何读取建筑工程中复杂庞多的数据?

在当今数字化时代&#xff0c;建筑行业正日益依赖于复杂的3D建模工具和软件&#xff0c;以便在设计、规划和建造过程中实现更高的效率和精确性。然而&#xff0c;这种效率的提升往往伴随着一个挑战&#xff1a;不同软件之间的3D模型格式可能不兼容&#xff0c;这导致了数据转换…

SpringBootWeb创建

创建spring项目 创建SpringBoot工程定义请求处理类运行常见问题java: 无效的源发行版: XXjava: 无法访问org.springframework.web.bind.annotation.RequestMapping类文件具有错误的版本 61.0, 应为 52.0 创建SpringBoot工程 定义请求处理类 RestController public class HelloC…

unity入门学习笔记

文章目录 unity学习笔记熟悉界面窗口页面快捷键视图特点移动、旋转、缩放快捷键聚焦和隐藏 一些基本概念模型模型的导入一些补充 资源文件资源包的导出资源包的导入 轴心物体的父子关系空物体Global与localpivot与center 组件脚本基础我的第一个脚本 获取脚本组件本地坐标播放模…

JVM调优--理论篇

在对Java应用进行性能优化时&#xff0c;JVM的调优是一个绕不开的话题。本文重点介绍下如何对JVM进行调优&#xff0c;以期提高Java应用的性能、稳定性、响应时间等性能目标。JVM的调优过程符合Java应用的调优过程&#xff0c;主要分为三步&#xff1a;性能监控、性能分析、性能…
最新文章