org.apache.shiro.util.Factory类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.1k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中org.apache.shiro.util.Factory类的一些代码示例,展示了Factory类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Factory类的具体详情如下:
包路径:org.apache.shiro.util.Factory
类名称:Factory

Factory介绍

[英]Generics-aware interface supporting the Factory Method design pattern.
[中]支持Factory Method设计模式的泛型感知接口。

代码示例

代码示例来源:origin: apache/shiro

protected Object resolveReference(String reference) {
  String id = getId(reference);
  log.debug("Encountered object reference '{}'.  Looking up object with id '{}'", reference, id);
  final Object referencedObject = getReferencedObject(id);
  if (referencedObject instanceof Factory) {
    return ((Factory) referencedObject).getInstance();
  }
  return referencedObject;
}

代码示例来源:origin: killbill/killbill

public static Collection<Realm> get(final ConfigSource configSource) {
  final SecurityConfig securityConfig = new ConfigurationObjectFactory(configSource).build(SecurityConfig.class);
  Collection<Realm> realms = null;
  try {
    final Factory<SecurityManager> factory = new IniSecurityManagerFactory(securityConfig.getShiroResourcePath());
    // TODO Pierre hack - lame cast here, but we need to have Shiro go through its reflection magic
    // to parse the [main] section of the ini file. Without duplicating code, this seems to be possible only
    // by going through IniSecurityManagerFactory.
    final DefaultSecurityManager securityManager = (DefaultSecurityManager) factory.getInstance();
    realms = securityManager.getRealms();
  } catch (final ConfigurationException e) {
    log.warn("Unable to configure RBAC", e);
  }
  return realms != null ? realms :
      ImmutableSet.<Realm>of(new IniRealm(securityConfig.getShiroResourcePath())); // Mainly for testing
}

代码示例来源:origin: apache/shiro

SecurityManager securityManager = factory.getInstance();

代码示例来源:origin: apache/shiro

@BeforeClass
public static void beforeClass() {
  //0.  Build and set the SecurityManager used to build Subject instances used in your tests
  //    This typically only needs to be done once per class if your shiro.ini doesn't change,
  //    otherwise, you'll need to do this logic in each test that is different
  Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:test.shiro.ini");
  setSecurityManager(factory.getInstance());
}

代码示例来源:origin: apache/shiro

protected FilterChainResolver createFilterChainResolver() {
  FilterChainResolver resolver = null;
  Ini ini = getIni();
  if (!CollectionUtils.isEmpty(ini)) {
    //only create a resolver if the 'filters' or 'urls' sections are defined:
    Ini.Section urls = ini.getSection(IniFilterChainResolverFactory.URLS);
    Ini.Section filters = ini.getSection(IniFilterChainResolverFactory.FILTERS);
    if (!CollectionUtils.isEmpty(urls) || !CollectionUtils.isEmpty(filters)) {
      //either the urls section or the filters section was defined.  Go ahead and create the resolver:
      Factory<FilterChainResolver> factory = (Factory<FilterChainResolver>) this.objects.get(FILTER_CHAIN_RESOLVER_NAME);
      if (factory instanceof IniFactorySupport) {
        IniFactorySupport iniFactory = (IniFactorySupport) factory;
        iniFactory.setIni(ini);
        iniFactory.setDefaults(this.objects);
      }
      resolver = factory.getInstance();
    }
  }
  return resolver;
}

代码示例来源:origin: killbill/killbill

protected void configureShiro() {
    final Ini config = new Ini();
    config.addSection("users");
    config.getSection("users").put("pierre", "password, creditor");
    config.getSection("users").put("stephane", "password, refunder");
    config.addSection("roles");
    config.getSection("roles").put("creditor", Permission.INVOICE_CAN_CREDIT.toString() + "," + Permission.INVOICE_CAN_ITEM_ADJUST.toString());
    config.getSection("roles").put("refunder", Permission.PAYMENT_CAN_REFUND.toString());

    // Reset the security manager
    ThreadContext.unbindSecurityManager();

    final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config);
    final SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
  }
}

代码示例来源:origin: line/centraldogma

private static SecurityManager createSecurityManager(Ini config, Supplier<String> sessionIdGenerator) {
  final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config) {
    @Override
    protected SecurityManager createDefaultInstance() {
      final DefaultSessionManager sessionManager = new DefaultSessionManager();
      // This session DAO is required to cache the session in a very short time, especially while
      // logging in to the Central Dogma server. After that, the general session manager provided
      // by Central Dogma server will be working for the session management.
      sessionManager.setSessionDAO(new LimitedMemorySessionDAO(sessionIdGenerator,
                                   64, Duration.ofHours(1)));
      final DefaultSecurityManager securityManager = new DefaultSecurityManager();
      securityManager.setSessionManager(sessionManager);
      return securityManager;
    }
  };
  return factory.getInstance();
}

