shiro配置文件读取(一)

通过Java代码加载shiro.ini文件。

//加载ini文件
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");


public IniSecurityManagerFactory(String iniResourcePath) {
this(Ini.fromResourcePath(iniResourcePath));
}



/**
* Creates a new {@code Ini} instance loaded with the INI-formatted data in the resource at the given path. The
* resource path may be any value interpretable by the
* {@link ResourceUtils#getInputStreamForPath(String) ResourceUtils.getInputStreamForPath} method.
*
* @param resourcePath the resource location of the INI data to load when creating the {@code Ini} instance.
* @return a new {@code Ini} instance loaded with the INI-formatted data in the resource at the given path.
* @throws ConfigurationException if the path cannot be loaded into an {@code Ini} instance.
*/
public static Ini fromResourcePath(String resourcePath) throws ConfigurationException {
if (!StringUtils.hasLength(resourcePath)) {
throw new IllegalArgumentException("Resource Path argument cannot be null or empty.");
}
Ini ini = new Ini();
ini.loadFromPath(resourcePath);
return ini;
}



/**
* Loads data from the specified resource path into this current {@code Ini} instance. The
* resource path may be any value interpretable by the
* {@link ResourceUtils#getInputStreamForPath(String) ResourceUtils.getInputStreamForPath} method.
*
* @param resourcePath the resource location of the INI data to load into this instance.
* @throws ConfigurationException if the path cannot be loaded
*/
public void loadFromPath(String resourcePath) throws ConfigurationException {
InputStream is;
try {
is = ResourceUtils.getInputStreamForPath(resourcePath);
} catch (IOException e) {
throw new ConfigurationException(e);
}
load(is);
}



/**
* Returns the InputStream for the resource represented by the specified path, supporting scheme
* prefixes that direct how to acquire the input stream
* ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX},
* {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}). If the path is not prefixed by one
* of these schemes, the path is assumed to be a file-based path that can be loaded with a
* {@link FileInputStream FileInputStream}.
*
* @param resourcePath the String path representing the resource to obtain.
* @return the InputStraem for the specified resource.
* @throws IOException if there is a problem acquiring the resource at the specified path.
*/
public static InputStream getInputStreamForPath(String resourcePath) throws IOException {

InputStream is;
if (resourcePath.startsWith(CLASSPATH_PREFIX)) {
is = loadFromClassPath(stripPrefix(resourcePath));

} else if (resourcePath.startsWith(URL_PREFIX)) {
is = loadFromUrl(stripPrefix(resourcePath));

} else if (resourcePath.startsWith(FILE_PREFIX)) {
is = loadFromFile(stripPrefix(resourcePath));

} else {
is = loadFromFile(resourcePath);
}

if (is == null) {
throw new IOException("Resource [" + resourcePath + "] could not be found.");
}

return is;
}



/**
* Resource path prefix that specifies to load from a classpath location, value is <b>{@code classpath:}</b>
*/
public static final String CLASSPATH_PREFIX = "classpath:";
/**
* Resource path prefix that specifies to load from a url location, value is <b>{@code url:}</b>
*/
public static final String URL_PREFIX = "url:";
/**
* Resource path prefix that specifies to load from a file location, value is <b>{@code file:}</b>
*/
public static final String FILE_PREFIX = "file:";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值