前端安全
为什么你需要前端安全
最近看到一个项目,登录表单直接把密码发送到服务器,没有任何加密。这存在严重的安全隐患。
反面教材
// 反面教材:不安全的登录
// components/LoginForm.jsx
export default function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
// 直接发送明文密码
const response = await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }) // 明文密码
});
if (response.ok) {
// 登录成功
}
};
return (
<form onSubmit={handleSubmit}>
<input type="text" value={username} onChange= => setUsername(e.target.value)} placeholder="用户名" />
setPassword(e.target.value)} placeholder="密码" />
登录
);
}

