SpringSecurity报错authenticationManager must be spec的解决

作者:小楼夜听雨QAQ 时间:2021-07-27 21:52:27 

在重写类UsernamePasswordAuthenticationFilter时抛出了这个异常,字面上理解是authenticationManager不明确,所以要显示的注入。

有两个地方要改下

首先要在配置文件重写authenticationManager

SpringSecurity报错authenticationManager must be spec的解决

@Bean
   @Override
   protected AuthenticationManager authenticationManager() throws Exception {
       return super.authenticationManager();
   }

然后在过滤器里面显示的设置一下

SpringSecurity报错authenticationManager must be spec的解决

@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中StringTokenizer的用法简介汇总

    2023-05-29 00:52:24
  • java文件上传下载代码实例

    2023-11-10 05:06:14
  • 淘宝IP地址库采集器c#代码

    2022-01-15 22:42:33
  • C#实现关闭其他程序窗口或进程代码分享

    2022-05-26 07:38:03
  • Flutter 队列任务的实现

    2023-07-07 17:25:14
  • 详解Java中while和do-while循环、break的使用

    2022-10-24 13:37:04
  • GSON实现Java对象与JSON格式对象相互转换的完全教程

    2023-11-23 09:23:37
  • Android编程实现自定义手势的方法详解

    2023-09-14 16:02:36
  • SpringBoot集成gRPC微服务工程搭建实践的方法

    2022-03-11 22:10:39
  • c#入门之实现简易存款利息计算器示例

    2023-12-15 06:54:36
  • Android获取手机系统版本等信息的方法

    2023-02-05 20:36:08
  • Android Studio常用快捷键功能说明

    2023-02-20 04:44:17
  • 使用adb or fastboot命令进入高通的9008(edl)模式的两种方法

    2023-02-02 15:50:49
  • C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法

    2021-06-06 04:59:48
  • Java实现查找当前字符串最大回文串代码分享

    2023-07-30 04:05:02
  • Unity键盘WASD实现物体移动

    2021-06-14 13:46:21
  • Unity计时器功能实现示例

    2022-03-08 20:02:39
  • java并发编程工具类JUC之ArrayBlockingQueue

    2023-07-04 21:02:20
  • Java源码解析之可重入锁ReentrantLock

    2021-12-28 22:32:40
  • 用C#生成不重复的随机数的代码

    2023-03-08 03:36:52
  • asp之家 软件编程 m.aspxhome.com