小编Alp*_*a2k的帖子

如何保护Ajax链接请求?

想象一下下一个场景:用户想要注册到网页并填写表格.当他填写表单时,如果字段有效,jQuery会继续检查正则表达式等等...

将电子邮件作为用户在注册登录后将使用的主键,需要使用Ajax检查电子邮件字段,以便用户知道该电子邮件是否已注册.我想用Ajax检查它,以避免发送完整的表单并清空它,刷新页面等...

因此,当用户结束填写电子邮件字段时,Ajax请求将发送到服务器,类似于下一个链接:

example.com/check.php?email=abcdefg@gmail.com
Run Code Online (Sandbox Code Playgroud)

当check.php收到电子邮件时,它会询问数据库是否存在,并返回如下消息:User already exists如果用户存在或者null用户不存在.

问题是:如果有人通过我的.js挖掘并发现类似的链接,他们可以使用该链接发送大量请求以查明是否存在这些随机电子邮件.这可能导致大量使用数据库,或者在最坏的情况下甚至导致崩溃和私人信息泄露.

有人可以做一个巨大的for循环检查电子邮件,如:

//Getting the response of the next links
example.com/check.php?email=aaaaaaa@gmail.com // Returns null
example.com/check.php?email=aaaaaab@gmail.com // Returns null
example.com/check.php?email=aaaaaac@gmail.com // Returns null
example.com/check.php?email=aaaaaad@gmail.com // Returns User already exists
Run Code Online (Sandbox Code Playgroud)

-------------------------------------------------- -------------------------------------------------- --------------------------------

自从我上次接受答案以来,我一直在调查这个问题,并找到了避免这种行为的解决方案.以下代码适用于JAVA,但逻辑可以应用于任何其他服务器端语言.

在对服务器执行任何ajax请求之前,我向服务器请求令牌.这个令牌看起来像这fmf5p81m6e56n4va3nkfu2ns8n是一个简单的方法,但它可以更复杂,但这很好.

public String getToken() throws UnsupportedEncodingException {
    return new BigInteger(130, new SecureRandom()).toString(32);
}
Run Code Online (Sandbox Code Playgroud)

在请求令牌时,服务器不仅会返回令牌,还会返回一个小脚本,以防万一有人使用浏览器检查元素(和浏览器导航栏),这样的脚本将运行并且令牌将被清除.Servlet返回如下内容:

_html += "<head>"
    + "<script> "
    + "window.onload=function(){\n"
    + "       document.body.innerHTML = \"\";\n"
    + "    }"
    + …
Run Code Online (Sandbox Code Playgroud)

javascript php ajax jquery

39
推荐指数
2
解决办法
3万
查看次数

MomentJS西班牙语时间

我正在使用下一个代码来转换从MySQL数据库格式接收的日期1993-10-23 00:00:00并以西班牙语显示它:

alert(moment('1993-10-23 00:00:00', 'YYYY-MM-DD', 'es')); 
Run Code Online (Sandbox Code Playgroud)

星期六23点.我希望得到,sábado但我得到了下一个:

Sat Oct 23 1993 00:00:00 GMT+0200
Run Code Online (Sandbox Code Playgroud)

也尝试添加:moment.locale('es-ES');,moment.locale('en-ES');moment.locale('es'); 但既不工程.

将日期从一种语言转换为另一种语言的正确方法是什么?

javascript momentjs

8
推荐指数
1
解决办法
1万
查看次数

JavaScript浏览器导航栏事件

我想阻止用户导航到不通过html元素访问的URL.例:

实际导航: myweb.com/news

我想myweb.com/news?article_id=10通过在浏览器导航栏中编写此内容来导航,以避免按任何元素(如<a>).

当用户myweb.com/news?article_id=10在浏览器URL中写入时,在他按下回车的那一刻,浏览器不应该允许他导航到该URL.

我试过了:

//This wont work since jquery does not support it
$(window.location.href).on('change', function() {
    //Here check if href contains '?'
    alert("Not allowed");
});

//Neither works, doesnt do anything
$(window).on('change', function() {
    alert("Not allowed");
});
Run Code Online (Sandbox Code Playgroud)

参考文献: 这里有类似问题On - window.location.hash - 更改?,但我对该问题的'参数'版本感兴趣.

html javascript jquery

5
推荐指数
1
解决办法
160
查看次数

通过 Nginx 在 Websocket 转发用户 IP

我使用此 Websocket 库和此处的示例来创建基于 Websocket 的多个服务。

接受请求的主服务器是 Nginx 服务器,它根据请求的来源(基于域)转发请求,因此我可以确定收到的请求是针对 Websocket 的。

