2016-06-13 15 views
0

我想讓一個客戶端將REST調用到一個jhipster生成的webapp。 而我似乎誤解了一些信息來做到這一點。在java中製作一個Jhipster REST客戶端

我發現是在application.yml我需要啓用cors選項。 所以我註釋掉如下:

jhipster: 
cors: #By default CORS are not enabled. Uncomment to enable. 
    allowed-origins: "*" 
    allowed-methods: GET, PUT, POST, DELETE, OPTIONS 
    allowed-headers: "*" 
    exposed-headers: 
    allow-credentials: true 
    max-age: 1800 

這將使REST調用可能。

我想我還需要啓用這個,但我不知道:

security: 
basic: 
    enabled: true 

我希望,我可以作出這樣一個電話:

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.sun.jersey.api.client.Client; 
import com.sun.jersey.api.client.ClientResponse; 
import com.sun.jersey.api.client.WebResource; 
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; 
.... 
Client client = Client.create(); 
client.addFilter(new HTTPBasicAuthFilter("admin", "admin")); 
WebResource webResource = client.resource("http://localhost:8080/api/example"); 

但我得到一個所以我錯過了什麼?

這裏我.yo-rc.json文件:

{ 
"generator-jhipster": { 
"jhipsterVersion": "3.0.0", 
"baseName": "tinybotsWeb", 
"packageName": "nl.tinybots.web", 
"packageFolder": "nl/tinybots/web", 
"serverPort": "8080", 
"authenticationType": "session", 
"hibernateCache": "ehcache", 
"clusteredHttpSession": "no", 
"websocket": "no", 
"databaseType": "sql", 
"devDatabaseType": "mysql", 
"prodDatabaseType": "mysql", 
"searchEngine": "elasticsearch", 
"buildTool": "maven", 
"enableSocialSignIn": true, 
"rememberMeKey": "6799bca03613c99e29cd3c1bb7ac878157250d87", 
"useSass": false, 
"applicationType": "monolith", 
"testFrameworks": [ 
    "gatling", 
    "cucumber", 
    "protractor" 
], 
"enableTranslation": true, 
"nativeLanguage": "nl", 
"languages": [ 
    "nl", 
    "en", 
    "de" 
    ] 
} 
} 

添加以下到SecuryConfiguration:

http 
     .csrf() 
     .ignoringAntMatchers("/basicAuthApi/**") 
... 
    .and() 
     .authorizeRequests() 
     .antMatchers("/basicAuthApi/**") 
     .hasAuthority(AuthoritiesConstants.BASIC_AUTH).and().httpBasic() 
... 

,現在我可以提出請求。

我現在的問題是: 是我應該怎麼做的? 安全嗎? 這是什麼?:做

security: 
    basic: 
    enabled: true 
+0

這可能是因爲我首先要對像一個URL驗證來生成REST客戶端:API /認證獲得令牌。但我沒有得到這個工作。 – tibi

回答

0

在瀏覽器和按F12鍵進行開發選項打開你的Jhipster應用。

檢查網絡,看看你的jspers應用程序的行爲如何認證調用它發送,它在這些調用ect的頭部中發送的內容。

在你的java應用程序中逆向工程這個過程。

enter image description here

0

這裏的如何我在我的情況,我認爲這是類似於實施的解決方案概要。它是真正的swift代碼,但請把它當作僞代碼,因爲它可能不正確。請注意,這是張貼在這裏解決的副本:爲成功和一個用於Jhipster + REST client + authentication

  1. 讓你需要調用任何方法的調用,傳遞這種方法的回調(塊,或同等)失敗

    func action(
        URLString:String, 
        method:Method, 
        encoding:Encoding = .JSON, 
        parameters:[String : AnyObject]?, 
        success:(statusCode:Int, responseObject:AnyObject)->Void, 
        failure:(statusCode:Int, error:NSError)->Void 
    ) 
    
  2. 方法es。 /events您處理特定的失敗情況,即狀態碼爲401時。

    if(r!.statusCode==ResponseCodes.HTTP_UNAUTHORIZED.rawValue){ 
    
        loginAndAction(URLString, method: method, encoding: encoding, parameters: parameters, success: success, failure: failure) 
    
    }else{ 
    
        failure(statusCode: response.response!.statusCode, error:response.result.error!) 
    
    } 
    
  3. 在這種特殊情況下,而不是返回回結果,並調用相應的回調函數,調用登錄()方法,經過必要的參數,接受原success()回調

    func loginAndAction(
        URLString:String, 
        method:Method, 
        encoding: Encoding, 
        parameters:[String:AnyObject]?, 
        success:(statusCode:Int, responseObject:AnyObject)->Void, 
        failure:(statusCode:Int, error:NSError)->Void 
        )->Void 
    
  4. 如果認證成功

    var d:[String:AnyObject] = response.result.value as! [String:AnyObject] 
    self.authToken = d["access_token"] as! String 
    
    action(URLString, method: method,encoding:encoding, parameters: parameters, success: success, failure: failure) 
    

此時方法操作可以使用適當的工作令牌。

這應該每天只發生一次(基於令牌過期),並且它是適用於oauth2 refresh_token調用的機制。