node.js - 网站的基本身份验证,以保护网站登录和 API 访问
问题描述
我正在研究正在开发的网站的安全模型。在研究了网络之后,我发现有几种安全模型可以保护网站,即 Basic Auth、JWT ...
目前,SSL 尚未启用,因为仍在开发中。网站有一个登录页面并通过 API 进行通信(包括登录和注销)。在登录页面上,作为测试,我尝试使用虚假详细信息登录,然后我查看了开发人员工具以识别安全机制,发现以下截图。我认为该站点正在使用基本身份验证,尽管我注意到电子邮件/密码未编码并且使用自定义登录表单。有人可以确认是否使用了基本身份验证?
[请求标题图片][2]
更新:我发现一旦用户通过电子邮件/密码进行身份验证,我应该发布屏幕截图,因为这是返回密钥的地方。在下面的屏幕截图中,投标人令牌和投标人秘密被发送回客户端。我认为这些是通过后端加密生成的。所以我认为它不是 JWT,但这是创建密钥而不是在标头中发送而是在响应正文中发送的合适方法吗?
登录表格代码:
{
/* prepare ui */
progress.classList.remove('hide');
login_btn.innerText = 'Logging In';
login_btn.setAttribute('disabled', true);
/* make http request */
var http = new XMLHttpRequest();
var url = SERVER + '/api/bidder/login';
var body = {
email: email.value,
password: password.value
};
http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/JSON');
http.onreadystatechange = function () { //Call a function when the state changes.
if (http.readyState == 4 && http.status == 200) {
var res = JSON.parse(http.responseText);
if (res.status) {
localStorage.setItem("bidData", JSON.stringify(res.data));
window.location.href = window.location.href.replace('login.html','');
} else {
Toast.show('Danger', res.message);
}
/* reset ui */
progress.classList.add('hide');
login_btn.innerText = 'Log In';
login_btn.removeAttribute('disabled');
}
}
http.send(JSON.stringify(body));
}
解决方案
Spring Security 是 Spring 项目中最基本的身份验证如果要启用 Spring Security,首先必须将 Spring Security 库添加到您的项目中。添加后,您创建一个类来配置 Spring 安全性。
Spring 安全性的类配置中的一个函数。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/api/statistical/**",
"/static/**",
"/webjars/**",
"/img/**",
"/css/**",
"/js/**",
"/api/diary/**")
.permitAll()
.antMatchers("/api/auth/**")
.permitAll()
.antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability")
.permitAll()
.antMatchers(HttpMethod.GET, "/api/users/**") //, "/api/polls/**"
.permitAll()
.anyRequest()
.authenticated();
推荐阅读
- java - 当两个对象具有相同的值时,为什么我的程序不能将它们识别为相等?
- html - 显示图像时使用 laravel 出错的地方
- java - USB 错误 9:无法提交控制消息:管道错误
- android - 可以使用哪种设计模式来代替 Manager 类?
- css - Normalize.css/Reset CSS 是否包含在 Angular/React/Vue.js 中?
- c# - 自动生成的 EF Core 上下文和实体(.efm、.edps 和 .view 文件)
- sql - 如何选择在 SQL 中某个操作之后立即发生的值?
- c++ - 如何在标头和 .cpp 文件中正确声明返回自定义类型的函数?
- node.js - Issues connecting to mosquitto broker with node mqtt client via SSL/TLS
- bash - 为什么这个带有子shell的嵌套bash命令会挂起?