如何向需要密码的api发出http请求,而不在javascript代码中写入密码?

ma8fv8wu  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(267)

我有一个spring启动应用程序,它可以作为api使用。如果请求,它将从我的数据库返回json数据。现在我想运行一个网站,在那里我使用的数据,我从我的api收到。我考虑使用springsecurity对想要获取我的数据的用户进行身份验证。在客户端,我知道的唯一解决方案是在我的javascript文件中写入用于身份验证的用户名和密码,并将其发送到api。问题是,如果我管理这个网站,每个人都可以阅读。有没有其他解决办法?

function req(method, url){
    var xmlhttp = new XMLHttpRequest();                     
    xmlhttp.open(method, url, false);`
    //And somehow also sent username and password here
    xmlhttp.send(); 
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        return JSON.parse(xmlhttp.responseText);
    }
}
dgenwo3n

dgenwo3n1#

您应该考虑使用另一种方法来保护api,比如jwt。下面是一个很好的例子:https://dzone.com/articles/spring-boot-security-json-web-tokenjwt-hello-world

相关问题