使用Spring Security配置Spring Boot会因为引用缺少的依赖性而导致构建失败

Cha*_*had 6 java spring spring-security maven

每当尝试运行时mvn install,在Spring Boot项目上构建都会因以下原因而失败:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.3:在项目wave上编译(default-compile):致命错误编译:java.lang.RuntimeException:com.sun.tools.javac.code.符号$ CompletionFailure:未找到org.springframework.security.ldap.DefaultSpringSecurityContextSource的类文件 - > [帮助1]

有两件事解决了这个问题:

  1. 删除以下安全配置

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
      @Inject
      private UserDetailsService userDetailsService;
    
      @Inject
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
          .userDetailsService(userDetailsService);
      }
    
      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http
          .httpBasic()
            .realmName("Wave")
            .and()
          .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/wave/service/employees/**").anonymous()
            .antMatchers("/wave/service/**").authenticated()
            .and()
          .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
      }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

对于coruse,删除配置不是一个选项,因为它会禁用我的应用程序的安全性

  1. 添加以下依赖项

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)

但是,在添加它时,错误消失了,出现了一个新的类似错误(仅指向另一个类):

无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.3:在项目wave上编译(default-compile):致命错误编译:java.lang.RuntimeException:com.sun.tools.javac.code.符号$ CompletionFailure:未找到org.springframework.security.openid.OpenIDAttribute的类文件 - > [帮助1]

再次修复此问题,我添加了以下依赖项:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-openid</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

这再次解决了问题,但出现了另一个错误:

无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.3:在项目wave上编译(default-compile):致命错误编译:java.lang.RuntimeException:com.sun.tools.javac.code.符号$ CompletionFailure:未找到org.apache.http.Header的类文件 - > [帮助1]

最后,添加以下依赖项修复了所有问题:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

但是,我现在有多个我不使用的依赖项.

有没有其他方法可以解决这个问题?

小智 6

我通过更换解决了这个问题

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
   <version>2.6.4</version>
   <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>5.0.12.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我认为org.eclipse.persistence.jpa.modelgen.processor中存在这个问题


小智 1

删除你的依赖并添加它;删除后这对我来说效果很好

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

'

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
Run Code Online (Sandbox Code Playgroud)

这是我的 pom.xml 文件

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>

        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>            
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.0.9.RELEASE</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)