1

理论上,当AMPByExample服务器收到登录页面的 POST 请求时,如果凭据正确,它会将请求重定向到的 URLreturnURL并添加参数 success = true。一旦完成,AMP执行时间最终可以授权页面。

登录页面如下:

登录.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>
    </head>
    <body>
        <form method="post" action="loginauthorization">
        Correo Electronico: <input type="text" name="correo"><br>
        Contraseña: <input type="password" name="clave"><br>

        <input name="returnurl" type="hidden" value="https://cdn.ampproject.org/v0/amp-login-done-0.1.html?url=https%3A%2F%2Fampbyexample.com%2Fplayground%2F">
        
        <input type="submit" value="Ingresar">
        </form>
    </body>
</html>

如您所见,returnurl它是相同的登录 URL,AmpByExample但它不起作用。

我已经尝试通过以下方式制作自己的网址:

<input name="returnurl" type="hidden" value="https://cdn.ampproject.org/v0/amp-login-done-0.1.html?url=http%3A%2F%2Flocalhost%3A8084%2Fmypage%2Fpanel.jsp">

它也不起作用。

loginauthorization.java我收到的 servlet 中returnurl,我添加了# success = true(假设我必须验证用户名和密码,但我想先让它工作)。

登录授权.java:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class loginauthorization extends HttpServlet {
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
     try{
      response.setContentType("text/html");
     
//I get the parameters
      String email = request.getParameter("correo");
      String password = request.getParameter("clave");
      String url = request.getParameter("pageurl");
      int ridini = url.indexOf("rid=")+4;
      int ridend = url.indexOf("&url=");
      String rid = url.substring(ridini, ridend);
      String returnurl = request.getParameter("returnurl");
      
//assuming that the username and password are correct, add to the returnurl success true
      returnurl= returnurl + "#success=true";
      
//create a session    
      HttpSession session=request.getSession();
      session.setAttribute("umail",email);
      session.setAttribute("upass",password);
      session.setAttribute("rid",rid);
      session.setAttribute("returnurl",returnurl);
      
      
//redirect after login with the success = true      
      response.sendRedirect(returnurl);
      
    }catch(Exception exp){
       System.out.println(exp);
     }
  }
}

面板配置如下:

面板.jsp

<script id="amp-access" type="application/json">
    {
        "authorization": "http://localhost:8084/mypage/jsonauthorization",
        "noPingback": "true",
        "login": {
          "sign-in": "/mypage/login.jsp?rid=READER_ID&url=CANONICAL_URL&return=RETURN_URL",
          "sign-out": "/mypage/endsession"
        },
        "authorizationFallbackResponse": {
            "loggedIn": false
        },
        "type": "server"
    }
  </script>

jsonauthorization印刷品{"loggedIn": true}{"loggedIn": false}:_

jsonauthorization.java

import java.io.*;
import javax.servlet.http.*;

public class jsonauthorization extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response){
  try{
      
      response.setContentType("application/json");
      response.setHeader("AMP-Access-Control-Allow-Source-Origin", "http://localhost:8084/mypage");
      PrintWriter pwriter = response.getWriter();
      HttpSession session=request.getSession(false);
      
      if(session != null){
        String email=(String)session.getAttribute("umail");
        if(email==null){
            session.invalidate();
            pwriter.print("{\"loggedIn\":false}");
            
        }else{
            String rid;
            rid = (String) session.getAttribute("rid");
            Cookie AmpCookie = new Cookie("authorized",rid);
            AmpCookie.setPath("/");
            AmpCookie.setDomain("/mypage");
            response.addCookie(AmpCookie);
            pwriter.print("{\"loggedIn\":true}");
        }
      }else{  
        pwriter.print("{\"loggedIn\":false}");
      }
      pwriter.close();
        
  }catch(Exception exp){
      System.out.println(exp);
   }
  }
}

我很感激答案,如果错误不在returnurl请告诉我在哪里:P

4

2 回答 2

0

我想通了,没有必要配置返回url。只需在 html 中添加隐藏的输入以关闭登录窗口并读取批准登录的 json url。

像这样:

<input name = "returnurl" type = "hidden" value = "https://cdn.ampproject.org/v0/amp-login-done-0.1.html">

然后,如果 json url 批准登录,它将起作用。

实际上代码很好,问题出在 json 生成器文件中。CORS 问题。需要设置标题“AMP-Access-Control-Allow-Source-Origin”的权利。

于 2019-04-13T23:23:28.077 回答
0

我还试图弄清楚 AMP 与登录/注册的集成。不确定这是否会有所帮助,但我发现返回 url 会自动添加到 url 参数中,因此您不必将其添加到初始化 json 对象中的登录 url。

于 2019-04-12T20:19:10.793 回答