Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.5.3</version>
  5. </dependency>
  6.  
  7. private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
  8. requireNonNull(targetUri);
  9. requireNonNull(loginPath);
  10.  
  11. // Keep track of cookies we might receive in an HttpClient:
  12. final CookieStore cookies = new BasicCookieStore();
  13.  
  14. // Build and work with an (AutoCloseable) HttpClient that uses the cookie store:
  15. try (CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookies).build()) {
  16.  
  17. // Prepare (login) request parameters:
  18. List<NameValuePair> reqParams = new ArrayList<>();
  19. if (params != null) {
  20. for (Map.Entry<String, String> entry : params.entrySet()) {
  21. reqParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  22. }
  23. }
  24.  
  25. // Execute the login (POST) request with the given parameters:
  26. HttpPost post = new HttpPost(targetUri + loginPath);
  27. post.setEntity(new UrlEncodedFormEntity(reqParams));
  28.  
  29. CloseableHttpResponse response = client.execute(post);
  30. // Eventually, check the response to see if successful
  31. response.close();
  32.  
  33. // Look for a JSESSIONID-named cookie stored in the HttpClient and return it to be used by calling code:
  34. for (org.apache.http.cookie.Cookie cookie : cookies.getCookies()) {
  35. if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
  36.  
  37. String domain = targetUri.getHost();
  38. if (domain.startsWith("www.")) {
  39. domain = domain.substring(4);
  40. }
  41.  
  42. Cookie authCookie = new Cookie(cookie.getName(), cookie.getValue());
  43. authCookie.setDomain(domain);
  44. authCookie.setPath("/");
  45. // Eventually, set expiry (to allow longer login) and other things...
  46.  
  47. return authCookie;
  48. }
  49. }
  50.  
  51. return null; // some sort of error?
  52. }
  53. }
  54.  
  55. @Title("Backoffice for SO Question #42927030")
  56. public class MainUI extends UI {
  57.  
  58. private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
  59. // ...
  60. }
  61.  
  62. @Override
  63. protected void init(VaadinRequest vaadinRequest) {
  64. setContent(new VerticalLayout(new Button("Log into site...", event -> {
  65.  
  66. try {
  67. URI targetUri = new URI("http://localhost:8080");
  68.  
  69. Map<String, String> params = new HashMap<>();
  70. params.put("username", "Johnny");
  71. params.put("password", "incorrect :)");
  72. // Eventual hidden fields, etc.
  73. // params.put("...", "...");
  74.  
  75. Cookie targetAuthCookie = login(targetUri, "/log-me-in", params);
  76.  
  77. // We're not ready just yet: we still need to 'transfer' the cookie
  78. // the HTTP client received to the current browser:
  79. VaadinService.getCurrentResponse().addCookie(targetAuthCookie);
  80.  
  81. // Upon responding to the Vaadin 'click' request, open the target URL (eventually in a new page / tab):
  82. Page.getCurrent().getJavaScript().execute("window.open('" + targetUri + "');");
  83.  
  84. } catch (Exception ex) {
  85. ex.printStackTrace();
  86. }
  87.  
  88. })));
  89. }
  90.  
  91. @WebServlet(urlPatterns = "/*", name = "MainUIServlet", asyncSupported = true)
  92. @VaadinServletConfiguration(ui = MainUI.class, productionMode = false)
  93. public static class MainUIServlet extends VaadinServlet {}
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement