Advertisement
Guest User

Untitled

a guest
Aug 28th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.ServletOutputStream;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.ws.rs.core.UriBuilder;
  10.  
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13.  
  14. @WebServlet("/openid-redirect")
  15. public class RedirectServlet extends HttpServlet {
  16.    
  17.     private final Logger logger = LoggerFactory.getLogger(getClass());
  18.    
  19.     // TODO remove after test.
  20.    
  21.     public static final String PATH = "/openid-redirect";
  22.    
  23.     public static final String PARAM_REDIRECT_URI = "u";
  24.    
  25.     public static final String PARAM_MODE = "mode";
  26.    
  27.     public static final String MODE_302_RESPONSE = "1";
  28.     public static final String MODE_JAVASCRIPT_LOCATION = "2";
  29.     public static final String MODE_META_REFRESH = "3";
  30.     public static final String MODE_LINK = "4";
  31.     public static final String MODE_CHAIN_JS_AND_302 = "5";
  32.     public static final String MODE_LINK_AUTOCLICK = "6";
  33.    
  34.     @Override
  35.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  36.             throws ServletException, IOException {
  37.         String uri = request.getParameter(PARAM_REDIRECT_URI);
  38.         String mode = request.getParameter(PARAM_MODE);
  39.        
  40.         logger.info("doGet: URI: {}", uri);
  41.        
  42.         if (uri == null) {
  43.             response.sendError(400, "No redirect URI");
  44.         }
  45.        
  46.         else if (mode == null || MODE_302_RESPONSE.equals(mode)) {
  47.             response.sendRedirect(uri);
  48.         }
  49.        
  50.         else if (MODE_JAVASCRIPT_LOCATION.equals(mode)) {
  51.             response.addHeader("Content-Type", "text/html; charset=utf8");
  52.             try (ServletOutputStream out = response.getOutputStream()) {
  53.                 // out.print("<!DOCTYPE html><html><body onload=\"window.location = '" + uri + "'\"></body></html>");
  54.                 out.print(String.format("<!DOCTYPE html><html><body><script type=\"text/javascript\">setTimeout(\"location.href = '%s';\",5000);</script>Redirecting in 5 seconds with location.href=uri</body></html>", uri));
  55.                 out.flush();
  56.             }
  57.         }
  58.        
  59.         else if (MODE_META_REFRESH.equals(mode)) {
  60.             response.addHeader("Content-Type", "text/html; charset=utf8");
  61.             try (ServletOutputStream out = response.getOutputStream()) {
  62.                 out.print("<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"5; url=" + uri
  63.                         + "\"></head><body>Meta refresh test: Redirecting in 5 seconds to " + uri + "</body></html>");
  64.                 out.flush();
  65.             }
  66.         }
  67.        
  68.         else if (MODE_CHAIN_JS_AND_302.equals(mode)) {
  69.             response.addHeader("Content-Type", "text/html; charset=utf8");
  70.             try (ServletOutputStream out = response.getOutputStream()) {
  71.                 String uri302 = UriBuilder.fromPath("").queryParam(PARAM_REDIRECT_URI, uri).queryParam(PARAM_MODE, MODE_302_RESPONSE).build().toString();
  72.                 out.print(String.format("<!DOCTYPE html><html><body><script type=\"text/javascript\">setTimeout(\"location.href = '%s';\",5000);</script>Redirecting in 5 seconds with location.href=uri to %s</body></html>", uri302, uri302));
  73.                 out.flush();
  74.             }
  75.         }
  76.        
  77.         else if (MODE_LINK.equals(mode)) {
  78.             response.addHeader("Content-Type", "text/html; charset=utf8");
  79.             try (ServletOutputStream out = response.getOutputStream()) {
  80.                 out.print("<!DOCTYPE html><html><head><body><a href=\"" + uri
  81.                         + "\">Click here to redirect</a></body></html>");
  82.                 out.flush();
  83.             }
  84.         }
  85.        
  86.         else if (MODE_LINK_AUTOCLICK.equals(mode)) {
  87.             response.addHeader("Content-Type", "text/html; charset=utf8");
  88.             try (ServletOutputStream out = response.getOutputStream()) {
  89.                 out.print("<!DOCTYPE html><html><head><body><a id=\"redirectlink\" href=\"" + uri
  90.                         + "\">Click here to redirect</a> <script type='text/javascript'> alert('hello'); setTimeout(\"document.getElementById('redirectlink').click();\",3000); </script> Javascript should click this link in 3 seconds  </body></html>");
  91.                 out.flush();
  92.             }
  93.         }
  94.        
  95.         else {
  96.             // Unknown mode
  97.             response.sendError(400, "Unknown mode");
  98.         }
  99.     }
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement