amazon-web-services - 如何使用 Eclipse JEE 从我的 Web 应用程序登录 AWS?

标签 amazon-web-services authentication web-applications

我希望我的 Web 应用程序能够登录 AWS。我正在使用 Eclipse JEE。关于框说:

Eclipse IDE for Enterprise Java Developers.

Version: 2019-03 (4.11.0)

Build id: 20190314-1200

我有以下代码:

index.html:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Hello AWS Web World!</title>
<link rel="stylesheet" href="styles/styles.css" type="text/css"
    media="screen">
</head>
<body>
    
    <h1>Post</h1>
    <div>
        <form action="LoginServlet" method="post">
            Name:<input type="text" name="name"><br>
            Password:<input type="password" name="password"><br>
            <input type="submit" value="submit">
        </form>
    </div>

    <h1>Get</h1>
    <div>
        <form action="LoginServlet" method="get">
            Name:<input type="text" name="name"><br>
            Password:<input type="password" name="password"><br>
            <input type="submit" value="submit">
        </form>
    </div>

</body>
</html>

索引.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="com.amazonaws.*" %>
<%@ page import="com.amazonaws.auth.*" %>
<%@ page import="com.amazonaws.auth.profile.*" %>
<%@ page import="com.amazonaws.services.ec2.*" %>
<%@ page import="com.amazonaws.services.ec2.model.*" %>
<%@ page import="com.amazonaws.services.s3.*" %>
<%@ page import="com.amazonaws.services.s3.model.*" %>
<%@ page import="com.amazonaws.services.dynamodbv2.*" %>
<%@ page import="com.amazonaws.services.dynamodbv2.model.*" %>

<%! // Share the client objects across threads to
    // avoid creating new clients for each web request
    private AmazonEC2         ec2;
    private AmazonS3           s3;
    private AmazonDynamoDB dynamo;
 %>

<%
    /*
     * AWS Elastic Beanstalk checks your application's health by periodically
     * sending an HTTP HEAD request to a resource in your application. By
     * default, this is the root or default resource in your application,
     * but can be configured for each environment.
     *
     * Here, we report success as long as the app server is up, but skip
     * generating the whole page since this is a HEAD request only. You
     * can employ more sophisticated health checks in your application.
     */
    if (request.getMethod().equals("HEAD")) return;
%>

<%
    if (ec2 == null) {
        AWSCredentialsProviderChain credentialsProvider = new AWSCredentialsProviderChain(
            new InstanceProfileCredentialsProvider(),
            new ProfileCredentialsProvider("default"));

        ec2    = new AmazonEC2Client(credentialsProvider);
        s3     = new AmazonS3Client(credentialsProvider);
        dynamo = new AmazonDynamoDBClient(credentialsProvider);
    }
%>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Hello AWS Web World!</title>
    <link rel="stylesheet" href="styles/styles.css" type="text/css" media="screen">
</head>
<body>
    <div id="content" class="container">
        <div class="section grid grid5 s3">
            <h2>Amazon S3 Buckets:</h2>
            <ul>
            <% for (Bucket bucket : s3.listBuckets()) { %>
               <li> <%= bucket.getName() %> </li>
            <% } %>
            </ul>
        </div>

        <div class="section grid grid5 sdb">
            <h2>Amazon DynamoDB Tables:</h2>
            <ul>
            <% for (String tableName : dynamo.listTables().getTableNames()) { %>
               <li> <%= tableName %></li>
            <% } %>
            </ul>
        </div>

        <div class="section grid grid5 gridlast ec2">
            <h2>Amazon EC2 Instances:</h2>
            <ul>
            <% for (Reservation reservation : ec2.describeInstances().getReservations()) { %>
                <% for (Instance instance : reservation.getInstances()) { %>
                   <li> <%= instance.getInstanceId() %></li>
                <% } %>
            <% } %>
            </ul>
        </div>
    </div>
</body>
</html>

登录Servlet.java

package package10_2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="LoginServlet", urlPatterns= {"/"})
public class LoginServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("doPost");
    
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        
        out.println("doPost");

        Boolean authenticated = request.authenticate(response);
        System.out.println("authenticated=" + authenticated);
        out.println("authenticated=" + authenticated);
        
        String authType = request.getAuthType();
        System.out.println("auth type =" + authType);
        out.println("auth type =" + authType);
        
        String remoteUser = request.getRemoteUser();
        System.out.println("remote user =" + remoteUser);
        out.println("remote user =" + remoteUser);
        
        out.close();

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("doGet");

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        
        out.println("doGet");

        String authType = request.getAuthType();
        System.out.println("auth type =" + authType);
        out.println("auth type =" + authType);
        
        String remoteUser = request.getRemoteUser();
        System.out.println("remote user =" + remoteUser);
        out.println("remote user =" + remoteUser);
        
        out.close();
        
    }

}

当我使用第一个使用 post 方法的表单时,我输入了正确的用户名和密码,但是当我点击提交时,我得到一个登录对话框,要求输入用户名和密码。在这种情况下,用户名是 tina。以下是之后网站上显示的内容:

doPost authenticated=true auth type =BASIC remote user =tina

当我使用第二种使用 get 方法的表单时,我输入了正确的用户名和密码,但是当我点击提交时,网站显示:

doGet auth type =null remote user =null

我应该使用post方式还是get方式登录?我想我应该使用 get 方法。

如何让 post 方法处理用户在 index.html 页面的表单中输入的用户名和密码值?

最佳答案

Http GET 不支持表单提交,必须使用JS 将它们添加到查询参数上。 但它存在安全风险。

所以通常 GET 不是处理登录的好方法。

关于amazon-web-services - 如何使用 Eclipse JEE 从我的 Web 应用程序登录 AWS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60119700/

相关文章:

python - 使用 awsebcli 部署 Elastic Beanstalk 应用程序时出现“操作被拒绝”错误

wcf - IIS 6.0 上 WCF 中的集成 Windows 身份验证

java - Spring 安全: how to change user roles without login and logout

amazon-web-services - AWS Cloudformation 的版本控制和管道

amazon-web-services - 使用java代码创建RDS实例的只读副本

linux - 如何在没有 ppk 文件的情况下从移动应用程序连接到 linux 服务器?

带有刷新 token 的 ASP.NET 个人帐户

c# - 获取服务器上的硬件信息

python - 使用 python 提取网页上的 URL 列表的简单方法是什么?

javascript - 根据目标设备 RAM 限制 DOM 元素的数量