SpringSecurity报错authenticationManager must be spec的解决
作者:小楼夜听雨QAQ 发布时间:2021-07-27 21:52:27
标签:SpringSecurity,报错,authenticationManager
在重写类UsernamePasswordAuthenticationFilter时抛出了这个异常,字面上理解是authenticationManager不明确,所以要显示的注入。
有两个地方要改下
首先要在配置文件重写authenticationManager
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
然后在过滤器里面显示的设置一下
@Autowired
@Override
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
顺便贴一下两个类的完整代码
仅供参考:
过滤器
@Component
public class TokenLoginFilter extends UsernamePasswordAuthenticationFilter {
@Autowired
JwtTokenUtils jwtTokenUtils;
public TokenLoginFilter() {
this.setPostOnly(false);
this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST"));
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//获取表单提交数据
try {
UserInfo user = new ObjectMapper().readValue(request.getInputStream(), UserInfo.class);
return super.getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(user.getLoginName(),user.getPassword(),
new ArrayList<>()));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
UserSecurity userSecurity = (UserSecurity) authResult.getPrincipal();
String token = jwtTokenUtils.createToken(userSecurity.getUsername());
ResponseUtils.out(response, R.ok(token));
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
ResponseUtils.out(response, R.fail(ServiceError.LOGIN_FAIL));
}
@Autowired
@Override
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
}
配置文件
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserInfoServiceImpl userInfoService;
@Autowired
JwtAuthorizationTokenFilter jwtAuthorizationTokenFilter;
@Autowired
TokenLoginFilter tokenLoginFilter;
@Autowired
JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userInfoService).passwordEncoder(passwordEncoderBean());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and().csrf().disable()
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/hello").permitAll()
.antMatchers(HttpMethod.OPTIONS, "/**").anonymous()
.anyRequest().authenticated()
.and()
.addFilterAt(tokenLoginFilter, UsernamePasswordAuthenticationFilter.class)
.addFilterAfter(jwtAuthorizationTokenFilter, TokenLoginFilter.class).httpBasic();
}
@Bean
public PasswordEncoder passwordEncoderBean() {
return new BCryptPasswordEncoder();
}
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
}
来源:https://blog.csdn.net/qq_37855749/article/details/111190776


猜你喜欢
- 本文实例讲述了C#中HttpWebRequest的用法。分享给大家供大家参考。具体如下:HttpWebRequest类主要利用HTTP 协议
- 页边距是指页面的边线到文字的距离。通常可在页边距内部的可打印区域中插入文字和图形,也可以将某些项目放置在页边距区域中(如页眉、页脚和页码等)
- 本文实例讲述了Android实时文件夹创建方法。分享给大家供大家参考。具体如下:实时文件夹是一种用来显示由某个ContentProvider
- 这篇文章主要介绍了spring强行注入和引用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 原因每次使用idea新建项目,就会在默认的c盘下的一个maven仓库中下载jar包,可是我自己指定maven仓库不是这个。如何让idea在新
- 在实践中,项目的某些配置信息是需要进行加密处理的,以减少敏感信息泄露的风险。比如,在使用Druid时,就可以基于它提供的公私钥加密方式对数据
- 概述常用的弹窗有菜单,或者Dialog,但更加人性化和可自定义的还是PopupWindow如果只是展示列表数据或者弹窗列表选择,直接使用Li
- 1.1 解决方案名和项目名上右键——重命名如下图:改完后效果如下:1.2 接下来要改名 代码中的名称
- 一个Java程序的执行要经过编译和执行(解释)这两个步骤,同时Java又是面向对象的编程语言。当子类和父类存在同一个方法,子类重写了父类的方
- 1.算法效率算法效率分析分为两种:第一种是时间效率,第二种是空间效率。时间效率被称为时间复杂度,而空间效率被称作空间复杂度。时间复杂度主要衡
- 目录一. 已有倒计时方案存在的问题1. CountDownTimer2. Handler3. Timer二. 自己封装倒计时总结一. 已有倒
- Java Exception 捕获和显示实例详解 在进行Java B/S架构开发时,经常有这样的场景:后端处理业务逻辑,前端负责
- 在java项目开发中。最开始换行符大家一般是在idea中设置新文件为LF,并且对旧文件通过IDEA下方的点击来更换换行符。很显然,对于几千文
- 前言最近在开发一个IM项目的时候有一个需求就是,好友搜索功能。即在EditText中输入好友名字,ListView列表中动态展示刷选的好友列
- 目标:了解Swagger的作用和概念了解前后端分离在springBoot中集成SwaggerSwagger简介前后端分离VUE+spring
- 前沿知识ThreadLocal存储线程变量,使用set方法设置变量,使用get方法获取变量线程隔离的实现是每个Thread类有一个类型为Th
- 每日一笑下班和实习生一起回家,公交站等车,一乞丐把碗推向实习生乞讨。这时,实习生不慌不忙的说了句:“我不要你的钱,你这钱
- 也许是本人不才,初识Maven时,被各种不明所以的教程搞得一头雾水,而在后来的使用中,我发现Maven大部分功能没有想象的那么困难。本片文章
- 本文实例讲述了C#计算程序执行过程花费时间的方法。分享给大家供大家参考。具体如下:计算执行完程序花费的时间:void AddInfo(){
- 前言Eureka是一种基于REST(具像状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移。本文记录一个简