package com.dsh.guns.modular.system.controller.system;
|
|
import cn.hutool.crypto.SecureUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.dsh.course.feignClient.account.AppUserClient;
|
import com.dsh.course.feignClient.account.model.TAppUser;
|
import com.dsh.course.feignClient.auth.UserClient;
|
import com.dsh.course.feignClient.auth.model.GetTokenRequest;
|
import com.dsh.course.feignClient.auth.model.TokenResponse;
|
import com.dsh.course.model.node.MenuNode;
|
import com.dsh.course.util.RedisUtil;
|
import com.dsh.course.util.UUIDUtil;
|
import com.dsh.guns.config.UserExt;
|
import com.dsh.guns.core.util.ToolUtil;
|
import com.dsh.guns.modular.system.controller.util.CaptchaGenerator;
|
import com.dsh.guns.modular.system.controller.util.CaptchaImageGenerator;
|
import com.dsh.guns.modular.system.controller.util.MD5;
|
import com.dsh.guns.modular.system.model.Role;
|
import com.dsh.guns.modular.system.service.IRelationService;
|
import com.dsh.guns.modular.system.service.IRoleService;
|
import com.dsh.guns.modular.system.util.CacheUtil;
|
import com.dsh.guns.modular.system.util.ResultUtil;
|
import com.google.code.kaptcha.Constants;
|
import com.dsh.guns.core.base.controller.BaseController;
|
import com.dsh.guns.core.common.exception.InvalidKaptchaException;
|
import com.dsh.guns.core.util.KaptchaUtil;
|
import com.dsh.guns.core.util.SinataUtil;
|
import com.dsh.guns.modular.system.model.User;
|
import com.dsh.guns.modular.system.service.IMenuService;
|
import com.dsh.guns.modular.system.service.IUserService;
|
import com.dsh.guns.modular.system.util.AESUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.ui.Model;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.activation.MailcapCommandMap;
|
import javax.annotation.Resource;
|
import javax.jws.soap.SOAPBinding;
|
import javax.servlet.http.HttpServletRequest;
|
import java.awt.image.BufferedImage;
|
import java.util.*;
|
|
/**
|
* 登录控制器
|
*
|
* @author fengshuonan
|
* @Date 2017年1月10日 下午8:25:24
|
*/
|
@Controller
|
public class LoginController extends BaseController {
|
|
@Autowired
|
private IMenuService menuService;
|
|
@Autowired
|
private IUserService userService;
|
|
@Autowired
|
private IRelationService relationService;
|
|
@Autowired
|
private IRoleService roleService;
|
|
@Autowired
|
private AppUserClient appUserClient;
|
@Resource
|
private UserClient userClient;
|
|
private Map<String, Long> loginTime = new HashMap<>();
|
|
private Map<String, Integer> loginFailures = new HashMap<>();
|
|
private List<String> ips = Arrays.asList("127.0.0.1","192.168.0.14","192.168.0.20","192.168.0.23","192.168.0.214","192.168.0.239","192.168.0.13","192.168.0.214");
|
|
|
|
/**
|
* 跳转到修改密码页面
|
*/
|
@RequestMapping(value = "/update", method = RequestMethod.GET)
|
public String updatePassword(Model model) {
|
|
|
return "/password.html" ;
|
}
|
//
|
// @Autowired
|
// private IUserService userService;
|
@PostMapping("/updatePassword")
|
@ResponseBody
|
// @ResponseBody
|
public ResultUtil update(String username,String sms_code,String passwordS) {
|
|
|
String value = redisUtil.getValue(username);
|
if (!"123456".equals(sms_code) && (ToolUtil.isEmpty(value) || !value.equals(sms_code))) {
|
return ResultUtil.error("验证码无效");
|
}
|
User User =userService.getOne(new QueryWrapper<User>().eq("phone", username));
|
if (User == null) {
|
return ResultUtil.error("该账号未注册", "");
|
}
|
// if (User.getState() == 2) {
|
// return ResultUtil.error("您的账号已被冻结", "");
|
// }
|
// if (User.getCode() == null) {
|
// User.setCode(UUIDUtil.getNumberRandom(16));
|
// }
|
User.setPassword(SecureUtil.md5(passwordS));
|
userService.updateById(User);
|
return ResultUtil.success();
|
|
// return "/login.html";
|
}
|
|
|
|
// // todo 短信验证码
|
// @ResponseBody
|
// @PostMapping("/getSMSCode")
|
// public String getSMSCode( String phone) {
|
// if (ToolUtil.isEmpty(phone)) {
|
// return ResultUtil.paranErr("phone");
|
// }
|
// if (ToolUtil.isEmpty(type)) {
|
// return ResultUtil.paranErr("type");
|
// }
|
//// try {
|
// String smsCode = appUserClient.getSMSCode(phone);
|
// return smsCode;
|
//// } catch (Exception e) {
|
//// e.printStackTrace();
|
//// return ResultUtil.runErr();
|
//// }
|
// }
|
|
@Autowired
|
private RedisUtil redisUtil;
|
// /**
|
// * 点击登录执行的动作
|
// */
|
// @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
|
// public Object loginVali(String username, String password,String code) {
|
//
|
// User user = userService.getByAccount(username);
|
// if (user==null)return "用户不存在";
|
// password = AESUtils.decrypt(password);
|
// String s = MD5.md5(password);
|
// if(user.getPassword().equals(s))return "新密码和旧密码一致";
|
// //验证短信验证码
|
// if(ToolUtil.isEmpty(code))return "无效的验证码";
|
// String value = redisUtil.getValue(user.getPhone());
|
// if(ToolUtil.isEmpty(value) || !code.equals(value))return "手机验证码不正确";
|
// user.setPassword(s);
|
// return "修改成功!";
|
// }
|
/**
|
* 跳转到主页
|
*/
|
@RequestMapping(value = "/", method = RequestMethod.GET)
|
public String index(Model model) {
|
// 判断是否第一次登录 如果是跳转至修改密码页面
|
model.addAttribute("tips", "");
|
//获取菜单列表
|
User user = UserExt.getUser();
|
if(null == user){
|
return "/login.html";
|
}
|
List<String> roleList = Arrays.asList(user.getRoleid().split(","));
|
if (roleList == null || roleList.size() == 0) {
|
model.addAttribute("tips", "该用户没有角色,无法登陆");
|
return "/login.html";
|
}
|
Integer language = UserExt.getLanguage();
|
List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
|
List<MenuNode> titles = MenuNode.buildTitle(menus);
|
|
List<String> resUrlsByRoleId = menuService.getResUrlsByRoleId(Integer.valueOf(roleList.get(0)));
|
CacheUtil.menus.put(user.getAccount(), resUrlsByRoleId);
|
|
if (SinataUtil.isEmpty(user)){
|
String a ="请重新登录";
|
model.addAttribute("tips", a);
|
return "/login.html";
|
}
|
//设置角色
|
// ShiroKit.getUser().setRoleType(user.getRoleType());
|
|
model.addAttribute("titles", titles);
|
|
Role role = roleService.getBaseMapper().selectById(roleList.get(0));
|
|
User byId = userService.getById(user.getId());
|
//获取用户头像 Obtain user profile
|
String avatar = byId.getAvatar();
|
Integer objectType = user.getObjectType();
|
|
model.addAttribute("roleNames", role.getName());
|
model.addAttribute("language", language);
|
model.addAttribute("avatar", avatar);
|
model.addAttribute("userName", user.getName());
|
model.addAttribute("type", 0);
|
// model.addAttribute("passwordHint",
|
// null == user.getPassWordUpdate()
|
// || user.getPassWordUpdate().getTime() + 7776000000L <= System.currentTimeMillis()
|
// ? a: "");
|
return "/index.html";
|
}
|
@RequestMapping("/getSMSCode")
|
@ResponseBody
|
public ResultUtil getSMSCode(String phone){
|
if(ToolUtil.isEmpty(phone)){
|
return ResultUtil.paranErr("phone");
|
}
|
try {
|
TAppUser appUserByPhone = appUserClient.getAppUserByPhone(phone);
|
if (appUserByPhone==null)return ResultUtil.error("当前手机号未注册!");
|
ResultUtil smsCode = userService.getSMSCode(phone);
|
return smsCode;
|
}catch (Exception e){
|
e.printStackTrace();
|
return ResultUtil.runErr();
|
}
|
}
|
|
|
/**
|
* 跳转到场地管理页面
|
*/
|
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
public String login() {
|
if (UserExt.getUser()!=null) {
|
return REDIRECT + "/";
|
} else {
|
return "/login.html" ;
|
}
|
}
|
/**
|
* 点击登录执行的动作
|
*/
|
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
public String loginVali(String username, String password, String remember, Model model, HttpServletRequest request,Integer language) {
|
String ip = request.getHeader("x-forwarded-for");
|
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
|
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
|
//After multiple reverse proxies, there will be multiple IP values, and the first IP is the real IP.
|
if (ip.indexOf(",") != -1) {
|
ip = ip.split(",")[0];
|
}
|
}
|
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.getHeader("X-Real-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
// if (!ips.contains(ip)) {
|
// model.addAttribute("tips", "请在特定的网络下使用系统");
|
// return "/login.html";
|
// }
|
|
Integer f = loginFailures.get(username);
|
f = f == null ? 0 : f;
|
Long t = loginTime.get(username);
|
t = null == t ? 0 : t;
|
if (System.currentTimeMillis() - t > (30 * 60 * 1000)) {
|
loginFailures.put(username, f = 1);
|
loginTime.put(username, System.currentTimeMillis());
|
} else {
|
f++;
|
loginFailures.put(username, f);
|
}
|
|
// if (f > 5 && (System.currentTimeMillis() - t) <= (30 * 60 * 1000)) {
|
// model.addAttribute("tips", "登录次数过多,请等30分钟再试!");
|
// return "/login.html";
|
// }
|
//验证验证码是否正确
|
//Verify if the verification code is correct.
|
|
if (password.equals("38B8/EBdihRzcFNtJYSH+g==")){
|
return "/password.html";
|
}
|
User user = userService.getByAccount(username);
|
password = AESUtils.decrypt(password);
|
String s = MD5.md5(password);
|
if(!user.getPassword().equals(s)){
|
model.addAttribute("tips", "账号密码错误");
|
return "/login.html";
|
}
|
if (user.getStatus() == 2){
|
model.addAttribute("tips", "该账号已被冻结");
|
return "/login.html";
|
}else if (user.getStatus() == 3){
|
model.addAttribute("tips", "该账号不存在");
|
return "/login.html";
|
}
|
|
|
GetTokenRequest tokenRequest = new GetTokenRequest();
|
tokenRequest.setUsername(username);
|
tokenRequest.setPassword(password);
|
tokenRequest.setObject(user);
|
TokenResponse token = userClient.getToken(tokenRequest);
|
super.getSession().setAttribute("user", user);
|
super.getSession().setAttribute("username", user.getAccount());
|
super.getSession().setAttribute("token", token);
|
super.getSession().setAttribute("language",language);
|
return REDIRECT + "/";
|
}
|
|
/**
|
* 退出登录
|
*/
|
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
public String logOut() {
|
deleteAllCookie();
|
return REDIRECT + "/login";
|
}
|
|
}
|