即使无法在包括用SpringMVC密钥库证书后进行身份验证服务器 [英] Unable to authenticate server even after including keystore certificate in SpringMVC
问题描述
我想验证到JIRA服务器,并在这里从服务器获取给定id问题的详细信息最初我正在给手动ID。我已经下载了服务器证书使用密钥工具导入,密码为密码密钥库。但我不能够验证到它下面是我的code
我的Java code:
@Controller
公共类服务{
@RequestMapping(/你好)
公共字符串数据(ModelMap模型)抛出KeyStoreException,抛出:NoSuchAlgorithmException,CertificateException,FileNotFoundException异常,IOException异常,KeyManagementException,UnrecoverableKeyException { 密钥库的keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(新的FileInputStream(/用户/ crohitk /文件/工作区/ FRR / publicKey1.store),
密码.toCharArray());
SSLConnectionSocketFactory的SocketFactory =新SSLConnectionSocketFactory(
新SSLContextBuilder()
.loadTrustMaterial(空,新TrustSelfSignedStrategy())
。.loadKeyMaterial(密钥库密码.toCharArray())建立());
HttpClient的HttpClient的= HttpClients.custom()setSSLSocketFactory(的SocketFactory).build();
ClientHtt prequestFactory requestFactory =新HttpComponentsClientHtt prequestFactory(
HttpClient的);
RestTemplate restTemplate =新RestTemplate(requestFactory);
ResponseEntity<串GT;结果= restTemplate.exchange(https://jira.example.com/,HttpMethod.GET,新HttpEntity<串GT;(createHeaders(用户,密码)),为String.class); model.addAttribute(信息,结果);
返回的HelloWorld; }
HttpHeaders createHeaders(字符串用户名,字符串密码){
HttpHeaders头=新HttpHeaders();
字符串AUTH =用户名+:+密码;
字节[] EN codedAuth = Base64.en codeBase64(auth.getBytes(Charset.forName(US-ASCII)));
字符串base64Creds =基本+新的String(EN codedAuth);
header.add(授权,基本+ base64Creds);
返回头;
}}
分配器 - servlet.xml中:
<豆的xmlns =http://www.springframework.org/schema/beans
的xmlns:上下文=http://www.springframework.org/schema/context
XMLNS:XSI =http://www.w3.org/2001/XMLSchema-instance
XSI:的schemaLocation =
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd\"> <背景:组件扫描基包=frr.frr/> <豆
类=org.springframework.web.servlet.view.InternalResourceViewResolver>
<属性名=preFIX>
< VALUE> / WEB-INF /视图/< /值>
< /性>
<属性名=后缀>
< VALUE>的.jsp< /值>
< /性>
< /豆>
< /豆>
web.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
< web应用程序的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns =http://java.sun.com/xml/ns/javaeeXSI:的schemaLocation = http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdID =WebApp_ID版本=3.0>
<显示-名称>原型创建Web应用程序和LT; /显示-名称> <&servlet的GT;
< servlet的名称>分派器LT; / servlet的名称>
<的servlet类>
org.springframework.web.servlet.DispatcherServlet
< / servlet的类>
&所述;负载上启动→1&下; /负载上启动>
< / servlet的> < Servlet映射>
< servlet的名称>分派器LT; / servlet的名称>
< URL模式> /< / URL模式>
< / Servlet映射> <的context-param>
<参数-名称>&contextConfigLocation的LT; /参数 - 名称>
&LT;参数值&GT; /WEB-INF/dispatcher-servlet.xml< /参数值&GT;
&LT; /的context-param&GT; &LT;听者GT;
&LT;监听级&GT;
org.springframework.web.context.ContextLoaderListener
&LT; /监听级&GT;
&LT; /听者GT;
&LT; / web-app的&GT;
pom.xml中:
&LT;项目的xmlns =http://maven.apache.org/POM/4.0.0的xmlns:XSI =http://www.w3.org/2001 / XML模式实例
XSI:的schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">
&LT; modelVersion&GT; 4.0.0&LT; / modelVersion&GT;
&LT;&的groupId GT;&FRR LT; /的groupId&GT;
&LT;&的artifactId GT;&FRR LT; / artifactId的&GT;
&LT;包装和GT;战争与LT; /包装&GT;
&LT;&版GT; 0.0.1-SNAPSHOT&LT; /版本&GT;
&LT;名称&gt; FRR的Maven的webapp&LT; /名称&gt;
&LT; URL&GT; HTTP://maven.apache.org< / URL&GT;
&LT;性状&gt;
&LT; spring.version&GT; 4.2.0.RELEASE&LT; /spring.version>
&LT; /性状&gt;
&LT;依赖和GT;
&LT;&依赖性GT;
&LT;&的groupId GT;的JUnit&LT; /的groupId&GT;
&LT;&的artifactId GT;的JUnit&LT; / artifactId的&GT;
&LT;&版GT; 3.8.1&LT; /版本&GT;
&LT;&范围GT;试验&LT; /&范围GT;
&LT; /依赖性&GT; &LT;&依赖性GT;
&LT;&的groupId GT; org.springframework&LT; /的groupId&GT;
&LT;&的artifactId GT;弹簧芯LT; / artifactId的&GT;
&LT;&版GT; $ {spring.version}&LT; /版本&GT;
&LT; /依赖性&GT; &LT;&依赖性GT;
&LT;&的groupId GT; org.springframework&LT; /的groupId&GT;
&LT;&的artifactId GT;春的Web&LT; / artifactId的&GT;
&LT;&版GT; $ {spring.version}&LT; /版本&GT;
&LT; /依赖性&GT; &LT;&依赖性GT;
&LT;&的groupId GT; org.springframework&LT; /的groupId&GT;
&LT;&的artifactId GT;弹簧webmvc&LT; / artifactId的&GT;
&LT;&版GT; $ {spring.version}&LT; /版本&GT;
&LT; /依赖性&GT;
&LT;&依赖性GT;
&LT;&的groupId GT; org.apache.httpcomponents&LT; /的groupId&GT;
&LT;&的artifactId GT;的HttpClient&LT; / artifactId的&GT;
&LT;&版GT; 4.3.5&LT; /版本&GT;
&LT; /依赖性&GT;
&LT;&依赖性GT;
&LT;&的groupId GT; commons- codeC&LT; /的groupId&GT;
&LT;&的artifactId GT; commons- codeC&LT; / artifactId的&GT;
&LT;&版GT; 1.9 LT; /版本&GT;
&LT; /依赖性&GT;
&LT; /依赖和GT;
&LT;建立&GT;
&LT; finalName&GT;&FRR LT; / finalName&GT;
&LT; /构建&GT;
&LT; /项目&GT;
$以上更改编码证书C $ C至低于code中的Java文件
有上述code
HttpHeaders createHeaders(){
字符串plainCreds =用户名:密码;
字节[] = plainCredsBytes plainCreds.getBytes();
字节[] = base64CredsBytes Base64.en codeBase64(plainCredsBytes);
字符串base64Creds =新的String(base64CredsBytes);
HttpHeaders标题=新HttpHeaders();
headers.add(授权,基本+ base64Creds);
返回头;
}
I am trying to authenticate to jira server and get issue details from the server given id here initially i am manually giving id. i have downloaded the server certificate and imported using keytool and the password is "password" to keystore. but i am not able to authenticate to it below is my code
my java code :
@Controller
public class Service{
@RequestMapping("/hello")
public String Data(ModelMap model) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyManagementException, UnrecoverableKeyException{
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream("/users/crohitk/Documents/workspace/frr/publicKey1.store"),
"password".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ResponseEntity<String> result = restTemplate.exchange("https://jira.example.com/", HttpMethod.GET, new HttpEntity<String>(createHeaders("user", "password")), String.class);
model.addAttribute("message", result);
return "helloworld";
}
HttpHeaders createHeaders( String username, String password ){
HttpHeaders header = new HttpHeaders();
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")) );
String base64Creds = "Basic " + new String( encodedAuth );
header.add("Authorization", "Basic " + base64Creds);
return header;
}
}
dispatcher-servlet.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="frr.frr" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>frr</groupId>
<artifactId>frr</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>frr Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<finalName>frr</finalName>
</build>
</project>
Change encoding credentials of above code to below code in java file there is some error in encoding of above code
HttpHeaders createHeaders(){
String plainCreds = "user:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
return headers;
}
这篇关于即使无法在包括用SpringMVC密钥库证书后进行身份验证服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!