SpringSecurity報(bào)錯(cuò)authenticationManager must be spec的解決
在重寫類UsernamePasswordAuthenticationFilter時(shí)拋出了這個(gè)異常,字面上理解是authenticationManager不明確,所以要顯示的注入。
有兩個(gè)地方要改下
首先要在配置文件重寫authenticationManager

@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}然后在過(guò)濾器里面顯示的設(shè)置一下

@Autowired
@Override
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}順便貼一下兩個(gè)類的完整代碼
僅供參考:
過(guò)濾器
@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 {
//獲取表單提交數(shù)據(jù)
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();
}
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot2.x 集成 Thymeleaf的詳細(xì)教程
本文主要對(duì)SpringBoot2.x集成Thymeleaf及其常用語(yǔ)法進(jìn)行簡(jiǎn)單總結(jié),其中SpringBoot使用的2.4.5版本。對(duì)SpringBoot2.x 集成 Thymeleaf知識(shí)感興趣的朋友跟隨小編一起看看吧2021-07-07
Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor
這篇文章主要為大家介紹了Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Spring Security注解方式權(quán)限控制過(guò)程
這篇文章主要介紹了Spring Security注解方式權(quán)限控制過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
詳解JAVA中ListIterator和Iterator的辨析
這篇文章主要為大家詳細(xì)介紹了JAVAListIterator和Iterator的辨析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形
這篇文章主要介紹了使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形,可作為一些CLI程序界面的基礎(chǔ)部分,需要的朋友可以參考下2016-03-03
使用Spring?Boot快速構(gòu)建一個(gè)簡(jiǎn)單的文件處理工具
在現(xiàn)代Web應(yīng)用中,文件上傳與處理是常見的需求,本文將通過(guò)一個(gè)實(shí)際案例,詳細(xì)介紹如何使用Spring?Boot構(gòu)建一個(gè)文件處理工具,感興趣的小伙伴可以參考一下2025-06-06

