Spring Security 的关键配置

0. 概述以前我们配置 springsecurity 的方式是继承 websecurityconfigureradapter ,然后重写其中的几个方法:
@configurationpublic class securityconfig extends websecurityconfigureradapter { //配置 spring security 中的过滤器链 @override void configure(httpsecurity http) {} //配置路径放行规则 @override void configure(websecurity web) {} //配置本地认证管理器 @override void configure(authenticationmanagerbuilder auth) {} //配置全局认证管理器 @override authenticationmanager authenticationmanagerbean() {}}目前这个类已经过期,虽然可以继续使用,但是总觉得别扭。那么它的替代方案是什么?下面我来为大家一一介绍。
1. httpsecurity原写法:
@overrideprotected void configure(httpsecurity http) throws exception { http .antmatcher(/**) .authorizerequests(authorize - > authorize .anyrequest().authenticated() );}新写法:
@beansecurityfilterchain filterchain(httpsecurity http) throws exception { return http .antmatcher(/**) .authorizerequests(authorize - > authorize .anyrequest().authenticated() ) .build();}2. websecurity原写法:
@overridepublic void configure(websecurity web) { web.ignoring().antmatchers(/ignore1, /ignore2);}新写法:
@beanpublic websecuritycustomizer websecuritycustomizer() { return (web) - > web.ignoring().antmatchers(/ignore1, /ignore2);}websecurity配置不常使用,如果需要忽略url,推荐通过 httpsecurity.authorizehttprequests 的 permitall 来实现。
3. authenticationmanager原写法:
@autowiredprivate userdetailsservice userdetailsservice;@beanpublic bcryptpasswordencoder bcryptpasswordencoder() { return new bcryptpasswordencoder();}//local@overrideprotected void configure(authenticationmanagerbuilder auth) throws exception { auth.userdetailsservice(userdetailsservice).passwordencoder(bcryptpasswordencoder());}//global@bean@overridepublic authenticationmanager authenticationmanagerbean() throws exception { return super.authenticationmanagerbean();}新写法:
@autowiredprivate userdetailsservice userdetailsservice;@beanpublic bcryptpasswordencoder bcryptpasswordencoder() { return new bcryptpasswordencoder();}//local@beanpublic securityfilterchain filterchain(httpsecurity http) throws exception { http .authorizehttprequests((authz) - > authz .anyrequest().authenticated() ) .httpbasic(withdefaults()) .authenticationmanager(new customauthenticationmanager());}//global@beanpublic authenticationmanager authenticationmanager(httpsecurity httpsecurity) throws exception { return httpsecurity.getsharedobject(authenticationmanagerbuilder.class) .userdetailsservice(userdetailsservice) .passwordencoder(bcryptpasswordencoder()) .and() .build();}4. 心得技术是不断迭代的,我们作为技术人员,不能墨守成规,要学会拥抱变化。

运放失调电压自动补偿设计与实现
适用于无人驾驶的分布式仿真平台
发动机 3D 可视化系统让工业机械展示更清晰
硬盘数据快速备份怎样实现
Flex宣布推出新型DC/DC高级总线转换器BMR480
Spring Security 的关键配置
米动健康手环1S发布 一款可以媲美苹果Watch4的健康手环
有机肥化验检测设备的特点
JTAG链路同时调试FPGA和HPS电路设计
激励机制究竟是不是加密货币的核心
如何构建扬声器的RLC等效模型
对于初次使用的buck电源芯片,如何做模块性能测试?
YAMAHA/雅马哈功放 RX-V359参数
mysql分页问题和优化的思路是什么
从独立模块到完整解决方案,IP开启ASIC模式
小米7什么时候上市?小米7最新消息:小米7工业设计的巅峰,小米7骁龙845+97%全面屏值得期待
LED驱动器为汽车应用提供单一解决方案
库克点评苹果自助维修计划:对这项目感觉很好
电容器的结构、原理及其作用的详细介绍
储能连接器插头工厂到底有什么优势