2013-06-29 3 views
3

Grizzly 2.3 기반의 JAX-RS 웹 서비스를 실행하는 서버에서 클라이언트 x.509 인증서의 CN 필드를 확인하고 싶습니다. . Grizzly 1에 대한 몇 가지 예를 발견했지만 코드가 크게 변경된 것으로 보입니다. 여기에 코드가 있습니다 :Grizzly 2.3에서 X.509 클라이언트 인증서를 얻는 방법 (클라이언트 인증을 사용할 때)

class Transport { 
    public static void main(String[] args){ 
     ResourceConfig rc = new PackagesResourceConfig(Transport.class.getPackage().getName());  
     String url = "http://myhost:8080/myURL";   
     URI uri = UriBuilder.fromUri(url).build(); 
     HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc); 
     SSLContextConfigurator sslContext = new SSLContextConfigurator(); 
     sslContext.setKeyStoreFile("path_to_my_keystore"); 
     sslContext.setKeyStorePass("password"); 
     sslContext.setTrustStoreFile("path_to_my_truststore"); 
     sslContext.setTrustStorePass("password"); 
     sslContext.setSecurityProtocol("TLSv1.2"); 
     SSLEngineConfigurator sslEngineConfigurator = new SSLEngineConfigurator(sslContext); 
     sslEngineConfigurator.setNeedClientAuth(true); 
     sslEngineConfigurator.setWantClientAuth(true); 
     sslEngineConfigurator.setEnabledCipherSuites(new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"}); 
     sslEngineConfigurator.setClientMode(false); 
     NetworkListener listener = new NetworkListener("grizzly_ssl", uri.getHost(), 8443); 
     listener.setSecure(true); 
     listener.setSSLEngineConfig(sslEngineConfigurator); 
     server.addListener(listener); 
    } 

    @POST 
    @Produces({ MediaType.APPLICATION_XML }) 
    @Consumes({ MediaType.APPLICATION_XML }) 
    @Path("foo") 
    public Response receivePayload(MyPayload payload) { 
     // How can I get the CN of the client x.509 certificate/or the fingerprint of the certificate? 
    } 
} 

답변

2

나는 대답을 찾았습니다 [1] [2].

[1] How to grab a pki certificate with Jersey/Spring? [2] 다음은

Grizzly and ServletContainerContext은 (안 저지 2 만 저지 1.1) 작동 코드 : 뉴저지 2 잘

class Transport { 
    public static void main(String[] args){ 
     ResourceConfig rc = new PackagesResourceConfig(Transport.class.getPackage().getName());  
     String url = "http://myhost:8080/myURL";   
     URI uri = UriBuilder.fromUri(url).build(); 
     HttpServer server = GrizzlyServerFactory.createHttpServer(uri, new HttpHandler() { 
      @Override 
      public void service(Request request, org.glassfish.grizzly.http.server.Response response) throws Exception { 
       response.setStatus(404, "Not found"); 
       response.getWriter().write("404: not found"); 
      } 
     }); 

     // Initialize and register Jersey Servlet 
     WebappContext context = new WebappContext("WebappContext", ""); 
     ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class); 
     registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, 
      ClassNamesResourceConfig.class.getName()); 
     registration.setInitParameter(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, Transport.class.getName()); 
     registration.addMapping("/*"); 
     context.deploy(server); 


     SSLContextConfigurator sslContext = new SSLContextConfigurator(); 
     sslContext.setKeyStoreFile("path_to_my_keystore"); 
     sslContext.setKeyStorePass("password"); 
     sslContext.setTrustStoreFile("path_to_my_truststore"); 
     sslContext.setTrustStorePass("password"); 
     sslContext.setSecurityProtocol("TLSv1.2"); 
     SSLEngineConfigurator sslEngineConfigurator = new SSLEngineConfigurator(sslContext); 
     sslEngineConfigurator.setNeedClientAuth(true); 
     sslEngineConfigurator.setWantClientAuth(true); 
     sslEngineConfigurator.setEnabledCipherSuites(new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"}); 
     sslEngineConfigurator.setClientMode(false); 
     NetworkListener listener = new NetworkListener("grizzly_ssl", uri.getHost(), 8443); 
     listener.setSecure(true); 
     listener.setSSLEngineConfig(sslEngineConfigurator); 
     server.addListener(listener); 
    } 

    @POST 
    @Produces({ MediaType.APPLICATION_XML }) 
    @Consumes({ MediaType.APPLICATION_XML }) 
    @Path("foo") 
    public Response receivePayload(@Context HttpServletRequest request, MyPayload payload) { 
     X509Certificate[] certChain = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); 
     if (certChain == null || certChain.length == 0){ 
     LOG.info("X509cert not found"); 
      return null; 
     } 
     X509Certificate certificate = certChain[0]; 
     // get information such as CN from certificate 
    } 
} 
+0

작품 – Hank