2016-04-21 60 views
0

我的用戶使用基本身份驗證登錄到我的網站。有什麼辦法,我可以使用的身份驗證的用戶的詳細信息,以作爲一個URLConnection的認證?....在JavaEE中使用URLConnection的當前用戶身份驗證

喜歡的東西

@get 
@Path("/rest/getInfo) 
public String giveMeTheResult(){ 

    URLConnection conn = new URL("/service/info"); 
    Authenticator.setDefault(getCurrentUserAuthentication()); 

    return (String) conn.getContent(); 

} 

我該怎麼做了getCurrentuserAuthentication一部分?

謝謝。

+0

請參閱:https://stackoverflow.com/help/someone-answers – c0der

回答

1

因爲它是基本認證,所以不應該太糟糕。我沒有這樣做,但我認爲你可以這樣做:

public String giveMeTheResult(@Context HttpServletRequest request){ 

    String authHeader = request.getHeader("Authorization"); 
    String[] authChunks = authHeader.split(" "); 
    String decodedAuth = new String(Base64.getDecoder().decode(authChunks[1])); 
    String[] userInfo = decodedAuth.split(":"); 
    // user name is userInfo[0], password is userInfo[1] 

    // not convinced that this is a good idea because it sets it for all 
    Authenticator.setDefault(new PasswordAuthentication(userInfo[0], 
          userInfo[1].toCharArray()); 

    return (String) conn.getContent(); 
} 

但警告 - 這是完全未經測試並沒有錯誤檢查!

相關問題