| | |
| | | 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.*; |
| | |
| | | */ |
| | | @RestController |
| | | @Api(tags = "登录") |
| | | @Slf4j |
| | | public class AdminLoginController { |
| | | @Autowired |
| | | PasswordEncoder passwordEncoder; |
| | |
| | | |
| | | @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 = "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登录"); |
| | | } |
| | | } |
| | | |
| | | SysUser sysUser = sysUserService.getByUserName(loginModel.getUserName()); |
| | | if (sysUser == null) { |
| | |
| | | // 存储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") |
| | |
| | | throw new YamiShopBindException("旧密码不正确!"); |
| | | } |
| | | user.setPassword(passwordEncoder.encode(model.getNewPassword())); |
| | | sysUserService.updateById(user); |
| | | return Result.ok(null); |
| | | } |
| | | |