Jersey 将shiro与stormpath一起用于jax rs rbac
我正试图将Brian Demers的这篇优秀的stormpath文章改编成适合我自己的文章,到目前为止它运行得非常好,只是现在我想添加stormpath用于用户/角色管理,而不是将用户放在shiro ini文件中 我正在使用ApacheShiro Shiro jaxrs 1.4.0-RC来使用jax-rs保护REST端点。它工作正常。我可以使用@RequiresPermissions标记有选择地保护端点,如下所示:Jersey 将shiro与stormpath一起用于jax rs rbac,jersey,jax-rs,shiro,stormpath,Jersey,Jax Rs,Shiro,Stormpath,我正试图将Brian Demers的这篇优秀的stormpath文章改编成适合我自己的文章,到目前为止它运行得非常好,只是现在我想添加stormpath用于用户/角色管理,而不是将用户放在shiro ini文件中 我正在使用ApacheShiro Shiro jaxrs 1.4.0-RC来使用jax-rs保护REST端点。它工作正常。我可以使用@RequiresPermissions标记有选择地保护端点,如下所示: @Path("/scan") @Produces("application/js
@Path("/scan")
@Produces("application/json")
public class ScanService {
final static Logger logger = Logger.getLogger(ScanService.class);
@GET
@Path("/gettest")
@RequiresPermissions("troopers:read")
public List<Barcode> gettest() throws Exception {
ArrayList<Barcode> listofstrings = new ArrayList<Barcode>();
Barcode b = new Barcode();
b.setBarcode("this is a big barcode");
listofstrings.add(b );
return listofstrings;
}
@GET
@Produces( MediaType.APPLICATION_JSON )
@Path("/gettest2")
public List<Barcode> gettest2() throws Exception {
ArrayList<Barcode> listofstrings = new ArrayList<Barcode>();
Barcode b = new Barcode();
b.setBarcode("this is a BIGGER barcode");
listofstrings.add(b );
return listofstrings;
}
我接下来要做的是为RBAC添加Stormpath,而不是在文件中添加用户和角色。我的感觉是有一个简单的方法来做这件事,我想得太多了
我认为在我的shiro.ini中添加以下内容是一种相当简单的方式:
stormpathClient = com.stormpath.shiro.client.ClientFactory
stormpathClient.cacheManager = $cacheManager
stormpath.application.href=http://....
但我错了。有人能给我指出正确的方向吗?我要在这里回答我自己的问题。我不认为这是最好的解决方案,但这是我设法做到的 我在shiro网站上学习了这个web应用程序教程 我签出了项目的step6并复制了shiro.ini的[main]部分,如下所示:注意,我添加了 $STORMPATH\u应用程序\u ID 在底部是[主]部分
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager
stormpathClient = com.stormpath.shiro.client.ClientFactory
stormpathClient.cacheManager = $cacheManager
# we can disable session tracking completely, and have Stormpath manage it for us.
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.sessionIdCookieEnabled = false
securityManager.sessionManager.sessionIdUrlRewritingEnabled = false
stormpathRealm = com.stormpath.shiro.realm.ApplicationRealm
stormpathRealm.client = $stormpathClient
stormpathRealm.groupRoleResolver.modeNames = name
securityManager.realm = $stormpathRealm
stormpathRealm.applicationRestUrl = https://api.stormpath.com/v1/applications/$STORMPATH_APPLICATION_ID
然后我完全删除了shiro.ini的[users]部分。由于它现在连接到Stormpath,我需要在那里添加用户和组。我的ScanService(如上)有一个名为gettest的方法:
@GET
@Path("/gettest")
@RequiresPermissions("trooper:read")
public List<Barcode> gettest() throws Exception {
.
.
.
然后,我只需确保我的帐户——在本例中,jlpicard是officer1组的一部分
卷曲测试
curl --user jlpicard:Changeme1 http://localhost:8080/JPA1_Web_exploded/rest/scan/gettest
确认jlpicard具有权限级别的访问权限。向ApacheShirOperamission数组添加和删除字符串条目,即允许细粒度访问
另外,从officer1中删除jlpicard或向其中添加另一个帐户也可以正常工作
毫无疑问,有一种更好的方法可以做到这一点,但到目前为止,这种方法对我是有效的。感谢您阅读这篇文章 我想指出几件事:
- 使用此功能
而不是com.stormpath.shiro.jaxrs.StormpathShiroFeature
ShiroFeature
- 您的shiro.ini可能看起来像:
[main]
cacheManager=org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager=$cacheManager
sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager=$sessionManager
securityManager.sessionManager.sessionIdCookieEnabled=false
securityManager.sessionManager.SessionDurlReWritingEnabled=false
[网址]
/**=noSessionCreation,authcBasic[许可]
[暴风路径]
stormpath.application.href=http://....
- 权限可以存储为用户或角色自定义数据,您可以在Stormpath管理控制台中更新自定义数据:
{
…您的其他自定义数据字段…,
“apacheShiroPermissions”:[
“士兵:创造”,
“士兵:读”,
“士兵:更新”
]
}
这涵盖了自定义数据位,它有点旧,但仍然相关。我会在不久的将来更新文档,所以欢迎反馈
如果这对你没有帮助,也可以ping,我们会让你走的
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager
stormpathClient = com.stormpath.shiro.client.ClientFactory
stormpathClient.cacheManager = $cacheManager
# we can disable session tracking completely, and have Stormpath manage it for us.
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.sessionIdCookieEnabled = false
securityManager.sessionManager.sessionIdUrlRewritingEnabled = false
stormpathRealm = com.stormpath.shiro.realm.ApplicationRealm
stormpathRealm.client = $stormpathClient
stormpathRealm.groupRoleResolver.modeNames = name
securityManager.realm = $stormpathRealm
stormpathRealm.applicationRestUrl = https://api.stormpath.com/v1/applications/$STORMPATH_APPLICATION_ID
@GET
@Path("/gettest")
@RequiresPermissions("trooper:read")
public List<Barcode> gettest() throws Exception {
.
.
.
{
"apacheShiroPermissions": [
"trooper:read"
]
}
curl --user jlpicard:Changeme1 http://localhost:8080/JPA1_Web_exploded/rest/scan/gettest