| | |
| | | package com.trafficaudit.security.controller; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.trafficaudit.common.Result; |
| | | import com.trafficaudit.security.captcha.CaptchaService; |
| | | import com.trafficaudit.security.utils.JwtUtils; |
| | | import com.trafficaudit.system.entity.User; |
| | | import com.trafficaudit.system.mapper.UserMapper; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | |
| | | @Resource |
| | | private CaptchaService captchaService; |
| | | |
| | | @Resource |
| | | private UserMapper userMapper; |
| | | |
| | | @Resource |
| | | private PasswordEncoder passwordEncoder; |
| | | |
| | | @PostMapping("/login") |
| | | public Result<Map<String, Object>> login(@RequestBody Map<String, Object> params) { |
| | |
| | | return Result.error(4001, "行为验证未通过,请重新验证"); |
| | | } |
| | | |
| | | if ("admin".equals(username) && "admin123".equals(password)) { |
| | | String token = jwtUtils.generateToken(username); |
| | | if (username == null || username.trim().isEmpty() || password == null || password.isEmpty()) { |
| | | return Result.error(401, "用户名或密码错误"); |
| | | } |
| | | User user = userMapper.selectOne(new LambdaQueryWrapper<User>() |
| | | .eq(User::getUsername, username.trim())); |
| | | if (user == null || !Integer.valueOf(1).equals(user.getStatus())) { |
| | | return Result.error(401, "用户名或密码错误"); |
| | | } |
| | | String encoded = user.getPassword(); |
| | | boolean ok; |
| | | try { |
| | | ok = encoded != null && passwordEncoder.matches(password, encoded); |
| | | } catch (IllegalArgumentException e) { |
| | | // 库中密码不是合法 BCrypt 时回退为明文对比(避免旧数据无法登录) |
| | | ok = encoded != null && encoded.equals(password); |
| | | } |
| | | if (!ok) { |
| | | return Result.error(401, "用户名或密码错误"); |
| | | } |
| | | String token = jwtUtils.generateToken(username.trim()); |
| | | Map<String, Object> data = new HashMap<>(); |
| | | data.put("token", token); |
| | | data.put("username", username); |
| | | data.put("username", username.trim()); |
| | | data.put("realName", user.getRealName()); |
| | | data.put("modules", user.getModules() == null ? "" : user.getModules()); |
| | | return Result.ok(data); |
| | | } |
| | | return Result.error(401, "用户名或密码错误"); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |