我有一个通过数据库使用base64身份验证进行保护的spring rest api。是否可以使用另一个rest api并以某种方式通过第一个api进行身份验证?
发布于 2015-02-05 09:45:30
您是否考虑过使用基于OAuth的身份验证和API密钥管理来保护您的API。security issues for APIs基本身份验证实际上并不被认为是理想,它来自于具有另一组HTTP用户名和密码的HTTP
无论哪种方式,您都可以考虑使用Stormpath来简化这项工作。看看this guide,它同时支持HTTP basic和OAuth。
这段示例代码将让您很好地了解这是多么容易。
假设您想要公开一个名为startEngines()
的操作,并且想要保护它。您还需要公开一个新操作来获取访问令牌,在本例中为String getAccessToken(ApiKey)
。
您的用户将运行如下代码:
@Test
public void executeSomeOauth2AuthenticatedOperation() {
String userApiKeyPath = System.getProperty("user.home") + "/.stormpath/apiKey_4Yrc0TJ5sBFldwtu6nfzf5.properties";
ApiKey userApiKey = ApiKeys.builder().setFileLocation(userApiKeyPath).build();
//Developer requests access token
String accessToken = getAccessToken(userApiKey);
//Developer executes an authenticated operation (e.g startEngines()) with the provided accessToken
if (startEngines(accessToken)) {
System.out.print("Client-side message: Execution allowed");
} else {
System.out.print("Client-side message: Execution denied");
}
}
您的代码将如下所示:
String path = System.getProperty("user.home") + "/.stormpath/apiKey.properties";
String applicationUrl = "https://api.stormpath.com/v1/applications/2TqboZ1qo73eDM4gTo2H94";
Client client = Clients.builder().setApiKey(ApiKeys.builder().setFileLocation(path).build()).build();
Application application = client.getResource(applicationUrl, Application.class);
public String getAccessToken(ApiKey apiKey) {
HttpRequest request = createOauthAuthenticationRequest(apiKey);
AccessTokenResult accessTokenResult = (AccessTokenResult) application.authenticateApiRequest(request);
System.out.println(accessTokenResult.getScope());
return accessTokenResult.getTokenResponse().getAccessToken();
}
public boolean startEngines(String accessToken) {
HttpRequest request = createRequestForOauth2AuthenticatedOperation(accessToken);
try {
OauthAuthenticationResult result = application.authenticateOauthRequest(request).execute();
System.out.println(result.getAccount().getEmail() + " is about to start the engines!");
doStartEngines(); //Here you will actually call your internal doStartEngines() operation
return true;
} catch (AccessTokenOauthException e) {
//This accessToken is not allowed to start the engines
System.out.print("AccessToken: " + accessToken + " just tried to start the engines. He is not allowed to do so.");
return false;
}
}
private HttpRequest createOauthAuthenticationRequest(ApiKey apiKey) {
try {
String credentials = apiKey.getId() + ":" + apiKey.getSecret();
Map<String, String[]> headers = new LinkedHashMap<String, String[]>();
headers.put("Accept", new String[]{"application/json"});
headers.put("Content-Type", new String[]{"application/x-www-form-urlencoded"});
headers.put("Authorization", new String[]{"Basic " + Base64.encodeBase64String(credentials.getBytes("UTF-8"))});
Map<String, String[]> parameters = new LinkedHashMap<String, String[]>();
parameters.put("grant_type", new String[]{"client_credentials"});
HttpRequest request = HttpRequests.method(HttpMethod.POST)
.headers(headers)
.parameters(parameters)
.build();
return request;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private HttpRequest createRequestForOauth2AuthenticatedOperation(String token) {
try {
Map<String, String[]> headers = new LinkedHashMap<String, String[]>();
headers.put("Accept", new String[]{"application/json"});
headers.put("Authorization", new String[]{"Bearer " + token});
HttpRequest request = HttpRequests.method(HttpMethod.GET)
.headers(headers)
.build();
return request;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void doStartEngines() {
System.out.println("Server-side message: Engines started!!!");
}
为了简单起见,我让所有这些代码在同一台机器上运行(客户端和服务器端代码之间没有网络通信)。您实际上需要使用Spring通过Rest API公开startEngines()
和String getAccessToken(ApiKey)
,并让您的最终用户通过网络访问它们。
试一试,它应该是一个相当简单和快速的解决方案。:)
全面披露-我在工作
发布于 2015-02-03 23:38:30
Base64是一种编码机制,而不是身份验证方案。我假设您使用的是basic authentication,这是在Authorization
HTTP头中编码的用户名和密码base64。
如果另一个服务也使用基本身份验证,并根据相同的凭据数据库检查用户名和密码,那么您将能够使用相同的base64编码字符串对另一个REST服务进行身份验证。
发布于 2015-02-04 00:48:30
我所要做的就是让类在我的第二个API中捕获所有请求,调用第一个API,它已经连接到我的数据库,并发送基本身份验证头来验证凭据。
https://stackoverflow.com/questions/28287420
复制相似问题