2017-01-29 34 views
1

我試圖適應這個優秀的stormpath帖子布賴恩德默斯 - https://stormpath.com/blog/protecting-jax-rs-resources-rbac-apache-shiro - 爲了我自己的目的,迄今爲止它工作得很好 - 除了現在我想爲用戶添加stormpath /角色管理,而不是讓用戶進入shiro-ini文件。使用shiro與stormpath爲jax-rs rbac

我使用Apache四郎四郎-JAXRS 1.4.0-RC,以確保使用JAXRS一個REST端點。它工作正常。我能夠選擇性地使用固定端點@RequiresPermissions標籤,像這樣:

@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; 
} 

我也有一個應用程序類,加入我的資源和ShiroFeature類,像這樣:

package ca.odell.erbscan; 
import ca.odell.erbscan.ws.ScanService; 

import javax.ws.rs.ApplicationPath; 
import javax.ws.rs.core.Application; 
import java.util.HashSet; 
import java.util.Set; 

import org.apache.shiro.web.jaxrs.ShiroFeature; 
import com.stormpath.shiro.jaxrs.StormpathShiroFeature; 


@ApplicationPath("/") 
public class ERBApplication extends Application { 

@Override 
public Set<Class<?>> getClasses() { 
    Set<Class<?>> classes = new HashSet<Class<?>>(); 

    // register Shiro 
    classes.add(ShiroFeature.class); 
    // register resources 
    classes.add(ScanService.class); 

    return classes; 
} 
} 

和我web.xml中的init我的應用程序類,像這樣:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
<display-name>ERBSCAN</display-name> 
<servlet> 


    <servlet-name>ERBRest</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>ca.odell.erbscan</param-value> 
    </init-param> 

    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>ca.odell.erbscan.ERBApplication</param-value> 
    </init-param> 


    <load-on-startup>1</load-on-startup> 
</servlet> 



<servlet-mapping> 
    <servlet-name>ERBRest</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

<listener> 
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class> 
</listener> 

<filter> 
    <filter-name>ShiroFilter</filter-name> 
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>ShiroFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
</filter-mapping> 


</web-app> 

最後我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.sessionIdUrlRewritingEnabled = false 


[urls] 
/** = noSessionCreation, authcBasic[permissive] 

[users] 
# format: username = password, role1, role2, ..., roleN 
root = secret,admin 
emperor = secret,admin 
officer = secret,officer 
guest = secret 

[roles] 

admin = * 
officer = troopers:create, troopers:read, troopers:update 

接下來我要做的是爲RBAC添加Stormpath,而不是讓用戶和角色在文件中。我的感覺是,有一個簡單的方法可以做到這一點,而且我正在推翻它。

我認爲這將是我的shiro.ini加入一個相當簡單的方式:

stormpathClient = com.stormpath.shiro.client.ClientFactory 
stormpathClient.cacheManager = $cacheManager 

stormpath.application.href=http://.... 

但是我錯了。有人能指出我正確的方向嗎?

回答

0

我會在這裏回答我自己的問題。我不認爲這是最好的解決方案,但它是我設法開始工作的。

我跟着這個Web應用程序教程關閉了shiro網站。

https://shiro.apache.org/webapp-tutorial.html

我檢查了該項目的第六步和複製的shiro.ini的[主]部分如下:注意我添加了

https://api.stormpath.com/v1/applications/ $ STORMPATH_APPLICATION_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 { 
. 
. 
. 

,所以我需要在stormpath相匹配,對上述資源的權限,添加的帳戶,一組和權限。爲了做到這一點,我需要在我現有的測試應用程序下添加一個Stormpath帳戶(我已經擁有應用程序設置)。我還加了一個叫officer1的小組。在這組下我添加自定義數據數組稱爲apacheShiroPermissions - 我添加了一個字符串鍵/值對「士兵:讀」到apacheShiroPermissions - 的JSON低於

{ 
    "apacheShiroPermissions": [ 
    "trooper:read" 
    ] 
} 

然後,我只是努力讓我的帳戶 - 中這個案件jlpicard是officer1組的一部分。

測試,捲曲

curl --user jlpicard:Changeme1 http://localhost:8080/JPA1_Web_exploded/rest/scan/gettest 

確認jlpicard對權限級別的訪問。向apacheShiroPermission數組添加和刪除字符串條目,即允許進行細粒度訪問。

同樣從officer1刪除jlpicard或添加其他帳戶到預期工作。

毫無疑問,有一個更好的方法可以做到這一點,但迄今爲止,這對我來說是有效的。

1

感謝您閱讀該文章!

一對夫婦的事情,我想指出:

  • 使用此功能的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.sessionIdUrlRewritingEnabled = false 

[urls] 
/** = noSessionCreation, authcBasic[permissive] 

[stormpath] 
stormpath.application.href=http://.... 
  • 權限可以存儲爲用戶或角色自定義數據,您可以更新Custo M數據在Stormpath管理控制檯:
{ 
    … your other custom data fields …, 
    "apacheShiroPermissions": [ 
     "troopers:create", 
     "troopers:read", 
     "troopers:update" 
    ] 
} 

blog post涵蓋了自定義的數據位,這是年紀大一點的,但仍然具有現實意義。我將在不久的將來更新此文檔,以便反饋意見。

如果這沒有幫助,你也可以ping support,我們會幫助你!