SpringSecurity报错authenticationManager must be spec的解决
作者:小楼夜听雨QAQ 时间:2021-07-27 21:52:27
在重写类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
标签:SpringSecurity,报错,authenticationManager
0
投稿
猜你喜欢
JAVA 中Spring的@Async用法总结
2023-11-28 16:35:58
详解Java中自定义注解的使用
2023-11-27 07:36:30
Java 在PDF中添加骑缝章示例解析
2023-11-24 22:41:35
详解Mybatis中常用的约束文件
2023-11-28 08:02:17
ThreadLocal的set方法原理示例解析
2023-11-09 15:06:09
SpringBoot程序的打包与运行的实现
2023-11-29 15:51:27
Java实战之用hutool-db实现多数据源配置
2023-11-28 19:37:10
详解IDEA启动多个微服务的配置方法
2023-11-24 09:22:24
java 字段值为null,不返回该字段的问题
2023-07-13 10:32:34
老生常谈Java String字符串(必看篇)
2023-06-20 19:56:20
JDK1.8中的ConcurrentHashMap源码分析
2023-11-27 06:02:32
SpringBoot2使用Jetty容器操作(替换默认Tomcat)
2023-11-24 01:17:15
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
springboot 正确的在异步线程中使用request的示例代码
2023-11-24 22:36:13
SpringBoot核心@SpringBootApplication使用介绍
2023-11-25 08:33:59
android压力测试命令monkey详解
2023-06-17 00:36:29
Java获取json数组对象的实例讲解
2023-08-24 14:55:28
C语言实现两个矩阵相乘
2023-07-22 12:41:20
JAVA如何定义构造函数过程解析
2023-11-04 08:15:09
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45