360 lines
11 KiB
Plaintext
360 lines
11 KiB
Plaintext
|
|
package com.bonus.sys.controller;
|
||
|
|
|
||
|
|
import com.bonus.core.DateTimeHelper;
|
||
|
|
import com.bonus.seat.beans.SeatBean;
|
||
|
|
import com.bonus.seat.service.SeatService;
|
||
|
|
import com.bonus.sys.*;
|
||
|
|
import com.bonus.sys.beans.UserBean;
|
||
|
|
import org.apache.commons.lang3.StringUtils;
|
||
|
|
import org.apache.shiro.SecurityUtils;
|
||
|
|
import org.apache.shiro.authc.*;
|
||
|
|
import org.apache.shiro.subject.Subject;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.stereotype.Controller;
|
||
|
|
import org.springframework.ui.Model;
|
||
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||
|
|
import org.springframework.web.servlet.ModelAndView;
|
||
|
|
|
||
|
|
import javax.servlet.http.HttpServletRequest;
|
||
|
|
import javax.servlet.http.HttpSession;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@Controller
|
||
|
|
public class LoginController extends BaseController<Object> {
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
SeatService seatService;
|
||
|
|
/**
|
||
|
|
* 访问登录页
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "/unauthorized")
|
||
|
|
public String unauthorized() {
|
||
|
|
return "unauthorized";
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* 进入后台
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "/proctor")
|
||
|
|
public String proctor() {
|
||
|
|
return "sys/proctor";
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 进入后台
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "/intercontroller")
|
||
|
|
public String intercontroller() {
|
||
|
|
return "sys/mainindex";
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@RequestMapping(value = "/gardeInfo")
|
||
|
|
public String gardeInfo(Model model) {
|
||
|
|
UserBean userInfo = UserShiroHelper.getCurrentUser();
|
||
|
|
model.addAttribute("userInfo", userInfo);
|
||
|
|
return "sys/center";
|
||
|
|
}
|
||
|
|
|
||
|
|
@RequestMapping(value = "/gardeInfoShort")
|
||
|
|
public String gardeInfoShort(Model model) { //考试结束后临时进入个人中心界面
|
||
|
|
UserBean userInfo = UserShiroHelper.getCurrentUser();
|
||
|
|
model.addAttribute("userInfo", userInfo);
|
||
|
|
String type = "endOfTest";
|
||
|
|
model.addAttribute("type", type);
|
||
|
|
return "sys/center";
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 访问登录页
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "/loginIndex")
|
||
|
|
public ModelAndView toLogin() {
|
||
|
|
ModelAndView mv = new ModelAndView();
|
||
|
|
mv.setViewName("login/login");
|
||
|
|
return mv;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 请求登录,验证用户
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "/system_login", produces = "application/json;charset=UTF-8")
|
||
|
|
@ResponseBody
|
||
|
|
public Map<String, Object> login(HttpServletRequest request,HttpSession session) {
|
||
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
||
|
|
try {
|
||
|
|
//tologout(request, session);
|
||
|
|
//Enumeration<String> s = session.getAttributeNames();
|
||
|
|
//System.out.println("session:"+session);
|
||
|
|
|
||
|
|
PageData pd = this.getPageData();
|
||
|
|
String errInfo = "";
|
||
|
|
String KEYDATA[] = pd.getString("KEYDATA").split(",jy,");
|
||
|
|
if (null != KEYDATA && KEYDATA.length == 3) {
|
||
|
|
// shiro管理的session
|
||
|
|
Subject currentUser = SecurityUtils.getSubject();
|
||
|
|
currentUser.logout();
|
||
|
|
session=request.getSession();
|
||
|
|
String username = KEYDATA[0];
|
||
|
|
String password = KEYDATA[1];
|
||
|
|
KEYDATA[0] = "";
|
||
|
|
KEYDATA[2] = "";
|
||
|
|
KEYDATA[1] = "";
|
||
|
|
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
|
||
|
|
errInfo = "nullup"; // 缺少用户名或密码
|
||
|
|
} else {
|
||
|
|
if (StringUtils.isNotEmpty(password)
|
||
|
|
/* && sessionCode.equalsIgnoreCase(code) */) {
|
||
|
|
// shiro加入身份验证
|
||
|
|
|
||
|
|
UsernamePasswordToken token = new UsernamePasswordToken(username, "123456");
|
||
|
|
//PhoneValidCodeToken token = new PhoneValidCodeToken(username, password.toUpperCase());
|
||
|
|
token.setRememberMe(true);
|
||
|
|
try {
|
||
|
|
if (!currentUser.isAuthenticated()) {
|
||
|
|
String remortIP = IPHelper.getRemortIP(request);
|
||
|
|
SeatBean bean=new SeatBean();
|
||
|
|
bean.setSeatId(remortIP);//ip地址保存
|
||
|
|
bean.setName(username);
|
||
|
|
String currentDateTime = DateTimeHelper.currentDateTime();
|
||
|
|
bean.setCreateTime(currentDateTime);
|
||
|
|
session.setAttribute("currentDateTime", currentDateTime);
|
||
|
|
//判断登录是否受限制
|
||
|
|
if(username.equals("szedu")) {
|
||
|
|
int n=seatService.findCount();
|
||
|
|
if(n<11) {
|
||
|
|
//当前登录的人员保存数据库
|
||
|
|
bean.setState("1");
|
||
|
|
seatService.insertlogo(bean);//日志的插入
|
||
|
|
currentUser.login(token);
|
||
|
|
}else {
|
||
|
|
errInfo = "szerror";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
bean.setState("0");
|
||
|
|
seatService.insertlogo(bean);//日志的插入
|
||
|
|
currentUser.login(token);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 记录登录日志
|
||
|
|
// TODO
|
||
|
|
} catch (UnknownAccountException uae) {
|
||
|
|
errInfo = "usererror";// 用户名或密码有误
|
||
|
|
} catch (IncorrectCredentialsException ice) {
|
||
|
|
errInfo = "usererror"; // 密码错误
|
||
|
|
} catch (LockedAccountException lae) {
|
||
|
|
errInfo = "inactive";// 未激活
|
||
|
|
} catch (ExcessiveAttemptsException eae) {
|
||
|
|
errInfo = "attemptserror";// 错误次数过多
|
||
|
|
} catch (Exception e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
} /*
|
||
|
|
* catch (AuthenticationException ae) { errInfo = "codeerror";// 验证未通过 }
|
||
|
|
*/
|
||
|
|
// 验证是否登录成功
|
||
|
|
if (!currentUser.isAuthenticated()) {
|
||
|
|
token.clear();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
errInfo = "codeerror"; // 验证码输入有误
|
||
|
|
}
|
||
|
|
if (StringUtils.isEmpty(errInfo)) {
|
||
|
|
session.setAttribute("getusername", username);
|
||
|
|
UserBean user = UserShiroHelper.getRealCurrentUser();
|
||
|
|
session.setAttribute("currentUser", user);
|
||
|
|
errInfo = "success"; // 验证成功
|
||
|
|
map.put("u", user);
|
||
|
|
// session.removeAttribute(GlobalConst.SESSION_SECURITY_CODE);//
|
||
|
|
// 移除SESSION的验证
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
errInfo = "error"; // 缺少参数
|
||
|
|
}
|
||
|
|
map.put("result", errInfo);
|
||
|
|
} catch (Exception e) {
|
||
|
|
logger.error(e.toString(),e);
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
UserBean user = UserShiroHelper.getRealCurrentUser();
|
||
|
|
String user1=user+"1";
|
||
|
|
if (user1.equals("null1") || user.getId() != 1){
|
||
|
|
String ipAddress = getIpAddress();
|
||
|
|
//登陆的IP地址查询对应座位
|
||
|
|
SeatBean seat = seatService.findSeatByIp(ipAddress);
|
||
|
|
//获取此座位的应坐人员
|
||
|
|
SeatBean pSeat = seatService.findPersonBySeat(seat);
|
||
|
|
if (pSeat == null) {
|
||
|
|
//seatService.insertInfo();
|
||
|
|
}else {
|
||
|
|
pSeat.setNowIdcard(user.getLoginName());
|
||
|
|
seatService.insertNowIdcard(pSeat);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return map;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 帐号注销
|
||
|
|
*
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping("/system_logout")
|
||
|
|
public String logout(HttpServletRequest request, HttpSession session) {
|
||
|
|
String currentDateTime = session.getAttribute("currentDateTime").toString();
|
||
|
|
//通过时间判断是否注销
|
||
|
|
seatService.deletebyTime(currentDateTime);
|
||
|
|
Subject currentUser = SecurityUtils.getSubject();
|
||
|
|
currentUser.logout();
|
||
|
|
session = request.getSession(true);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_USER);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_MENULIST);
|
||
|
|
|
||
|
|
return "redirect:loginIndex.html";
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 浏览器删除
|
||
|
|
*
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping("/gugelogout")
|
||
|
|
public void gugelogout(HttpServletRequest request, HttpSession session) {
|
||
|
|
String currentDateTime = session.getAttribute("currentDateTime").toString();
|
||
|
|
//通过时间判断是否注销
|
||
|
|
seatService.deletebyTime(currentDateTime);
|
||
|
|
Subject currentUser = SecurityUtils.getSubject();
|
||
|
|
currentUser.logout();
|
||
|
|
session = request.getSession(true);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_USER);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_MENULIST);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void tologout(HttpServletRequest request, HttpSession session) {
|
||
|
|
session = request.getSession(true);
|
||
|
|
session.removeAttribute("sessionUser");
|
||
|
|
session.removeAttribute("sessionMenuList");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 帐号注销
|
||
|
|
*
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping("/portal_logout")
|
||
|
|
@ResponseBody
|
||
|
|
public AjaxRes portalLogout(HttpServletRequest request, HttpSession session) {
|
||
|
|
AjaxRes ar = getAjaxRes();
|
||
|
|
try {
|
||
|
|
Subject currentUser = SecurityUtils.getSubject();
|
||
|
|
currentUser.logout();
|
||
|
|
session = request.getSession(true);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_USER);
|
||
|
|
session.removeAttribute(GlobalConst.SESSION_MENULIST);
|
||
|
|
ar.setSucceedMsg("退出成功!");
|
||
|
|
} catch (Exception e) {
|
||
|
|
ar.setFailMsg("退出失败!");
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
|
|
return ar;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 短信验证码登录
|
||
|
|
* @param
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
@RequestMapping(value = "phoneLogin", method = RequestMethod.GET)
|
||
|
|
@ResponseBody
|
||
|
|
public AjaxRes codeLogin(HttpServletRequest request,HttpSession session) {
|
||
|
|
AjaxRes ar = getAjaxRes();
|
||
|
|
//String phonenum = request.getParameter("phonenum");
|
||
|
|
//String yzm = request.getParameter("yzm");
|
||
|
|
//String loginCode = session.getAttribute("loginCode").toString();
|
||
|
|
String loginPhone = session.getAttribute("loginPhone").toString();
|
||
|
|
Subject subject = SecurityUtils.getSubject();
|
||
|
|
/*PhoneValidCodeToken token = new PhoneValidCodeToken(loginPhone);
|
||
|
|
try {
|
||
|
|
subject.login(token);
|
||
|
|
ar.setSucceedMsg("登陆成功!");
|
||
|
|
} catch (Exception e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
ar.setFailMsg("登陆失败!");
|
||
|
|
}*/
|
||
|
|
return ar;
|
||
|
|
}
|
||
|
|
@RequestMapping(value = "/web_login", produces = "application/json;charset=UTF-8")
|
||
|
|
@ResponseBody
|
||
|
|
public Map<String, Object> weblogin() {
|
||
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
||
|
|
PageData pd = this.getPageData();
|
||
|
|
String errInfo = "";
|
||
|
|
String KEYDATA[] = pd.getString("KEYDATA").split(",jy,");
|
||
|
|
if (null != KEYDATA && KEYDATA.length == 3) {
|
||
|
|
// shiro管理的session
|
||
|
|
Subject currentUser = SecurityUtils.getSubject();
|
||
|
|
|
||
|
|
String username = KEYDATA[0];
|
||
|
|
String password = KEYDATA[1];
|
||
|
|
|
||
|
|
KEYDATA[0] = "";
|
||
|
|
KEYDATA[2] = "";
|
||
|
|
KEYDATA[1] = "";
|
||
|
|
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
|
||
|
|
errInfo = "nullup"; // 缺少用户名或密码
|
||
|
|
} else {
|
||
|
|
if (StringUtils.isNotEmpty(password)
|
||
|
|
/* && sessionCode.equalsIgnoreCase(code) */) {
|
||
|
|
// shiro加入身份验证
|
||
|
|
UsernamePasswordToken token = new UsernamePasswordToken(username, password.toUpperCase());
|
||
|
|
token.setRememberMe(true);
|
||
|
|
try {
|
||
|
|
if (!currentUser.isAuthenticated()) {
|
||
|
|
currentUser.login(token);
|
||
|
|
}
|
||
|
|
// 记录登录日志
|
||
|
|
// TODO
|
||
|
|
} catch (UnknownAccountException uae) {
|
||
|
|
errInfo = "usererror";// 用户名或密码有误
|
||
|
|
} catch (IncorrectCredentialsException ice) {
|
||
|
|
errInfo = "usererror"; // 密码错误
|
||
|
|
} catch (LockedAccountException lae) {
|
||
|
|
errInfo = "inactive";// 未激活
|
||
|
|
} catch (ExcessiveAttemptsException eae) {
|
||
|
|
errInfo = "attemptserror";// 错误次数过多
|
||
|
|
} catch (AuthenticationException ae) {
|
||
|
|
errInfo = "codeerror";// 验证未通过
|
||
|
|
}
|
||
|
|
// 验证是否登录成功
|
||
|
|
if (!currentUser.isAuthenticated()) {
|
||
|
|
token.clear();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
errInfo = "codeerror"; // 验证码输入有误
|
||
|
|
}
|
||
|
|
if (StringUtils.isEmpty(errInfo)) {
|
||
|
|
errInfo = "success"; // 验证成功
|
||
|
|
// session.removeAttribute(GlobalConst.SESSION_SECURITY_CODE);
|
||
|
|
// 移除SESSION的验证
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
errInfo = "error"; // 缺少参数
|
||
|
|
}
|
||
|
|
map.put("result", errInfo);
|
||
|
|
return map;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|