当前位置: 首页 > news >正文

SpringSecurity-重写默认配置

重写UserDetailService组件

1.注入Bean的方式

/*** @author: coffee* @date: 2024/6/22 21:22* @description: 重写springsecurity默认组件:注入Bean的方式*/@Configuration
public class ProjectConfig {/*** 重写userDetailsService组件*/@Beanpublic UserDetailsService userDetailsService () {// InMemoryUserDetailsManager实现并不适用生成环境,此处进作为demo使用InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();// 使用指定用户名、密码和权限列表构建用户UserDetails user = User.withUsername("john").password("12345").authorities("read").build();// 添加该用户以便让UserDetailsService对其进行管理userDetailsService.createUser(user);return userDetailsService;}/*** 重写UserDetailsService组件也必须重写PasswordEncoder组件,否则会报:*    java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"*/@Beanpublic PasswordEncoder passwordEncoder () {// NoOpPasswordEncoder实例会将密码视为普通文本,他不会对密码进行加密或者hash处理return NoOpPasswordEncoder.getInstance();}
}

2.扩展WebSecurityConfigurerAdapter

/*** @author: coffee* @date: 2024/6/22 21:46* @description:*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {/*** 重写端点授权配置,就需要扩展WebSecurityConfigurerAdapter类,可以使用HttpSecurity对象的不同方法更改配置*/@Overrideprotected void configure (HttpSecurity httpSecurity) throws Exception {httpSecurity.httpBasic();// 所有请求都需要身份验证// httpSecurity.authorizeRequests().anyRequest().authenticated();// permitAll()方法修改授权配置,无需凭据(用户名密码)也可以直接调用接口。   curl http://localhost:8080/hellohttpSecurity.authorizeRequests().anyRequest().permitAll();}/*** 重写springsecurity默认组件:继承WebSecurityConfigurerAdapter的方式*/@Overrideprotected void configure (AuthenticationManagerBuilder auth) throws Exception {// InMemoryUserDetailsManager实现并不适用生成环境,此处进作为demo使用InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();// 使用指定用户名、密码和权限列表构建用户UserDetails user = User.withUsername("john").password("12345").authorities("read").build();// 添加该用户以便让UserDetailsService对其进行管理userDetailsService.createUser(user);// AuthenticationManagerBuilder调用userDetailsService()方法来注册UserDetailsService实例// AuthenticationManagerBuilder调用passwordEncoder()方法来注册NoOpPasswordEncoder实例auth.userDetailsService(userDetailsService).passwordEncoder(NoOpPasswordEncoder.getInstance());}
}

重写端点授权配置

/*** @author: coffee* @date: 2024/6/22 21:46* @description:*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {/*** 重写端点授权配置,就需要扩展WebSecurityConfigurerAdapter类,可以使用HttpSecurity对象的不同方法更改配置*/@Overrideprotected void configure (HttpSecurity httpSecurity) throws Exception {httpSecurity.httpBasic();// 所有请求都需要身份验证// httpSecurity.authorizeRequests().anyRequest().authenticated();// permitAll()方法修改授权配置,无需凭据(用户名密码)也可以直接调用接口。   curl http://localhost:8080/hellohttpSecurity.authorizeRequests().anyRequest().permitAll();}
}

重写AuthenticationProvider实现

/*** @author: coffee* @date: 2024/6/22 22:15* @description: ...*/
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {@Overridepublic Authentication authenticate(Authentication authentication) throws AuthenticationException {String userName = authentication.getName();String password = String.valueOf(authentication.getCredentials());// 重写身份验证提供者,用if else 替换 UserDetailsService和PasswordEncoderif ("john".equals(userName) && "12345".equals(password)) {return new UsernamePasswordAuthenticationToken(userName, password, Arrays.asList());} else {throw new AuthenticationCredentialsNotFoundException("ERROR");}}@Overridepublic boolean supports(Class<?> authentication) {return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);}
}
/*** @author: coffee* @date: 2024/6/22 21:46* @description:*/
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {@Autowiredprivate CustomAuthenticationProvider customAuthenticationProvider;/*** 重写端点授权配置,就需要扩展WebSecurityConfigurerAdapter类,可以使用HttpSecurity对象的不同方法更改配置*/@Overrideprotected void configure (HttpSecurity httpSecurity) throws Exception {httpSecurity.httpBasic();// 所有请求都需要身份验证httpSecurity.authorizeRequests().anyRequest().authenticated();}/*** 重写身份验证提供者*/@Overrideprotected void configure (AuthenticationManagerBuilder auth) throws Exception {auth.authenticationProvider(customAuthenticationProvider);}
}

http://www.lryc.cn/news/384326.html

相关文章:

  • C# 判断值是否在枚举里
  • Interview preparation--elasticSearch倒排索引原理
  • 银河麒麟高级服务器操作系统V10SP2(X86)配置bond0的mac地址为指定子网卡的mac地址
  • python中不同维度的Tensor向量为何可以直接相加——广播机制
  • 38.MessageToMessageCodec线程安全可被共享Handler
  • Linux中的全局环境变量和局部环境变量
  • 【研究】AI大模型需要什么样的硬件?
  • 人工智能--自然语言处理NLP概述
  • 基于Java微信小程序火锅店点餐系统设计和实现(源码+LW+调试文档+讲解等)
  • SpringCloud_GateWay服务网关
  • 使用Dropout大幅优化PyTorch模型,实现图像识别
  • Vue3中的常见组件通信(超详细版)
  • Stm32的DMA的学习
  • 应用安全(补充)
  • 鸿蒙开发Ability Kit(程序框架服务):【FA模型切换Stage模型指导】 app和deviceConfig的切换
  • 通过命令行配置调整KVM的虚拟网络
  • Apache POI操作excel
  • Python错误集锦:faker模块生成xml文件时提示:`xml` requires the `xmltodict` Python library
  • Vue3-尚硅谷笔记
  • RockChip Android12 System之MultipleUsers
  • 第12天:前端集成与Django后端 - 用户认证与状态管理
  • 在ROS2中蓝牙崩溃的原因分析
  • 【PythonWeb开发】Flask中间件钩子函数实现封IP
  • 可以一键生成热点营销视频的工具,建议收藏
  • Unity Meta Quest 开发:关闭 MR 应用的安全边界
  • 4.sql注入攻击(OWASP实战训练)
  • 前端Web开发HTML5+CSS3+移动web视频教程 Day1
  • 中医实训室:在传统针灸教学中的应用与创新
  • React Hooks 小记(七)_useReducer
  • 甲子光年专访天润融通CEO吴强:客户经营如何穿越低速周期?