用MVC模式进行后端管理员RSA加密登录具体代码实现
时间: 2023-05-28 21:01:42 浏览: 223
ASP.NET MVC结合JavaScript登录、校验和加密
这里提供一个示例代码实现,使用Java语言和Spring框架:
1. Model层
管理员信息模型类Admin:
```java
public class Admin {
private Integer id;
private String username;
private String password;
// 省略getter和setter方法...
}
```
2. View层
管理员登录的控制器AdminLoginController:
```java
@Controller
@RequestMapping("/admin")
public class AdminLoginController {
@Autowired
private AdminService adminService;
@GetMapping("/login")
public String loginPage() {
return "admin/login";
}
@PostMapping("/login")
@ResponseBody
public ResultVO login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
Admin admin = adminService.findByUsername(username);
if (admin == null) {
return ResultVO.fail("用户名不存在!");
}
// 对密码进行RSA加密
String encryptedPassword = RSAUtil.encrypt(password, admin.getPublicKey());
if (!admin.getPassword().equals(encryptedPassword)) {
return ResultVO.fail("密码错误!");
}
// 登录成功,将管理员信息存入Session
session.setAttribute("admin", admin);
return ResultVO.success();
}
}
```
3. Controller层
管理员业务逻辑的服务类AdminService(核心代码是获取RSA公钥并保存到数据库中):
```java
@Service
public class AdminService {
@Autowired
private AdminMapper adminMapper;
@Autowired
private RSAUtil rsaUtil;
public Admin findByUsername(String username) {
Admin admin = adminMapper.findByUsername(username);
if (admin != null && admin.getPublicKey() == null) {
// 如果RSA公钥不存在,则生成新的公钥并保存到数据库中
Map<String, Object> keyMap = rsaUtil.genKeyPair();
String publicKey = rsaUtil.getPublicKey(keyMap);
admin.setPublicKey(publicKey);
adminMapper.updatePublicKeyById(admin.getId(), publicKey);
}
return admin;
}
}
```
4. 工具类
RSA加密算法的工具类RSAUtil(使用了Bouncy Castle库):
```java
@Component
public class RSAUtil {
// 生成RSA密钥对
public Map<String, Object> genKeyPair() {
try {
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
Map<String, Object> keyMap = new HashMap<>();
keyMap.put("private", privateKey);
keyMap.put("public", publicKey);
return keyMap;
} catch (Exception e) {
throw new RuntimeException("RSA密钥生成失败!", e);
}
}
// 获取RSA公钥字符串
public String getPublicKey(Map<String, Object> keyMap) {
try {
PublicKey publicKey = (PublicKey) keyMap.get("public");
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
} catch (Exception e) {
throw new RuntimeException("RSA公钥获取失败!", e);
}
}
// RSA加密
public String encrypt(String plaintext, String publicKeyStr) {
try {
Security.addProvider(new BouncyCastleProvider());
byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ciphertextBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(ciphertextBytes);
} catch (Exception e) {
throw new RuntimeException("RSA加密失败!", e);
}
}
}
```
以上是使用MVC模式进行后端管理员RSA加密登录的具体代码实现,仅供参考。
阅读全文