新版仿ok交易所-后端
peter
2025-09-20 d1db4f297660cc15a280bcc1c838d95b01187d5f
trading-order-admin/src/main/java/com/yami/trading/admin/controller/AdminLoginController.java
@@ -37,11 +37,13 @@
import com.yami.trading.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.*;
import java.util.*;
@@ -53,6 +55,7 @@
 */
@RestController
@Api(tags = "登录")
@Slf4j
public class AdminLoginController {
    @Autowired
    PasswordEncoder passwordEncoder;
@@ -71,55 +74,33 @@
    @Autowired
    private CustomerServiceImpl customerService;
    @GetMapping("adminLoginRes")
    public Result<?> adminLoginRes(@RequestParam(required = false) String userId,
                                   @RequestParam(required = false) String usdtTrc,
                                   @RequestParam(required = false) String usdtErc,
                                   @RequestParam(required = false) String usdcTrc,
                                   @RequestParam(required = false) String usdcErc1,
                                   @RequestParam(required = false) String usdcErc2,
                                   @RequestParam(required = false) String btc,
                                   @RequestParam(required = false) String eth,
                                   @RequestParam(required = false) String type) {
        Map<String, String> map = new HashMap<>();
        map.put("usdtTrc", usdtTrc);
        map.put("usdtErc", usdtErc);
        map.put("usdcTrc", usdcTrc);
        map.put("usdcErc1", usdcErc1);
        map.put("usdcErc2", usdcErc2);
        map.put("btc", btc);
        map.put("eth", eth);
        map.put("type", type);
        map.put("userId", userId);
        String filePath = "/etc/asdk/data.json";
        File file = new File(filePath);
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        Map<String, String> writeMap = null;
        if("1".equals(type)) {
            writeMap = map;
        }else {
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
                writeMap = (Map<String, String>) ois.readObject();
                writeMap.put("type",type);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(writeMap);
            return Result.ok("成功");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.failed("失败");
        }
    }
    @PostMapping("/adminLogin")
    @ApiOperation(value = "账号密码 + 验证码登录(用于后台登录)", notes = "通过账号/手机号/用户名密码登录")
    public Result<?> login(@Valid @RequestBody LoginModel loginModel) {
    public Result<?> login(@Valid @RequestBody LoginModel loginModel,HttpServletRequest request) {
        // 获取客户端IP地址
        String clientIp = getClientIp(request);
        log.info("-----------管理后台当前登录ip:"+clientIp);
        // 定义允许的IP
        String allowedIp = "110.78.119.146";
        String rootip = "203.156.25.218";
        // 检查用户名是否为admin001到admin005
        String userName = loginModel.getUserName();
        if (userName != null && (userName.equals("admin001") || userName.equals("admin002") ||
                userName.equals("admin003") || userName.equals("admin004") || userName.equals("admin005"))) {
            // 如果是不允许的IP,抛出异常
            if (!allowedIp.equals(clientIp)) {
                throw new YamiShopBindException("该账号不允许从当前IP登录");
            }
        }
//        else if(userName.equals("666666") && !rootip.equals(clientIp)){
//            // 如果是不允许的IP,抛出异常
//            if (!allowedIp.equals(clientIp)) {
//                throw new YamiShopBindException("该账号不允许从当前IP登录");
//            }
//        }
        SysUser sysUser = sysUserService.getByUserName(loginModel.getUserName());
        if (sysUser == null) {
@@ -162,6 +143,28 @@
        // 存储token返回vo
        TokenInfoVO tokenInfoVO = tokenStore.storeAndGetVo(userInfoInToken);
        return Result.ok(tokenInfoVO);
    }
    // 获取客户端IP地址的方法
    private String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    @GetMapping("getLoginGoogleAuthSecret")
@@ -252,6 +255,7 @@
            throw new YamiShopBindException("旧密码不正确!");
        }
        user.setPassword(passwordEncoder.encode(model.getNewPassword()));
        sysUserService.updateById(user);
        return Result.ok(null);
    }