servlet的坑_Spring Security 4.x -> 5.x 踩坑记录

Spring Security 4.x -> 5.x 踩坑记录

1、应用服务基路径问题

这个问题应该是Spring Boot 2.0升级带来的,既然遇到了,就在这里写一写。笔者在授权服务器想设置一个统一基路径,按照Spring Boot 1.0,是这样的:

server.context-path=/xxx

但是升级之后并不好使,最后看官方文档发现改掉了,现在是这样的:

server.servlet.context-path=/xxx

2、AuthenticationManager无法注入

在覆写AuthorizationServerConfigurerAdapter类的public void configure(AuthorizationServerEndpointsConfigurer endpoints) 方法时,往往需要显式注入AuthenticationManager ,但是在5.x版本中,启动会报如下错误:

***************************APPLICATION FAILED TO START***************************Description:Field authenticationManager in cn.springcloud.book.OAuthConfiguration required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.Action:Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

解决方案:

在启动主类继承WebSecurityConfigurerAdapter 类同时,手动注入:

 @Bean(name = BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }

3、登陆报错:There is no PasswordEncoder mapped for the id “null”

在使用Spring Security 5.x登陆页面进行登陆时,后端会报错:There is no PasswordEncoder mapped for the id “null”,因为5.x版本新增了多种密码加密方式,必须指定一种,比如这样解决:

 @Bean public static NoOpPasswordEncoder passwordEncoder() { return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance(); }

下列加密方式供参考,选取一种即可:

bcrypt - BCryptPasswordEncoder (Also used for encoding)ldap - LdapShaPasswordEncoderMD4 - Md4PasswordEncoderMD5 - new MessageDigestPasswordEncoder("MD5")noop - NoOpPasswordEncoderpbkdf2 - Pbkdf2PasswordEncoderscrypt - SCryptPasswordEncoderSHA-1 - new MessageDigestPasswordEncoder("SHA-1")SHA-256 - new MessageDigestPasswordEncoder("SHA-256")sha256 - StandardPasswordEncoder

关注我,私信关键字【资料】即可获取Java并发编程/Spring源码分

析/redis/mongodb/dubbo/zookeeperfka /Spring-cloud和高并发、高可用、分布式、

高性能架构设计精讲,还有面试专题的资料

原文:https://blog.csdn.net/rickiyeat/article/details/79949807

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值