前端安全:基础与实践
引言
在现代 Web 开发中,安全性至关重要。许多项目存在安全隐患,例如登录表单直接发送明文密码。
风险示例
// 不安全示例:明文传输密码
export default function LoginForm() {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
// 直接发送明文密码
await fetch('/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
};
return (
<form onSubmit={handleSubmit}>
<input type="text" value={username} onChange={(e) => setUsername(e.target.value)} placeholder="用户名" />
<input type="password" value= = => setPassword(e.target.value)} placeholder="密码" />
登录
);
}

