Ver
https://stackoverflow.com/questions/9901248/reading-client-certificate-in-servlet
https://self-learning-java-tutorial.blogspot.com/2017/12/how-to-get-client-certificate-from.html
(este úlyimo no me funciona con vaadin)
Veamos el código en VAADIN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | package com.vaadin.starter.skeleton; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.util.Date; import com.vaadin.flow.component.Key; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.html.Anchor; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.router.Route; import com.vaadin.flow.server.PWA; import com.vaadin.flow.server.StreamRegistration; import com.vaadin.flow.server.StreamResource; import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; /** * The main view contains a button and a click listener. */ @Route("") //@PWA(name = "Project Base for Vaadin", shortName = "Project Base") @CssImport("./styles/shared-styles.css") @CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field") public class MainView extends VerticalLayout { private Anchor anchor; private TextField textField; X509Certificate cert=null; //private StreamResource resource=null; public MainView() { cert=this.extractCertificate(); if (cert!=null)Notification.show("Cert:"+ cert.getBasicConstraints() + "\n" + cert.getIssuerDN() + "\n" + cert.getNotAfter() + "\n" + cert.getNotBefore() + "\n" + cert.getSubjectDN() + "\n" ); // Use TextField for standard text input boolean isValidCert=true; if(cert!=null) { try { cert.checkValidity(); } catch (Exception e) { isValidCert=false; } } if (!isValidCert) Notification.show("Certificado no válido"); else { textField = new TextField("Your name"); //IMPORTANT !!!! ficar este codi anchor=new Anchor(); anchor.getStyle().set("display", "none"); anchor.setTarget("_blank"); //FI IMPORTANT !!!! ficar este codi // Button click listeners can be defined as lambda expressions GreetService greetService = new GreetService(); Button button = new Button("Say hello", e -> Notification.show(greetService.greet(textField.getValue()))); Button button1 = new Button("Download_I_abandona Pagina", event -> { boolean isCheckPassed = true; if (!isCheckPassed) { Notification.show("Unfortunately you can not download this file"); } else { final StreamResource resource = new StreamResource(textField.getValue(), //() -> new ByteArrayInputStream("foo".getBytes())); () -> { try { return new FileInputStream("C:/Myresources/"+textField.getValue()); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; }); final StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource); UI.getCurrent().getPage().setLocation(registration.getResourceUri()); if (cert!=null)System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"<-->"+textField.getValue()+" -------> "+ cert.getSubjectDN()); } }); Button button2 = new Button("download_BO", event -> forceDownload()); // Theme variants give you predefined extra styles for components. // Example: Primary button is more prominent look. button.addThemeVariants(ButtonVariant.LUMO_PRIMARY); // You can specify keyboard shortcuts for buttons. // Example: Pressing enter in this view clicks the Button. button.addClickShortcut(Key.ENTER); // Use custom CSS classes to apply styling. This is defined in shared-styles.css. addClassName("centered-content"); add(textField, button, button1, button2, anchor); } } /* Este es el BO !!!!!*/ protected void forceDownload() { StreamResource resource= new StreamResource(textField.getValue(), () -> { try { return new FileInputStream("C:/MyResources/"+textField.getValue( ) ); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return null; }); this.anchor.setHref(resource); // downloadWidget is an Anchor // //UI.getCurrent().getPage().executeJavaScript("$0.click();", this.downloadWidget.getElement()); UI.getCurrent().getPage().executeJs("$0.click();", anchor.getElement()); if (cert!=null)System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"<-->"+textField.getValue()+" -------> "+ cert.getSubjectDN()); } //protected X509Certificate extractCertificate(HttpServletRequest req) { protected X509Certificate extractCertificate() { VaadinRequest req= VaadinService.getCurrentRequest(); X509Certificate[] certs = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate"); if (null != certs && certs.length > 0) { System.out.println("Cert:"+ certs[0].getBasicConstraints() + "\n" + certs[0].getIssuerDN() + "\n" + certs[0].getNotAfter() + "\n" + certs[0].getNotBefore() + "\n" + certs[0].getSubjectDN() + "\n" ); Notification.show("Cert:"+ certs[0].getBasicConstraints() + "\n" + certs[0].getIssuerDN() + "\n" + certs[0].getNotAfter() + "\n" + certs[0].getNotBefore() + "\n" + certs[0].getSubjectDN() + "\n" ); return certs[0]; } //throw new RuntimeException("No X.509 client certificate found in request"); return null; } } |
No hay comentarios :
Publicar un comentario