通过Java代码加载shiro.ini文件。
//加载ini文件
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath: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:";