[英]Get parameter value from method in another class
我想要達到的目的是,使用Raygun(崩潰報告服務)向遇到該問題的用戶發送用戶詳細信息。
我最初嘗試使用與此類似的方法來獲取用戶名:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
username = authentication.getName();
但是由於某種原因,它返回了值ipaduser
。 我不確定該價值從何而來,但這顯然不是答案。
然后,我嘗試:
System.getProperty("user.name");
但這只是獲取我們API服務器的已登錄用戶的名稱,而不是使用該應用程序的用戶的用戶名。
經過一番挖掘,我發現用戶的數據庫的用戶名和名稱存儲在名為ApiAccessToken的自定義對象中。 問題在於,僅在我們的Controller類中的方法中設置了此ApiAccessToken對象。
例如:
@RequestMapping(path = "/api/foo", method = RequestMethod.GET)
public List<Bar> getBarForDatabase(@RequestParam(value = "api_key") String apiKey{
ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
// Etc
}
ApiAccessToken和validateApiKey()的代碼如下:
public final class ApiAccessToken {
private String databaseName;
private String username;
public String getDatabaseName() { return databaseName; }
public void setDatabaseName(String databaseName) { this.databaseName = databaseName; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
}
和
protected ApiAccessToken validateApiKey(ApiKeyService apiKeyService, String apiKey){
UserAccountJdbcRepository userAccountJdbcRepository = new UserAccountJdbcRepository(createDataSourceForDatabase(USERS_DATABASE_NAME));
ApiAccessToken accessToken = apiKeyService.validateApiKey(userAccountJdbcRepository, apiKey);
if (accessToken == null) {
throw new InvalidApiKeyException();
}
return accessToken;
}
我的另一個想法是在Controller類中有一個本地變量,該變量將存儲apiKey
的值,然后使用getter將值傳遞給另一個方法。 例如,在Controller類中:
private static String apiKey;
public static String getApiKey() { return apiKey; }
@RequestMapping(path = "/api/foo", method = RequestMethod.GET)
public List<Bar> getBarForDatabase(@RequestParam(value = "api_key") String apiKey{
ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
apiKey = this.apiKey;
//Etc
}
然后在我向Raygun發送異常詳細信息的類中:
String apiKey = Controller.getApiKey();
ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
username = accessToken.getUsername();
database = accessToken.getDatabaseName();
但這只會返回NullPointerException,而不會將任何內容發送給Raygun。
任何幫助將不勝感激,因為我不確定要嘗試其他方法。 讓我知道您是否認為我應該保留的遺漏之處。 再說一次,我對Spring MVC還是很陌生,寫了大部分內容的開發人員不再受雇於我們。
由於api_key是請求參數,因此獲取它的唯一方法是通過控制器。
class Controller {
@RequestMapping(path = "/api/foo", method = RequestMethod.GET)
public List<Bar> getBarForDatabase(@RequestParam(value = "api_key") String apiKey) {
...
Raygun.sendException(apiKey)
...
}
}
class Raygun {
public static sendException(String apiKey) {
ApiAccessToken accessToken = validateApiKey(apiKeyService, apiKey);
username = accessToken.getUsername();
database = accessToken.getDatabaseName();
...
}
}
如果要在多個位置使用api鍵獲取用戶信息,請查看Interceptor
在Minh Hoang的建議下,我得以解決此問題。
我向類RaygunUnhandledExceptionHandler添加了全局變量:
private static ApiAccessToken accessToken = new ApiAccessToken();
然后,我在RaygunUnhandledExceptionHandler中編寫了此方法以獲取accessToken
並將其設置為我在上面聲明的變量:
public static void getAccessTokenFromController(ApiAccessToken accessTokenFromController) { accessToken = accessTokenFromController; }
然后我加入這行到控制器/ API /富以便它的發送accesstoken
到RaygunUnhandledExceptionHandler:
// Sends the API Key to RaygunUnhandledExceptionHandler
RaygunUnhandledExceptionHandler.getAccessTokenFromController(accessToken);
然后在RaygunUnhandledExceptionHandler中的defaultErrorHandler()中:
ArrayList tags = new ArrayList<String>();
tags.add("username: " + accessToken.getUsername());
tags.add("database: " + accessToken.getDatabaseName());
再次感謝Minh Hoang和fantaghirocco在幫助我解決此問題上所提供的幫助。
如果您發現此實現存在任何問題,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.