使登录到另一个站点javaEE的按钮
我在瓦丁制造的平台上工作。如果您想查看,请访问www.wikre.it。 在本主页中,要登录,您必须单击“Accedi”,然后将文本字段编译为显示的模式表单 该平台还有另一个应用程序(与前面提到的应用程序不同),用作后台 其目的是在后台制作一个按钮,用一些凭据登录站点(如何获取并不重要) 我是这个平台的新手,我有一些想法,但我不知道该做什么(作为工作流): -休息服务 -来自站点的Web服务 我怎样才能达到我的目的使登录到另一个站点javaEE的按钮,java,vaadin,Java,Vaadin,我在瓦丁制造的平台上工作。如果您想查看,请访问www.wikre.it。 在本主页中,要登录,您必须单击“Accedi”,然后将文本字段编译为显示的模式表单 该平台还有另一个应用程序(与前面提到的应用程序不同),用作后台 其目的是在后台制作一个按钮,用一些凭据登录站点(如何获取并不重要) 我是这个平台的新手,我有一些想法,但我不知道该做什么(作为工作流): -休息服务 -来自站点的Web服务 我怎样才能达到我的目的 抱歉,如果这是一个一般性的问题,但我需要它为我的工作,我不知道该做什么为第一 在
抱歉,如果这是一个一般性的问题,但我需要它为我的工作,我不知道该做什么为第一 在不改变现有站点的情况下,可以这样做的一种方法是:
POST
)请求,该请求由一些用户操作触发,如在backoffice应用程序中单击按钮或链接,并带有必要的参数(用户名、密码、最终隐藏字段)指向您的站点登录地址(我相信在您的情况下是这样的)HttpClient
实例发送至少一个用于身份验证的cookie——获取它:)(在我提供的示例中——见下文——我假设此cookie名为JSESSION,这是Java应用程序创建用户会话的常见情况;如果您的站点使用了不同的技术,如PHP等。请确保您了解该技术的会话/身份验证cookie的外观)wikire.it
和/
作为路径),该请求是在后台完成的(请记住:A.您从站点收到的身份验证cookie目前是为HttpClient
实例设置/提供的,而不是为实际的客户端(即您的浏览器);B.在侦听器中处理Vaadin事件,最终意味着将向您的浏览器发送一个响应窗口。打开('http://www.wikire.it/)
JavaScript调用(即,传递目标站点的地址,可能还有,“\u blank”作为第二个参数,以强制在新窗口/选项卡中打开页面;这可能会被不允许打开弹出窗口的浏览器阻止,因此…小心)我在这里创建了两个示例项目:。是一个非常基本的java/jsp应用程序,受登录页面(您可以用来登录的用户名是Johnny,密码不重要)的保护,作为登录的目标站点。是一个小型Vaadin应用程序,带有一个按钮,您可以单击以登录
站点
为方便起见,我在下面突出显示相关的代码位
在HttpClient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
org.apache.httpcomponents
httpclient
4.5.3
创建登录站点并返回身份验证Cookie的例程
这意味着您知道用于登录操作的路径(通常是登录表单中指定的路径)
private Cookie登录(URI targetUri、字符串loginPath、映射参数)引发IOException{
requirennull(targetUri);
requirennoull(loginPath);
//跟踪我们可能在HttpClient中收到的Cookie:
最终CookieStore cookies=新BasicCookieStore();
//构建并使用使用cookie存储的(可自动关闭的)HttpClient:
try(CloseableHttpClient=HttpClients.custom().setDefaultCookieStore(cookies.build()){
//准备(登录)请求参数:
List reqParams=新建ArrayList();
如果(参数!=null){
对于(Map.Entry:params.entrySet()){
添加(新的BasicNameValuePair(entry.getKey(),entry.getValue());
}
}
//使用给定参数执行登录(POST)请求:
HttpPost=newhttppost(targetUri+loginPath);
post.setEntity(新的UrlEncodedFormEntity(reqParams));
CloseableHttpResponse response=client.execute(post);
//最后,检查响应是否成功
response.close();
//查找HttpClient中存储的名为cookie的JSESSIONID,并返回该ID以供调用代码使用:
for(org.apache.http.cookie.cookie:cookies.getCookies()){
if(“JSESSIONID.equalsIgnoreCase(cookie.getName())){
String domain=targetUri.getHost();
if(domain.startsWith(“www.”){
domain=domain.substring(4);
}
Cookie authCookie=新Cookie(Cookie.getName(),Cookie.getValue());
authCookie.setDomain(域);
authCookie.setPath(“/”);
//最后,设置到期日(允许更长的登录时间)和其他事项。。。
返回authCookie;
}
}
返回null;//某种错误?
}
}
在浏览器中设置Auth Cookie并在Vaadin中打开站点
@Title("Backoffice for SO Question #42927030")
public class MainUI extends UI {
private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
// ...
}
@Override
protected void init(VaadinRequest vaadinRequest) {
setContent(new VerticalLayout(new Button("Log into site...", event -> {
try {
URI targetUri = new URI("http://localhost:8080");
Map<String, String> params = new HashMap<>();
params.put("username", "Johnny");
params.put("password", "incorrect :)");
// Eventual hidden fields, etc.
// params.put("...", "...");
Cookie targetAuthCookie = login(targetUri, "/log-me-in", params);
// We're not ready just yet: we still need to 'transfer' the cookie
// the HTTP client received to the current browser:
VaadinService.getCurrentResponse().addCookie(targetAuthCookie);
// Upon responding to the Vaadin 'click' request, open the target URL (eventually in a new page / tab):
Page.getCurrent().getJavaScript().execute("window.open('" + targetUri + "');");
} catch (Exception ex) {
ex.printStackTrace();
}
})));
}
@WebServlet(urlPatterns = "/*", name = "MainUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MainUI.class, productionMode = false)
public static class MainUIServlet extends VaadinServlet {}
}
@Title(“SO问题的后台办公室#42927030”)
公共类MainUI扩展了UI{
私有Cookie登录(URI targetUri、字符串loginPath、映射参数)引发IOException{
// ...
}
@凌驾
受保护的void init(VaadinRequest VaadinRequest){
setContent(新建垂直布局)(新建按钮(“登录站点…”),事件->{
试一试{
URI targetUri=新URI(“http://localhost:8080");
M
@Title("Backoffice for SO Question #42927030")
public class MainUI extends UI {
private Cookie login(URI targetUri, String loginPath, Map<String, String> params) throws IOException {
// ...
}
@Override
protected void init(VaadinRequest vaadinRequest) {
setContent(new VerticalLayout(new Button("Log into site...", event -> {
try {
URI targetUri = new URI("http://localhost:8080");
Map<String, String> params = new HashMap<>();
params.put("username", "Johnny");
params.put("password", "incorrect :)");
// Eventual hidden fields, etc.
// params.put("...", "...");
Cookie targetAuthCookie = login(targetUri, "/log-me-in", params);
// We're not ready just yet: we still need to 'transfer' the cookie
// the HTTP client received to the current browser:
VaadinService.getCurrentResponse().addCookie(targetAuthCookie);
// Upon responding to the Vaadin 'click' request, open the target URL (eventually in a new page / tab):
Page.getCurrent().getJavaScript().execute("window.open('" + targetUri + "');");
} catch (Exception ex) {
ex.printStackTrace();
}
})));
}
@WebServlet(urlPatterns = "/*", name = "MainUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MainUI.class, productionMode = false)
public static class MainUIServlet extends VaadinServlet {}
}