2016-11-11 57 views
1

我期待實現JWT驗證並從Java EE上的令牌中獲取一些額外的信息。Java EE(JBoss EAP)自定義驗證方法JWT

問題是我需要在我的web.xml文件中使用「jwt」自定義auth方法,但除BASIC,DIGEST,FORM和CLIENT-CERT之外,不支持此方法。

有沒有辦法實現自定義登錄方法來啓動身份驗證過程?

我不需要客戶端交互,授權頭將使用承載域從調用應用程序填充。

即授權:承載cn389ncoiwuencr

+0

同樣的問題。沒有任何文件涉及這個! –

+0

@MatthewCachia我現在寫了我自己的模塊來做到這一點。我可以分享如何做到這一點的細節。打算開源,但尚未。 – IanWatson

+0

@MatthewCachia請參閱下面的答案。 – IanWatson

回答

0

注意這是在6.4 EAP測試,可能需要改變爲V7特別是與使用的閥。

您需要編寫自定義xx.Authentication機制。你可以通過擴展org.apache.cataline.authenticator.FormAuthenticator來重寫authenticate方法。

在認證方法將執行

Principal principal = request.getUserPrincipal(); 
if (principal != null) { 
logger.trace("User already authenticated"); 
return true; 
} 

Realm realm = context.getRealm(); 

principal = realm.authenticate("user", (String) null); 

register(request, response, principal, "Bearer", "user", null); 

return true; 

境界然後可以在獨立的XML安全子系統下進行配置。

<security-domain name="JWT"> 
    <authentication> 
     <login-module code="xx.xx.xx.JWTLoginModule" flag="required"> 
     </login-module> 
    </authentication> 
</security-domain> 

的JWTLoginModule是它使用https://github.com/jwtk/jjwt庫定義LoginModule。有關登錄模塊的信息,請參見https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.3/html/Security_Guide/chap-Login_Modules.html。創建自己的擴展org.jboss.security.auth.spi.AbstractServerLoginModule。

然後,您需要使用org.picketbox模塊的相同模塊依賴關係將這些擴展添加到eap服務器的modules目錄中。

完成服務器設置。現在,你需要指示您的應用程序使用此設置:

在你的WEB-INF目錄中創建: 的jboss-web.xml中爲

<jboss-web> 
    <security-domain>JWT</security-domain> 
    <valve> 
     <class-name>xx.Authentication</class-name> 
    </valve> 
</jboss-web> 

和加載自定義模塊在JBoss中部署結構

<jboss-deployment-structure> 
    <deployment> 
     <dependencies> 
      <module name="xx.custom"/> 
     </dependencies> 
    </deployment> 
</jboss-deployment-structure> 

最後在您的web.xml文件中將auth-method更改爲「JWT」。

打算創建一個開源版本,但在此之前這將不得不做。