欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringSecurity報(bào)錯(cuò)authenticationManager must be spec的解決

 更新時(shí)間:2022年11月17日 10:15:14   作者:小樓夜聽雨QAQ  
這篇文章主要介紹了SpringSecurity報(bào)錯(cuò)authenticationManager must be spec的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

在重寫類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ì)教程

    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
  • Java多線程ForkJoinPool實(shí)例詳解

    Java多線程ForkJoinPool實(shí)例詳解

    這篇文章主要介紹了Java多線程ForkJoinPool實(shí)例詳解,涉及forkjoin框架的相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-09-09
  • java異步寫日志到文件中實(shí)現(xiàn)代碼

    java異步寫日志到文件中實(shí)現(xiàn)代碼

    這篇文章主要介紹了java異步寫日志到文件中實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor

    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ò)程

    這篇文章主要介紹了Spring Security注解方式權(quán)限控制過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 詳解JAVA中ListIterator和Iterator的辨析

    詳解JAVA中ListIterator和Iterator的辨析

    這篇文章主要為大家詳細(xì)介紹了JAVAListIterator和Iterator的辨析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • MyBatis通用的10種寫法總結(jié)大全

    MyBatis通用的10種寫法總結(jié)大全

    這篇文章主要給大家介紹了關(guān)于MyBatis通用的10種寫法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Java集合框架之Collection接口詳解

    Java集合框架之Collection接口詳解

    這篇文章主要為大家詳細(xì)介紹了Java集合框架之Collection接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形

    使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形

    這篇文章主要介紹了使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形,可作為一些CLI程序界面的基礎(chǔ)部分,需要的朋友可以參考下
    2016-03-03
  • 使用Spring?Boot快速構(gòu)建一個(gè)簡(jiǎn)單的文件處理工具

    使用Spring?Boot快速構(gòu)建一個(gè)簡(jiǎn)單的文件處理工具

    在現(xiàn)代Web應(yīng)用中,文件上傳與處理是常見的需求,本文將通過(guò)一個(gè)實(shí)際案例,詳細(xì)介紹如何使用Spring?Boot構(gòu)建一個(gè)文件處理工具,感興趣的小伙伴可以參考一下
    2025-06-06

最新評(píng)論