2016-09-13 3 views
0

스프링 부트 1.4, 스팍 및 스프링 부트 스타터 보안을 사용 중입니다. 나는 또한 내 application-test.properties 파일이 추가TestRestTemplate 및 아파치로 인해 스프링 부트 통합이 시작되지 않음 SSL

springBootVersion = '1.4.0.BUILD-SNAPSHOT' 
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
compile('org.springframework.boot:spring-boot-starter-data-jpa') 
compile('org.springframework.boot:spring-boot-devtools') 
compile('org.springframework.boot:spring-boot-starter-security') 
compile('org.springframework.boot:spring-boot-starter-web') 
compile('org.jasypt:jasypt-spring31:1.9.2') 
compile('org.apache.httpcomponents:httpclient:4.5.2') 
testCompile('org.springframework.boot:spring-boot-starter-test') 
testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-2') 
testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-2') 

:

server.ssl.enabled=false 
spring.datasource.username=test 
spring.datasource.password=test 
spring.datasource.url=jdbc:h2:mem:testdb 
spring.datasource.driverClassName=org.h2.Driver 

나는 JWT를 생성하는 인증 된 엔드 포인트를 만들 수 있었다 (사용 jasypt 여기 내 관련 Gradle을 파일입니다 -spring31 : 1.9.2) Authorization 헤더에 반환합니다. 이 기능은 브라우저 나 우편 배달부를 통해 액세스 할 때 유용합니다. 그러나 스프링 부트 통합 테스트에서이 작업을 수행 할 수 없었습니다.

가 내 비밀번호는 암호화되어 있기 때문에 DB 구성 클래스가 수행

@Configuration 
public class DBConfig { 

    @Autowired 
    private Environment env; 

    @Value("${spring.datasource.driverClassName}") 
    private String databaseDriverClassName; 

    @Value("${spring.datasource.url}") 
    private String datasourceUrl; 

    @Value("${spring.datasource.username}") 
    private String databaseUsername; 

    @Value("${jasypt.key}") 
    private String jasyptKey; 

    @Bean 
    public DataSource datasource() throws IOException { 

     return DataSourceBuilder 
         .create() 
         .username(databaseUsername) 
         .password(getSecurePassword()) 
         .url(datasourceUrl) 
         .driverClassName(databaseDriverClassName) 
         .build(); 
    } 

    private String getSecurePassword() throws IOException { 
     StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
     encryptor.setPassword(jasyptKey); 
     Properties props = new EncryptableProperties(encryptor); 
     props.load(this.getClass().getClassLoader().getResourceAsStream("application-" + env.getActiveProfiles()[0] + ".properties")); 
     return props.getProperty("spring.datasource.password"); 
    } 
} 

내 기본 테스트 클래스 임베디드 톰캣 인스턴스를 시작하고 모든 엔드 포인트를 사용할 수 있도록하고, JWT를 얻을 수있는 방법이있다 할 것이다 :

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
@TestPropertySource("classpath:application-test.properties") 
class BaseSpecification extends Specification { 

    @Autowired 
    def TestRestTemplate restTemplate 

    String loginAndGetJWT() { 

     def model = new ModelMap(); 
     model.addAttribute("username", "ausername") 
     model.addAttribute("password", "apassword"); 

     ResponseEntity<JwtAuthenticationResponse> response = restTemplate.getForEntity("/auth", String.class, model); 
     def jwt = response.getBody().token 

     return jwt; 
    } 
} 

내 샘플 테스트 실행하려고하면 :

class ApplicationSpecWithoutAnnotation extends BaseSpecification { 

    @Autowired 
    WebApplicationContext context 

    def "should boot up without errors"() { 

     expect: "web application context exists" 
     context != null 
    } 
} 

내가 t 수를 this에 따르면

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.conn.ssl.SSLConnectionSocketFactory 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 

을 : 그는 다음 @SpringBootTest가 사용될 때마다 TestRestTemplate는 빈으로 사용할 수 있습니다. 에 다음 오류 변화를 내가 (': HttpClient를 4.5.2 org.apache.httpcomponents') : 컴파일을 제거하면

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients 

그래서 봄 웹 & 아파치에 대한 종속성이있을 나타납니다 그건 옳지 않은 것 같아.

+0

명시 적 종속성으로 'httpclient'가 필요하지 않다고 생각합니다. 클래스를 초기화하는 중에 오류가 발생했다는 것을 나타냅니다 (이 [link] (https://stackoverflow.com/questions/1457863/what-causes) 참조). 'NoClassDefFoundError'에 대한 설명은 noclassdeffounderror와 classn)의 차이점을 참조하십시오. 어떤 JDK 버전을 사용하고 있습니까? 'server.ssl.enabled = false'를'true'로 설정하거나 단순히 설정 키를 생략 할 수도 있습니다. –

+0

실제로 server.ssl.enabled = false이고 JDK1.8을 사용 중입니다. 내 build.gradle에서 "compile ('org.apache.httpcomponents : httpclient : 4.5.2')"줄을 제거하면 오류가 발생합니다. 원인 : java.lang.NoClassDefFoundError : org/apache/http/impl/클라이언트/HttpClients. 내가 이것을 기반으로하는 모든 봄 예제가 maven에 있기 때문에 이것이 gradle 문제인지 궁금하다. – sonoerin

+0

저는 이것이 Gradle 문제라고 생각하지 않습니다. Github에서 문제를 보여주는 작은 예제 프로젝트를 게시 할 수 있습니까? –

답변

1

포함 된 QBO 라이브러리에 불쾌한 http 라이브러리가있는 것으로 나타났습니다. 그 2.5 라이브러리에 대한 모든 참조를 제거하고 내 빌드가 수정되었습니다. 왜 그것이 내 gradley 의존성 트리에 나타나지 않았는지 모르지만 내가 가져온 항아리를 보았을 때 나는 그것을 발견했다.

+0

동일한 문제가 있습니다. 가져온 항아리는 어떻게 보입니까? Maven Btw를 사용하고 있습니다. –

관련 문제