コード例 #1
0
ファイル: Bootstrap.java プロジェクト: guanxi/guanxi-idp
  /** Initialise the interceptor */
  public void init() {
    try {
      /* If we try to add the BouncyCastle provider but another Guanxi::SP running
       * in another webapp in the same container has already done so, then we'll get
       * -1 returned from the method, in which case, we should leave unloading of the
       * provider to the particular Guanxi::SP that loaded it.
       */
      if ((Security.addProvider(new BouncyCastleProvider())) != -1) {
        // We've loaded it, so we should unload it
        okToUnloadBCProvider = true;
      }

      IdpDocument configDoc =
          IdpDocument.Factory.parse(new File(servletContext.getRealPath(configFile)));
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_CONFIG_DOC, configDoc);
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_CONFIG, configDoc.getIdp());

      // Sort out the cookie's age
      int cookieAge = -1;
      String cookieMaxAge = configDoc.getIdp().getCookie().getAge().getStringValue();
      String cookieAgeUnits = configDoc.getIdp().getCookie().getAge().getUnits().toString();
      if (cookieAgeUnits.equals("seconds")) cookieAge = Integer.parseInt(cookieMaxAge);
      else if (cookieAgeUnits.equals("minutes")) cookieAge = Integer.parseInt(cookieMaxAge) * 60;
      else if (cookieAgeUnits.equals("hours")) cookieAge = Integer.parseInt(cookieMaxAge) * 3600;
      else if (cookieAgeUnits.equals("days")) cookieAge = Integer.parseInt(cookieMaxAge) * 86400;
      else if (cookieAgeUnits.equals("weeks")) cookieAge = Integer.parseInt(cookieMaxAge) * 604800;
      else if (cookieAgeUnits.equals("months"))
        cookieAge = Integer.parseInt(cookieMaxAge) * 2419200;
      else if (cookieAgeUnits.equals("years"))
        cookieAge = Integer.parseInt(cookieMaxAge) * 29030400;
      else if (cookieAgeUnits.equals("transient")) cookieAge = -1;

      String cookieDomain =
          (configDoc.getIdp().getCookie().getDomain() == null)
              ? ""
              : configDoc.getIdp().getCookie().getDomain();

      // Register the IdP's ID and cookie details in case we're embedded
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_ID, configDoc.getIdp().getID());
      servletContext.setAttribute(
          Guanxi.CONTEXT_ATTR_IDP_COOKIE_PREFIX, configDoc.getIdp().getCookie().getPrefix());
      servletContext.setAttribute(
          Guanxi.CONTEXT_ATTR_IDP_COOKIE_NAME,
          configDoc.getIdp().getCookie().getPrefix() + configDoc.getIdp().getID());
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_COOKIE_DOMAIN, cookieDomain);
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_COOKIE_AGE, new Integer(cookieAge));

      setup();

      startJobs();
    } catch (Exception e) {
    }
  }
コード例 #2
0
ファイル: Bootstrap.java プロジェクト: guanxi/guanxi-idp
  /**
   * Called by Spring when application events occur. At the moment we handle: ContextClosedEvent
   * ContextRefreshedEvent RequestHandledEvent
   *
   * <p>This is where we inject the job controllers into the application context, each one under
   * it's own key.
   *
   * @param applicationEvent Spring application event
   */
  public void onApplicationEvent(ApplicationEvent applicationEvent) {
    if (applicationEvent instanceof ContextRefreshedEvent) {
      logger.info("Bootstrap init");

      // Inject the metadata farm to handle all source of metadata
      servletContext.setAttribute(Guanxi.CONTEXT_ATTR_IDP_ENTITY_FARM, entityFarm);
    }

    if (applicationEvent instanceof ContextClosedEvent) {
      if (okToUnloadBCProvider) {
        Provider[] providers = Security.getProviders();

        /* Although addProvider() returns the ID of the newly installed provider,
         * we can't rely on this. If another webapp removes a provider from the list of
         * installed providers, all the other providers shuffle up the list by one, thus
         * invalidating the ID we got from addProvider().
         */
        try {
          for (int i = 0; i < providers.length; i++) {
            if (providers[i].getName().equalsIgnoreCase(Guanxi.BOUNCY_CASTLE_PROVIDER_NAME)) {
              Security.removeProvider(Guanxi.BOUNCY_CASTLE_PROVIDER_NAME);
            }
          }

          // Stop the jobs
          scheduler.shutdown();
        } catch (SecurityException se) {
          /* We'll end up here if a security manager is installed and it refuses us
           * permission to remove the BouncyCastle provider
           */
        } catch (SchedulerException se) {
          logger.error("Could not stop jobs", se);
        }
      }
    }

    if (applicationEvent instanceof RequestHandledEvent) {}
  }