代码示例来源:origin: stackoverflow.com

Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = (SecurityManager) factory.getInstance();

代码示例来源:origin: org.apache.shiro/shiro-config-ogdl

protected Object resolveReference(String reference) {
  String id = getId(reference);
  log.debug("Encountered object reference '{}'.  Looking up object with id '{}'", reference, id);
  final Object referencedObject = getReferencedObject(id);
  if (referencedObject instanceof Factory) {
    return ((Factory) referencedObject).getInstance();
  }
  return referencedObject;
}

代码示例来源:origin: org.apache.camel/camel-shiro

public ShiroSecurityPolicy(Ini ini) {
  this();
  Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
  securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
}

代码示例来源:origin: org.apache.camel/camel-shiro

public ShiroSecurityPolicy(String iniResourcePath) {
  this();
  Factory<SecurityManager> factory = new IniSecurityManagerFactory(iniResourcePath);
  securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
}

代码示例来源:origin: org.jasig.cas/cas-server-support-generic

/**
 * Sets shiro configuration to the path of the resource
 * that points to the {@code shiro.ini} file.
 *
 * @param resource the resource
 */
@Autowired
public void setShiroConfiguration(@Value("${shiro.authn.config.file:classpath:shiro.ini}") final Resource resource) {
  try {
    if (resource.exists()) {
      final String location = resource.getURI().toString();
      logger.debug("Loading Shiro configuration from {}", location);
      final Factory<SecurityManager> factory = new IniSecurityManagerFactory(location);
      final SecurityManager securityManager = factory.getInstance();
      SecurityUtils.setSecurityManager(securityManager);
    } else {
      logger.debug("Shiro configuration is not defined");
    }
  } catch (final Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: stormpath/stormpath-shiro

/**
 * Wraps the original FilterChainResolver in a priority based instance, which will detect Stormpath API based logins
 * (form, auth headers, etc).
 * @return
 */
@Override
protected FilterChainResolver createFilterChainResolver() {
  FilterChainResolver originalFilterChainResolver = super.createFilterChainResolver();
  if (originalFilterChainResolver == null) {
    return null;
  }
  return getFilterChainResolverFactory(originalFilterChainResolver).getInstance();
}

代码示例来源:origin: org.apache.knox/gateway-server

/**
 *
 * @param config - the shiro.ini config file created in topology deployment.
 * @return returns the Subject given by the shiro config's settings.
 */
protected Subject getSubject(Ini config) throws BadSubjectException {
 try {
  ThreadContext.unbindSubject();
  Factory factory = new IniSecurityManagerFactory(config);
  org.apache.shiro.mgt.SecurityManager securityManager = (org.apache.shiro.mgt.SecurityManager) factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
  Subject subject = SecurityUtils.getSubject();
  if( subject != null) {
   return subject;
  } else {
   out.println("Error Creating Subject from config at: " + config);
  }
 } catch (Exception e){
  out.println(e.toString());
 }
 throw new BadSubjectException("Subject could not be created with Shiro Config at " + config);
}

代码示例来源:origin: apache/knox

/**
 *
 * @param config - the shiro.ini config file created in topology deployment.
 * @return returns the Subject given by the shiro config's settings.
 */
protected Subject getSubject(Ini config) throws BadSubjectException {
 try {
  ThreadContext.unbindSubject();
  @SuppressWarnings("deprecation")
  Factory factory = new IniSecurityManagerFactory(config);
  org.apache.shiro.mgt.SecurityManager securityManager = (org.apache.shiro.mgt.SecurityManager) factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
  Subject subject = SecurityUtils.getSubject();
  if( subject != null) {
   return subject;
  } else {
   out.println("Error Creating Subject from config at: " + config);
  }
 } catch (Exception e){
  out.println(e.toString());
 }
 throw new BadSubjectException("Subject could not be created with Shiro Config at " + config);
}

代码示例来源:origin: com.linecorp.centraldogma/centraldogma-server-shaded

public CentralDogmaSecurityManager(File dataDir, Ini securityConfig,
                  long sessionTimeoutMillis, String sessionCacheSpec) {
  try {
    sessionDao = new FileBasedSessionDAO(new File(dataDir, "_sessions").toPath());
  } catch (IOException e) {
    throw new IOError(e);
  }
  checkArgument(sessionTimeoutMillis > 0,
         "sessionTimeoutMillis: %s (expected: > 0)", sessionTimeoutMillis);
  sessionManager = new CentralDogmaSessionManager(sessionDao, sessionTimeoutMillis);
  validateCacheSpec(sessionCacheSpec);
  final Factory<SecurityManager> factory = new IniSecurityManagerFactory(securityConfig) {
    @Override
    protected SecurityManager createDefaultInstance() {
      final DefaultSecurityManager securityManager = new DefaultSecurityManager();
      securityManager.setSessionManager(sessionManager);
      securityManager.setCacheManager(new CaffeineCacheManager(sessionCacheSpec));
      return securityManager;
    }
  };
  delegate = factory.getInstance();
}

代码示例来源:origin: org.kill-bill.billing/killbill-util

public static Collection<Realm> get(final ConfigSource configSource) {
  final SecurityConfig securityConfig = new ConfigurationObjectFactory(configSource).build(SecurityConfig.class);
  Collection<Realm> realms = null;
  try {
    final Factory<SecurityManager> factory = new IniSecurityManagerFactory(securityConfig.getShiroResourcePath());
    // TODO Pierre hack - lame cast here, but we need to have Shiro go through its reflection magic
    // to parse the [main] section of the ini file. Without duplicating code, this seems to be possible only
    // by going through IniSecurityManagerFactory.
    final DefaultSecurityManager securityManager = (DefaultSecurityManager) factory.getInstance();
    realms = securityManager.getRealms();
  } catch (final ConfigurationException e) {
    log.warn("Unable to configure RBAC", e);
  }
  return realms != null ? realms :
      ImmutableSet.<Realm>of(new IniRealm(securityConfig.getShiroResourcePath())); // Mainly for testing
}

代码示例来源:origin: stormpath/stormpath-shiro

@Override
protected void configure() {
  // create the config object
  Config stormpathConfig = configureStormpathEnvironment(); // pull values from the [stormpath] section
  stormpathInterpolationMap.putAll(stormpathConfig); // values from the shiro.ini files will be interpolated
  // Make the servlet context available to beans
  defaultEnvironmentObjects.put("servletContext", getServletContext());
  try {
    // Put the Stormpath Event Listener in the environment map, so the EventBus can be configured if needed.
    RequestEventListener requestEventListener = stormpathConfig.getInstance(EventPublisherFactory.REQUEST_EVENT_LISTENER);
    defaultEnvironmentObjects.put("stormpathRequestEventListener", requestEventListener);
  }
  catch (ServletException e) {
    throw new ConfigurationException("Could not get instance of Stormpath event listener. ", e);
  }
  this.objects.clear();
  // this causes the ReflectionBuilder to parse the config
  WebSecurityManager securityManager = createWebSecurityManager();
  setWebSecurityManager(securityManager);
  // After the ReflectionBuilder parse the config, we nee to pull out the Stormpath Client
  // and make it available in the ServletContext.  This is needed for other parts of the Stormpath API.
  Factory clientFactory = getObject(DEFAULTS_STORMPATH_CLIENT_PROPERTY, Factory.class);
  log.debug("Updating Client in ServletContext, with instance configured via shiro.ini");
  getServletContext().setAttribute(Client.class.getName(), clientFactory.getInstance());
  // and finally get the FilterChainResolver
  FilterChainResolver resolver = createFilterChainResolver();
  if (resolver != null) {
    setFilterChainResolver(resolver);
  }
}

代码示例来源:origin: com.github.sdorra/shiro-unit

/**
 * Method description
 *
 *
 * @param subjectAware
 */
private void initializeSecurityManager(SubjectAwareDescriptor subjectAware)
{
 String cfg = subjectAware.getConfiguration();
 if (cfg.length() > 0)
 {
  Factory<SecurityManager> factory = new IniSecurityManagerFactory(cfg);
  SecurityManager securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
 }
 String username = subjectAware.getUsername();
 if ((username != null) && (username.length() > 0))
 {
  UsernamePasswordToken token = new UsernamePasswordToken(username,
                  subjectAware.getPassword());
  SecurityUtils.getSubject().login(token);
 }
}

代码示例来源:origin: sonia.junit.shiro/shiro-unit

/**
 * Method description
 *
 *
 * @param subjectAware
 */
private void initializeSecurityManager(SubjectAwareDescriptor subjectAware)
{
 String cfg = subjectAware.getConfiguration();
 if (cfg.length() > 0)
 {
  Factory<SecurityManager> factory = new IniSecurityManagerFactory(cfg);
  SecurityManager securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
 }
 String username = subjectAware.getUsername();
 if ((username != null) && (username.length() > 0))
 {
  UsernamePasswordToken token = new UsernamePasswordToken(username,
                  subjectAware.getPassword());
  SecurityUtils.getSubject().login(token);
 }
}

相关文章

Factory类方法