org.apache.shiro.util.Factory.getInstance()方法的使用及代码示例

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

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

Factory.getInstance介绍

[英]Returns an instance of the required type. The implementation determines whether or not a new or cached instance is created every time this method is called.
[中]返回所需类型的实例。实现确定每次调用此方法时是否创建新实例或缓存实例。

代码示例

代码示例来源: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: 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: flyleft/xmarket-server

@Test
  public void one(){
    // 读取配置文件,初始化SecurityManager工厂
    Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
    // 获取securityManager实例
    SecurityManager securityManager=factory.getInstance();
    // 把securityManager实例绑定到SecurityUtils
    SecurityUtils.setSecurityManager(securityManager);
    // 得到当前执行的用户
    Subject currentUser=SecurityUtils.getSubject();
    // 创建token令牌,用户名/密码
    UsernamePasswordToken token=new UsernamePasswordToken("jcala", "12345");
    try{
      // 身份认证
      currentUser.login(token);
      System.out.println("身份认证成功!");
    }catch(AuthenticationException e){
      e.printStackTrace();
      System.out.println("身份认证失败!");
    }
    // 退出
    currentUser.logout();
  }
}

代码示例来源:origin: com.ning.billing/killbill-util

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: org.kill-bill.billing/killbill-util

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: org.kill-bill.billing/killbill-entitlement

protected void configureShiro() {
  final Ini config = new Ini();
  config.addSection("users");
  config.getSection("users").put("EntitlementUser", "password, entitlement");
  config.addSection("roles");
  config.getSection("roles").put("entitlement", Permission.ACCOUNT_CAN_CREATE.toString() +
                         "," + Permission.ENTITLEMENT_CAN_CREATE.toString() +
                         "," + Permission.ENTITLEMENT_CAN_CHANGE_PLAN.toString() +
                         "," + Permission.ENTITLEMENT_CAN_PAUSE_RESUME.toString() +
                         "," + Permission.ENTITLEMENT_CAN_TRANSFER.toString() +
                         "," + Permission.ENTITLEMENT_CAN_CANCEL.toString());
  // Reset the security manager
  ThreadContext.unbindSecurityManager();
  final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config);
  final SecurityManager securityManager = factory.getInstance();
  SecurityUtils.setSecurityManager(securityManager);
}

相关文章

Factory类方法