You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by tanvir <ta...@sportstats.ca> on 2014/08/08 17:45:46 UTC
Dynamically set Client.apiKeyFileLocation value during
initialization (shiro.ini?)
Hi,
I have spent several days trying to find a solution for my issue so any help
would be more than welcome! I am using stormpath as my Client. Although I
know all the reasons why apiKeyFile should not be located inside the project
classpath, This is exactly what I need to do but I am having trouble setting
it properly.
Currently in my shiro.ini I have-
_______________
...
stormpathClient = com.stormpath.shiro.client.ClientFactory
stormpathClient.cacheManager = $cacheManager
stormpathClient.apiKeyFileLocation = c:/stormpathApiKey.properties
stormpathRealm = com.stormpath.shiro.realm.ApplicationRealm
stormpathRealm.client = $stormpathClient
...
_______________
When I change the apiKeyFileLocation to without the quote
'/stormpathApiKey.properties' or '/WEB-INF/stormpathApiKey.properties' it
does not work even though the file is present in both the src and the
WEB-INF folder.
I have also tried writing my own Environment Initializer class
IniWebEnvironment but that didn't work either. Here is what I tried to do:
_______________
public class ShiroWebEnvironment extends IniWebEnvironment{
private static final String applicationRestUrl =
"https://api.stormpath.com/v1/applications/APPHREF";
private static final String stormpathApiKeyFile =
"stormpathApiKey.properties";
@Override
public void init() {
Settings settings = new Settings();
String location = settings.getStormpathIniFilePath();
Ini ini = createIni(location, false);
ApplicationRealm stormpathRealm = new ApplicationRealm();
DefaultSecurityManager securityManager = new
DefaultSecurityManager(stormpathRealm);
MemoryConstrainedCacheManager cacheManager = new
MemoryConstrainedCacheManager();
securityManager.setCacheManager(cacheManager);
//Initializing Stormpath
ClientFactory stormpathClient = new ClientFactory();
stormpathClient.setCacheManager(cacheManager);
InputStream
apiKeyIS=SettingsProperties.class.getClassLoader().getResourceAsStream(stormpathApiKeyFile);
stormpathClient.setApiKeyInputStream(apiKeyIS);
stormpathRealm.setClient(stormpathClient.getInstance());
stormpathRealm.setApplicationRestUrl(applicationRestUrl);
stormpathRealm.setName(groupRoleResolverModeNames);
DefaultGroupRoleResolver groupRoleResolver = new
DefaultGroupRoleResolver();
stormpathRealm.setGroupRoleResolver(groupRoleResolver);
this.setIni(ini);
super.init();
}
_______________
Thank you so much!
--
View this message in context: http://shiro-user.582556.n2.nabble.com/Dynamically-set-Client-apiKeyFileLocation-value-during-initialization-shiro-ini-tp7580145.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: Dynamically set Client.apiKeyFileLocation value during
initialization (shiro.ini?)
Posted by tanvir <ta...@sportstats.ca>.
Hello Jared,
Thank you so much for your post. Unfortunately I'm still learning how shiro
works and I didn't clearly understand your suggestion.
My ShiroWebEnvironment class extends IniWebEnvironment class and in the
web.xml file I had to add the following code so shiro during initialization
will call this method.-
_____
<context-param>
<param-name>shiroEnvironmentClass</param-name>
<param-value>ca.sportstats.acl.ShiroWebEnvironment</param-value>
</context-param>
_____
Do I have to do something similar in your suggestion as well? Write a class
that extends createWebSecurityManager and point to it in the web.xml file?
If so, what should be the param-name of the context-param I'll have to add?
Another problem is that will I still have to initialize via the ini file in
this case? I'm also Interested in knowing how I can programmatically
initialize the [urls] section so I may consider avoiding the use of the
shiro.ini file altogether.
Thank you!
--
View this message in context: http://shiro-user.582556.n2.nabble.com/Dynamically-set-Client-apiKeyFileLocation-value-during-initialization-shiro-ini-tp7580145p7580147.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: Dynamically set Client.apiKeyFileLocation value during
initialization (shiro.ini?)
Posted by Jared Bunting <ja...@peachjean.com>.
So, I'm not particularly familiar with stormpath so I can't help with your
first question. However, on your second I might be able to.
You instantiate a Realm, but it doesn't appear that you ever do anything
with it.
What if, you move this realm instantiation code into
createWebSecurityManager - that way you can just set the realm on the
security manager?
```java
protected WebSecurityManager createWebSecurityManager() {
ApplicationRealm stormpathRealm = new ApplicationRealm();
DefaultSecurityManager securityManager = new
DefaultSecurityManager(stormpathRealm);
MemoryConstrainedCacheManager cacheManager = new
MemoryConstrainedCacheManager();
securityManager.setCacheManager(cacheManager);
//Initializing Stormpath
ClientFactory stormpathClient = new ClientFactory();
stormpathClient.setCacheManager(cacheManager);
InputStream apiKeyIS=SettingsProperties.
class.getClassLoader().getResourceAsStream(stormpathApiKeyFile);
stormpathClient.setApiKeyInputStream(apiKeyIS);
stormpathRealm.setClient(stormpathClient.getInstance());
stormpathRealm.setApplicationRestUrl(applicationRestUrl);
stormpathRealm.setName(groupRoleResolverModeNames);
DefaultGroupRoleResolver groupRoleResolver = new
DefaultGroupRoleResolver();
stormpathRealm.setGroupRoleResolver(groupRoleResolver);
WebSecurityManager sm = super.createWebSecurityManager();
((RealmSecurityManager)sm).setRealm(stormpathRealm);
return sm;
}
```
This does introduce the restriction that your INI file specifies a
SecurityManager that implements RealmSecurityManager - but most do.
On Fri, Aug 8, 2014 at 10:45 AM, tanvir <ta...@sportstats.ca> wrote:
> Hi,
> I have spent several days trying to find a solution for my issue so any
> help
> would be more than welcome! I am using stormpath as my Client. Although I
> know all the reasons why apiKeyFile should not be located inside the
> project
> classpath, This is exactly what I need to do but I am having trouble
> setting
> it properly.
>
> Currently in my shiro.ini I have-
> _______________
> ...
> stormpathClient = com.stormpath.shiro.client.ClientFactory
> stormpathClient.cacheManager = $cacheManager
> stormpathClient.apiKeyFileLocation = c:/stormpathApiKey.properties
> stormpathRealm = com.stormpath.shiro.realm.ApplicationRealm
> stormpathRealm.client = $stormpathClient
> ...
> _______________
>
>
> When I change the apiKeyFileLocation to without the quote
> '/stormpathApiKey.properties' or '/WEB-INF/stormpathApiKey.properties' it
> does not work even though the file is present in both the src and the
> WEB-INF folder.
>
>
> I have also tried writing my own Environment Initializer class
> IniWebEnvironment but that didn't work either. Here is what I tried to do:
>
> _______________
> public class ShiroWebEnvironment extends IniWebEnvironment{
> private static final String applicationRestUrl =
> "https://api.stormpath.com/v1/applications/APPHREF";
> private static final String stormpathApiKeyFile =
> "stormpathApiKey.properties";
>
> @Override
> public void init() {
>
> Settings settings = new Settings();
> String location = settings.getStormpathIniFilePath();
> Ini ini = createIni(location, false);
>
> ApplicationRealm stormpathRealm = new ApplicationRealm();
> DefaultSecurityManager securityManager = new
> DefaultSecurityManager(stormpathRealm);
>
> MemoryConstrainedCacheManager cacheManager = new
> MemoryConstrainedCacheManager();
> securityManager.setCacheManager(cacheManager);
>
> //Initializing Stormpath
> ClientFactory stormpathClient = new ClientFactory();
> stormpathClient.setCacheManager(cacheManager);
> InputStream
>
> apiKeyIS=SettingsProperties.class.getClassLoader().getResourceAsStream(stormpathApiKeyFile);
> stormpathClient.setApiKeyInputStream(apiKeyIS);
> stormpathRealm.setClient(stormpathClient.getInstance());
>
> stormpathRealm.setApplicationRestUrl(applicationRestUrl);
> stormpathRealm.setName(groupRoleResolverModeNames);
>
> DefaultGroupRoleResolver groupRoleResolver = new
> DefaultGroupRoleResolver();
> stormpathRealm.setGroupRoleResolver(groupRoleResolver);
>
> this.setIni(ini);
> super.init();
>
> }
> _______________
>
>
> Thank you so much!
>
>
>
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/Dynamically-set-Client-apiKeyFileLocation-value-during-initialization-shiro-ini-tp7580145.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>