最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

在重寫(xiě)類UsernamePasswordAuthenticationFilter時(shí)拋出了這個(gè)異常,字面上理解是authenticationManager不明確,所以要顯示的注入。

有兩個(gè)地方要改下

首先要在配置文件重寫(xiě)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)文章

最新評(píng)論

金塔县| 旅游| 红原县| 汉中市| 巍山| 岑溪市| 黎川县| 深圳市| 巍山| 枞阳县| 隆化县| 万山特区| 罗源县| 大城县| 东源县| 澜沧| 澜沧| 桐柏县| 武城县| 云林县| 莱州市| 枞阳县| 东乌珠穆沁旗| 武隆县| 临清市| 隆子县| 米易县| 都兰县| 凤阳县| 孙吴县| 夏河县| 台中市| 多伦县| 庄浪县| 阜阳市| 彩票| 黄冈市| 伽师县| 谷城县| 彰化县| 儋州市|