我有一个使用表单身份验证的基于JSF的Web应用程序。安全域在jboss
config中设置。我也有一个JAX-RS
包含REST-API的应用程序,我希望只有在第一个App中通过身份验证的用户才能访问其某些方法。
我看了一下jasig,CAS
但就我的目的而言似乎有点沉重,我想有一个更简单的解决方案,也许你们会帮我找到它。提前致谢。
您需要的可以通过Oauth解决。
您的后端(REST-API)将要求对您的API操作进行身份验证访问。反过来,您的前端(基于JSF的Web应用程序)在与后端通信时将需要发出经过身份验证的请求。这是通过发送来实现的access tokens
。
尽管这看起来很复杂,但是对您来说,查看Stormpath还是很有用的。为此,我们有一个非常简单的解决方案。请查看使用Stormpath进行API身份验证。
概括来说,您的解决方案如下所示:
在您的前端,当用户按下登录按钮时,您的前端将通过其REST API将凭据安全地发送到您的后端。
2.1。顺便说一下,Stormpath极大地增强了这里的所有可能性。您可以通过其IDSite将登录/注册功能完全委托给Stormpath,而不用拥有自己的登录页面,或者也可以将其委托给我们的Servlet Plugin。Stormpath还支持Google,Facebook,LinkedIn和Github登录。
然后,您的后端将尝试根据Stormpath后端对用户进行身份验证,并返回access token
一个结果:
/** This code will throw an Exception if the authentication fails */
public void postOAuthToken(HttpServletRequest request, HttpServletResponse response) {
Application application = client.getResource(applicationRestUrl, Application.class);
//Getting the authentication result
AccessTokenResult result = (AccessTokenResult) application.authenticateApiRequest(request);
//Here you can get all the user data stored in Stormpath
Account account = accessTokenResult.getAccount();
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
//Output the json of the Access Token
response.getWriter().print(token.toJson());
response.getWriter().flush();
}
然后,对于每个经过身份验证的请求,您的后端将执行以下操作:
public void getEquipment(HttpServletRequest request, HttpServletResponse response) {
Application application = client.getResource(applicationRestUrl, Application.class);
OauthAuthenticationResult result = (OauthAuthenticationResult) application.authenticateOauthRequest(request).execute();
System.out.println(result.getApiKey());
System.out.println(result.getAccount());
//Return what you need to return in the response
handleEquipmentRequest(response);
}
请在这里查看更多信息
希望对您有所帮助!
免责声明,我是Stormpath的活跃贡献者。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句