当前位置:  开发笔记 > 程序员 > 正文

用https固定测试弹簧支架控制器

如何解决《用https固定测试弹簧支架控制器》经验,为你挑选了1个好方法。

任何人都可以为我提供一个代码示例来为使用HTTPS保护的控制器编写集成测试吗?使用HTTP我能够写,但使用HTTPS我得到认证错误.

调节器

@RestController
@RequestMapping("/rest/otm/v1")
public class LoginController {  

    @Autowired
    UserAuthenticationService userAuthService;

    @ExceptionHandler({ AuthenticationFailedException.class})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public void login(HttpServletRequest request,HttpServletResponse response) throws AuthenticationDeniedException {

        //some code
    }

}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0") 
public class IdentityControllerTest {

    @Value("${local.server.port}")
    int port;

    @Test
    public void test_Login_Controller() {

        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("some-proxy", 8080));
        clientHttpRequestFactory.setProxy(proxy);

        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("credential", "pratap");
        requestHeaders.set("deviceid", "xyz123");

        HttpHeaders response = restTemplate.postForObject("https://localhost:"+port+"/rest/otm/v1/login", requestHeaders, HttpHeaders.class);               

    }

}

错误

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://localhost:51184/rest/otm/v1/login":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:607)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:357)
    at com.bosch.inl.otm.controller.IdentityControllerTest.test_Login_Controller(IdentityControllerTest.java:45)

提前致谢



1> Guy Bouallet..:

如果使用自签名证书,通常会报告此错误.默认情况下,Java不信任它,因此您必须禁用证书验证或将证书添加到信任库.这两种解决方案都在这里描述.

推荐阅读
低调pasta_730
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有