【问题标题】:Ways to validate Http request In Java?在 Java 中验证 Http 请求的方法?
【发布时间】:2018-08-28 11:26:44
【问题描述】:

我正在尝试在我的服务器中验证 HTTP 请求。 http 客户端可以向我发送不同的请求,其中一些请求可能与我的服务器不匹配,因此在这种情况下,我必须向客户端发送响应,说明请求有问题。

在继续阅读之前,请考虑更改字段名称的顺序不会影响响应。因此字段的顺序无关紧要。

例如: 这是法律要求:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = 123456
Item_Id = 5007
Color = 1010

(注意:如果请求只有这些字段名称,则该请求是合法的, 如果它包含任何其他字段名称,则将被视为非法)

因为这是有效请求,我的服务器将发送 200 OK 作为响应,如下所示:

HTTP/1.1 200 OK
Connection: Close
Payment: 2755201

但是,例如,如果请求具有错误的字段值或未知字段,如下所示: 错误的 User_Id 值:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = -2323
Item_Id = 5007
Color = 1010

响应将是:

HTTP/1.1 200 OK
Connection: Close
Error_ID: 602
Error_String: Error in user_Id (illegal user ID)

或未知的字段名称:

POST http://localhost HTTP/1.1
HOST: (here is an IP adress)
Content-Length: 50
Connection: Close
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
User_Id = 123456
Item_Id = 5007
Sale = 0.5
Color = 1010

响应将是:

HTTP/1.1 200 OK
Connection: Close
Error_ID: 602
Error_String: Sale , unknown field name

作为一种验证方式,我考虑将所有有效字段名称放入枚举中,当我收到请求时,我会根据此枚举分析它们,但我有更多有效字段(不仅是 user_Id ,item_Id 和 color )所以应用这种方式可能需要很长时间并且可能效率低下,尤其是因为字段的顺序并不重要。

这是我的服务器代码:

@Test
    public void main() throws IOException
    {
        serverSocket = new ServerSocket(80);
        Socket clnt = null;
        while (true)
        {
            clnt = serverSocket.accept();
            System.out.println("after accept in server");
            BufferedReader request = new BufferedReader(new InputStreamReader(clnt.getInputStream()));
            BufferedWriter response = new BufferedWriter(new OutputStreamWriter(clnt.getOutputStream()));

            String req = ".", strReq = "";
            try
            {
                req=request.readLine();
                while (!req.equals(""))
                {
                    System.out.println(req);
                    strReq += (req + "\n");
                    req=request.readLine();
                }
            }
            catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
                System.out.println("xxxx");
            }
            System.out.println(strReq);
            String httpResponse = "HTTP/1.1 200 OK\r\n";
            //here will be the rest of the response
            response.write(httpResponse);
            System.out.println(httpResponse);
            response.flush();
        }
    }

我是 http 的新手,所以如果有一些验证库或任何其他更有效的验证方式,如果你能提出建议,我将不胜感激。

【问题讨论】:

  • 您是否有理由编写自己的 HTTP 服务器并根据您的(静态)要求验证事物?第一部分很难做好,第二部分不应该包含在服务器本身中。
  • 因为我想发送一个与我收到的请求相匹配的便捷响应
  • @Kayaman 第二部分您的意思是构建响应?
  • 不,第一部分很难(编写适当的 HTTP 服务器),第二部分(验证)不应该包含在服务器中。你是为学校项目写这篇文章还是什么?
  • 是的,它来自一个新项目

标签: java apache http http-post


【解决方案1】:

使用 Spring mvc 控制器。它使您的生活更轻松。验证将自动进行。例如

  @RequestMapping(value = "user", method = RequestMethod.POST) 
  public Response addUser(@RequestBody @Valid User user) {
    return userService.addUser(user);
  }

public class User implements Serializable {

private Long id;
@NotNull
@NotEmpty
private String userName;
@NotNull
@NotEmpty
private String password;
@NotNull
@NotEmpty
private String firstName;
private String lastName;

//all getters and setters here

}
@ControllerAdvice
@RestController
public class GlobalExceptionController {

 @ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public  Response handleBadRequest(Exception ex) {
    Response response = new Response();
    response.setStatus(HttpStatus.BAD_REQUEST.value());
    response.setMessage(ex.getMessage());
    return response;
}
}

所有验证都将由注解@Valid 处理。如果需要,可以在 Spring 全局异常控制器中处理异常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2022-08-09
    • 2019-09-02
    相关资源
    最近更新 更多