然而问题就在这里:

public void onOpen(WebSocket ws, ClientHandshake handshake) {
    System.out.println("-------------------------------");
    System.out.println(ws.getRemoteSocketAddress());
    System.out.println("-------------------------------");
}
Run Code Online (Sandbox Code Playgroud)

getRemoteSocketAddress将始终返回 nginx 服务器 IP,例如/192.168.1.100:43556

我的 nginx 配置是:

server{
    listen 80;
    server_name we.mydomain.com;
    access_log /var/log/nginx/access-ws-debug.log;
    error_log /var/log/nginx/error-ws-debug.log;

    set_real_ip_from  192.168.1.0/24;
    real_ip_header X-Real-IP;
    real_ip_recursive on;

    location / {
       proxy_pass http://192.168.1.101:54321;

       proxy_http_version 1.1;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header Host $http_host; #host
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Y-Real-IP $realip_remote_addr;

       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";

       proxy_connect_timeout 1d;
       proxy_send_timeout 1d;
       proxy_read_timeout …
Run Code Online (Sandbox Code Playgroud)

java nginx websocket

5
推荐指数
1
解决办法
1592
查看次数

如何刷新客户端 INLINE javascript

当使用外部 js 文件时,浏览器可能会被强制重新加载文件。见这里

最近,我发现 INLINE 脚本也被缓存,至少在 Chrome 版本 80.0.3987.132 中,片段示例:

<html>
    <head>
        <script>alert("I am cached!");</script>
    </head>

    <body>
        <script>alert("Me too!");</script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

刷新内联脚本的方法是什么?


更新 1:我不得不提到返回内容的网络服务器正在使用 HTTP 2.0

更新 2:一个有效的解决方案是以辅助脚本为基础,当页面加载时通过 ajax 或 websocket 获取“真实”脚本内容,然后将其附加到头部,如下所示:

function addScript(content){
    let s = document.createElement('script');
    s.innerHTML = content;
    document.head.appendChild(s);
}
Run Code Online (Sandbox Code Playgroud)

这可以完成工作,但它不是最佳的,因为它需要比必要更多的请求。

更新 3:从后端发送的标头似乎都不起作用,使用这些标头:

Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
Header().Set("Pragma", "no-cache") // HTTP 1.0.
Header().Set("Expires", "0") // Proxies.
Run Code Online (Sandbox Code Playgroud)

更新 4:根据 Jinxmcg 的回答,文档https://v8.dev/blog/code-caching-for-devs Don’t change URLs提到:

我们可能有一天决定将缓存与源文本而不是源 URL 相关联,这个建议将不再有效。

可能这一天已经到来,也适用于内联脚本。


谢谢大家的参与 …

javascript caching google-chrome refresh

5
推荐指数
1
解决办法
907
查看次数

Netbeans 8.0.2尚未部署该模块

我刚刚安装了netbeans,我在部署新的Java Web App时遇到了问题.我只是创建项目而不编辑任何东西,这是默认情况下的项目(使用apache):

index.html

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div>TODO write content</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

错误:

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
In-place deployment at C:\Users\2kGamer\Dropbox\Projects\Java\NetBeans\DAW 2\WebApplication1\build\web
Deployment is in progress...
deploy?config=file%3A%2FC%3A%2FUsers%2F2kGamer%2FAppData%2FLocal%2FTemp%2Fcontext4402830100786872488.xml&path=/WebApplication1
 http://localhost:8084/manager/text/deploy?config=file%3A%2FC%3A%2FUsers%2F2kGamer%2FAppData%2FLocal%2FTemp%2Fcontext4402830100786872488.xml&path=/WebApplication1
C:\Users\2kGamer\Dropbox\Projects\Java\NetBeans\DAW 2\WebApplication1\nbproject\build-impl.xml:1045: The module has not …
Run Code Online (Sandbox Code Playgroud)

java apache tomcat netbeans

4
推荐指数
1
解决办法
12万
查看次数

Java用时间戳计算时间

我试图计算两个时间戳之间的时间差,这是代码:

        Calendar calendar = Calendar.getInstance();
        java.util.Date now = calendar.getTime();
        Timestamp currentTimestamp = new Timestamp(now.getTime());
        System.out.println("Current\n"+currentTimestamp);

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = dateFormat.parse("28/02/2015");
        Timestamp timestampBefore = new Timestamp(date.getTime());
        System.out.println("Before\n"+timestampBefore);

        Timestamp calculated = new Timestamp(currentTimestamp.getTime() - timestampBefore.getTime());
        System.out.println("Calculated\n"+calculated);
Run Code Online (Sandbox Code Playgroud)

输出:

Current
2015-02-28 12:12:40.975
Before
2015-02-28 00:00:00.0
Calculated
1970-01-01 13:12:40.975
Run Code Online (Sandbox Code Playgroud)
  1. 我可以理解为什么它会返回1970-01-01,但为什么它会13:12:40.975多返回 1 小时?

  2. 如何计算两个日期之间的差异,以便输出如下(基于此示例):
    年:0,月:0,天:0,小时:12,分钟:12,秒:40?

更新:对于低于 1.8 的 java,请查看http://www.joda.org/joda-time/index.html
,对于 java 1.8,请参阅答案。

此处类似的解决方案: Java 8:计算两个 LocalDateTime 之间的差异

java timestamp calendar date

3
推荐指数
1
解决办法
8752
查看次数

Oracle select*和rownum

我试图从表中选择所有值+按字母顺序排序后为每一行添加rownum.(使用java,这是查询)

Select * FROM
  (Select *, rownum as row_id from
          (Select * FROM emp ORDER BY ename))
                Where row_id>=((?-1)*?)+1 and row_id<=(?*?);
Run Code Online (Sandbox Code Playgroud)

但是我得到了

java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected
Run Code Online (Sandbox Code Playgroud)

我相信它是因为Select *, rownum as row_id from选择所有字段和添加新字段的正确方法是什么?

java sql oracle

2
推荐指数
1
解决办法
1571
查看次数

Java - 反射getDeclaredMethods在声明的顺序奇怪的行为

这是一个奇怪的行为getDeclaredMethods,这里是场景,一个名为Entity的类:

public class Entity {
private Object reference;

/**
 * @return the reference
 */
public Object getReference() {
    return reference;
}

/**
 * @param reference the reference to set
 */
public void setReference(Object reference) {
    this.reference = reference;
}

public Object getReference2() {
    return reference;
}

public void setReference2(Object reference) {
    this.reference = reference;
}
}
Run Code Online (Sandbox Code Playgroud)

而主要课程:

public static void main(String Args[]){
    try {
        Entity _entity = new Entity();


        Class _newClass = _entity.getClass();
        Method[] _method = _newClass.getDeclaredMethods();
        for …
Run Code Online (Sandbox Code Playgroud)

java reflection

1
推荐指数
1
解决办法
1942
查看次数

Oracle SQL Developer - 计数函数

输出

这是 select * from table1 的输出,我对 count 函数有疑问...我想计算 NULL,为了做到这一点,正确的选项是这样做:

select count(*) from table1 where fecha_devolucion is null --> 这给了我正确的答案计数 1 但是如果我这样做:

select count(fecha_devolucion)
  from table1
  where fecha_devolucion is null --> this returns 0, why? Isn't the same syntax? 
Run Code Online (Sandbox Code Playgroud)

从表中选择特定字段和 * 有什么区别?

sql oracle

0
推荐指数
1
解决办法
1万
查看次数

Java - 实例化未知数量的实体

我有一个名为Person的实体,其中有2个参数:Name和Age.
我不知道我可能需要多少个实体,这是我如何使用预定义数量的实体:

private Person[] person = new Person[3]; //With this I'm saying that there will be only 3... 
Run Code Online (Sandbox Code Playgroud)

现在,我不知道我有多少人,我怎么能用List做到这一点?

List<Person> personList = new List<Person>(); //random guessing
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
40
查看次数

String不是JavaScript函数

我想为一个字段设置一个值,这是html:

<form name="myForm"> 
<input type="text" id="user">
</form>
Run Code Online (Sandbox Code Playgroud)

和javascript:

window.onload = init;
function init() {
generateUser();
//more code... 
}

function generateUser(){
var generated = "usr_"+Math.floor((Math.random() * 1000) + 1);
document.myForm.user.value(generated);
}
Run Code Online (Sandbox Code Playgroud)

我找不到设置该值的方法,总是得到"String not a function".

javascript

0
推荐指数
1
解决办法
102
查看次数

将<string>转换为float []

得到这个代码(不工作):

    for (int i = 0; i < splitSource.length; i++) {
        float[] nr = Float.parseFloat(splitSource[i]);
    }
Run Code Online (Sandbox Code Playgroud)

我有一系列字符串......

  List<String> stringCollection = new ArrayList<>();  
Run Code Online (Sandbox Code Playgroud)

以前,列表中的每个字符串都单独处理,从中提取必要和不必要的字符,最终结果是一串纯数字.
现在,我需要将这些数字从String转换为Float,但我得到"float不能转换为float []"的错误...

java

-4
推荐指数
1
解决办法
95
查看次数