无法解析org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter类型

时间:2018-06-29 11:39:12

标签: java spring spring-boot spring-security

我一直在尝试学习Spring安全性。以下代码来自Spring.io,我使用了相同的pom.xml文件和spring boot版本。

package com.demo.hello;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
             User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

仍然Iam在configure方法中出现以下错误

The type org.springframework.security.web.authentication.UsernamePassw
     ordAuthenticationFilter cannot be resolved. It is indirectly 
     referenced from required .class files


当我使用spring-boot-1.5.14时没有显示错误,但是它不适用于spring-boot-2.0.3,并且Spring.io的代码使用2.0.3版本。 我无法弄清楚出了什么问题

2 个答案:

答案 0 :(得分:0)

我通过从

删除以下文件夹解决了该问题

maven存储库并更新了项目

  1. .m2 \ repository \ org \ springframework \ security \ spring-security-config \ 5.0.6.RELEASE
  2. .m2 \ repository \ org \ springframework \ security \ spring-security-web \ 5.0.6RELEASE
  3. .m2 \ repository \ org \ springframework \ security \ spring-security-core \ 5.0.6RELEASE
  4. .m2 \ repository \ org \ springframework \ boot \ spring-boot-starter-security \ 2.0.3.RELEASE

答案 1 :(得分:0)

同一个问题。 就我而言,该问题是由于“ spring-boot-starter-security”依赖性不匹配造成的。添加了具有兼容版本的新“ spring-boot-starter-security”

出于兼容性,我的意思是其他<groupId>org.springframework.boot</groupId>依赖项的版本。

希望这可能对某人有所帮助!