问题描述
我有一个 Spring Boot 应用程序,它用作 api。如果请求,它会从我的数据库返回 json 数据。现在我想运行一个网站,在那里我使用从我的 api 收到的数据。我想到了使用 Spring Security 来验证想要获取我的数据的用户。在客户端,我知道的唯一解决方案是在我的 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);
}
}
解决方法
您应该考虑使用另一种方法来保护您的 API,例如 JWT。下面是一个很好的例子:https://dzone.com/articles/spring-boot-security-json-web-tokenjwt-hello-world