예제 #1
0
 /**
  * Creates, sends (to the given ServletResponse), and returns a TicketGrantingTicket for the given
  * username.
  *
  * @param username username
  * @param request request
  * @param response response
  * @return TicketGrantingTicket
  * @throws ServletException ServletException
  * @throws UnsupportedEncodingException UnsupportedEncodingException
  */
 private TicketGrantingTicket sendTgc(
     final String username, final HttpServletRequest request, final HttpServletResponse response)
     throws ServletException, UnsupportedEncodingException {
   try {
     TicketGrantingTicket t = new TicketGrantingTicket(InfoBase64Coding.encrypt(username));
     String token = tgcCache.addTicket(t);
     Cookie tgc = new Cookie(TGC_ID, token);
     tgc.setSecure(false);
     tgc.setMaxAge(
         StringUtils.isNotEmpty(keepLogin)
             ? Integer.parseInt(app.getInitParameter("UNCookieTimeOut"))
             : -1);
     tgc.setPath("/");
     tgc.setDomain(DomainConstant.DOMAIN);
     response.addCookie(tgc);
     writeCookieWithName(username, response);
     return t;
   } catch (TicketException ex) {
     throw new ServletException(ex.toString());
   }
 }
예제 #2
0
  /**
   * Grants a service ticket for the given service, using the given TicketGrantingTicket. If no
   * 'service' is specified, simply forward to message conveying generic success.
   *
   * @param request request
   * @param response response
   * @param t t
   * @param serviceId serviceId
   * @param first first
   * @throws ServletException ServletException
   * @throws IOException IOException
   */
  private void grantForService(
      final HttpServletRequest request,
      final HttpServletResponse response,
      final TicketGrantingTicket t,
      final String serviceId,
      final boolean first)
      throws ServletException, IOException {
    try {
      String actualServiceId = serviceId != null ? serviceId : "http://www.lvmama.com/";
      if (actualServiceId.contains("&")) {
        // log("service=="+actualServiceId);
        actualServiceId = actualServiceId.replaceAll("&", "&");
      }
      ServiceTicket st = new ServiceTicket(t, actualServiceId, first);
      String token = stCache.addTicket(st);
      request.setAttribute("serviceId", actualServiceId);
      request.setAttribute("token", token);
      if (!first) {
        if (privacyRequested(request)) {
          app.getRequestDispatcher(confirmService).forward(request, response);
        } else {
          request.setAttribute("first", "false");
          Cookie unCookie = null;
          Cookie[] cookies = request.getCookies();
          if (null != cookies) {
            for (int i = 0; i < cookies.length; i++) {
              if (cookies[i].getName().equals("UN")) {
                unCookie = cookies[i];
              }
            }
          }
          if (null == unCookie) {
            unCookie = new Cookie("UN", null);
            unCookie.setDomain(DomainConstant.DOMAIN);
            unCookie.setMaxAge(
                StringUtils.isNotEmpty(keepLogin)
                    ? Integer.parseInt(app.getInitParameter("UNCookieTimeOut"))
                    : -1);
            unCookie.setPath("/");

            unCookie.setValue(
                URLEncoder.encode(new String(InfoBase64Coding.decrypt(t.getUsername())), "UTF-8"));

            response.addCookie(unCookie);
          }

          app.getRequestDispatcher(serviceSuccess).forward(request, response);
        }
      } else {
        request.setAttribute("first", "true");
        Cookie unCookie = null;
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
          for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("UN")) {
              unCookie = cookies[i];
            }
          }
        }
        if (null == unCookie) {
          unCookie = new Cookie("UN", null);
          unCookie.setDomain(DomainConstant.DOMAIN);
          unCookie.setMaxAge(
              StringUtils.isNotEmpty(keepLogin)
                  ? Integer.parseInt(app.getInitParameter("UNCookieTimeOut"))
                  : -1);
          unCookie.setPath("/");
          unCookie.setValue(
              URLEncoder.encode(new String(InfoBase64Coding.decrypt(t.getUsername())), "UTF-8"));
          response.addCookie(unCookie);
        }
        app.getRequestDispatcher(serviceSuccess).forward(request, response);
      }
    } catch (TicketException ex) {
      throw new ServletException(ex.toString());
    }
  }