package com.trafficaudit.security.controller;
|
|
import com.trafficaudit.common.Result;
|
import com.trafficaudit.security.captcha.CaptchaService;
|
import com.trafficaudit.security.utils.JwtUtils;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@RestController
|
@RequestMapping("/api/auth")
|
public class LoginController {
|
|
@Resource
|
private JwtUtils jwtUtils;
|
|
@Resource
|
private CaptchaService captchaService;
|
|
@PostMapping("/login")
|
public Result<Map<String, Object>> login(@RequestBody Map<String, Object> params) {
|
String username = (String) params.get("username");
|
String password = (String) params.get("password");
|
String captchaId = (String) params.get("captchaId");
|
List<String> clickIds = strList(params.get("captchaClickIds"));
|
|
CaptchaService.ConsumeResult cr = captchaService.consume(captchaId, clickIds);
|
if (cr == CaptchaService.ConsumeResult.NOT_FOUND) {
|
return Result.error(4002, "验证码不存在或已过期,请刷新重试");
|
}
|
if (cr == CaptchaService.ConsumeResult.NOT_VERIFIED) {
|
return Result.error(4003, "请先完成行为验证");
|
}
|
if (cr == CaptchaService.ConsumeResult.WRONG) {
|
return Result.error(4001, "行为验证未通过,请重新验证");
|
}
|
|
if ("admin".equals(username) && "admin123".equals(password)) {
|
String token = jwtUtils.generateToken(username);
|
Map<String, Object> data = new HashMap<>();
|
data.put("token", token);
|
data.put("username", username);
|
return Result.ok(data);
|
}
|
return Result.error(401, "用户名或密码错误");
|
}
|
|
@SuppressWarnings("unchecked")
|
private List<String> strList(Object o) {
|
if (o instanceof List) {
|
return (List<String>) o;
|
}
|
return Collections.emptyList();
|
}
|
}
|