我正在尝试为 JBPM 服务器中的服务主机创建 RestEasy 客户端。服务 URL 始终重定向到基于表单的登录屏幕,该屏幕需要 j_username 和 j_password。
我需要登录该服务,并且还必须存储 cookie 以避免每次都进行身份验证。请建议实现此目的的最佳实现。
现在所有服务调用最终都会返回登录 html 页面。
我尝试了此处发布的一些解决方案,但在我的场景中不起作用。
RESTEasy client framework authentication credentials
RestEasy Client Authentication and HTTP Put with Marshalling
最佳答案
首先,编写一个身份验证 Servlet(您可以在其中拦截登录凭据并将其存储到您的 Cookie 中):
@WebServlet(urlPatterns = {"/security_check"}) public class AuthenticationServlet extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException { request.login(userName, userPassword); StoreInCookieMethod(request.getUserPrincipal().getName(), userPassword); response.sendRedirect(request.getContextPath() + "/protectedResourceUrlPattern/"); } }
在
login_form
中,将操作映射到 servlet URL 例如:<form method="post" action="security_check">
对于除登录之外的所有其他请求,定义 URL 模式(例如 protectedResourceUrlPattern)并使用 cookie 中的凭据进行身份验证
关于java - RestEasy Web 服务客户端基于表单的身份验证支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861643/