ParamterNullCheck
package com.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class ParamterNullCheck {
public static ParamterNullCheck instance = new ParamterNullCheck();
/**
* 判断传来的数据是否为空
*
* @param object
* key
* @param paramters
* 定义字符串key
* @return
*/
public JSONObject checkNull(Object object, String[] paramters) {
JSONObject param = null;
JSONObject result = new JSONObject();
if (!(object instanceof JSONObject)) {
param = (JSONObject) JSON.toJSON(object);
} else {
param = (JSONObject) object;
}
if (paramters != null) {
for (String key : paramters) {
Object value = param.get(key);
if (value == null || "".equals(value)) {
result.put("msg", Constants.SYS_PARAMTER_MISSING);
return result;
} else if (value instanceof JSONArray) {
if (((JSONArray) value).size() == 0) {
result.put("msg", Constants.SYS_PARAMTER_MISSING);
return result;
}
}
}
}
return null;
}
public static ParamterNullCheck getInstance() {
return instance;
}
public static void setInstance(ParamterNullCheck instance) {
ParamterNullCheck.instance = instance;
}
}
Controller
package com.ty.controller;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
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 com.alibaba.fastjson.JSONObject;
import com.ty.model.AdminTbl;
import com.ty.model.MenuTbl;
import com.ty.service.LoginService;
import com.utils.Constants;
import com.utils.ParamterNullCheck;
@Controller
@RequestMapping(value = "/account")
public class LoginController {
/**
* 调测日志记录器。
*/
private static final Logger DEBUGGER = Logger.getLogger(LoginController.class);
@Autowired
private LoginService loginService;
/**
* 登录
*
* @param request
* @param response
* @param model
* @param admintabl
* @return
*/
@RequestMapping(value = "/account_login", method = RequestMethod.POST)
@ResponseBody
public String account_login(HttpServletRequest request, HttpServletResponse response, Model model,
AdminTbl admintabl) {
DEBUGGER.info("登录操作前台传来的数据admintabl:" + admintabl);
JSONObject result = new JSONObject();
String[] args = { "job_number", "password_hash" };
JSONObject nullcheck = ParamterNullCheck.getInstance().checkNull(admintabl, args);
if (nullcheck == null) {
/**
* 使用Shiro编写认证操作
*/
// 1.获取Subject
Subject subject = SecurityUtils.getSubject();
String password = admintabl.getPassword_hash().toUpperCase();
System.out.println(password);
// 2.封装用户数据
UsernamePasswordToken token = new UsernamePasswordToken(admintabl.getJob_number(), password);
// System.out.println("token==" + token);
AdminTbl adt = loginService.get_account(admintabl);
if (adt == null) {
// 账号不存在
result.put("msg", Constants.NO_ACCOUNT);
} else {
if (adt.getStatus() == 1) {
try {
subject.login(token);
AdminTbl atb = loginService.get_account(admintabl);
result.put("msg", Constants.SUCCESS);
result.put("data", atb);
} catch (UnknownAccountException e) {
result.put("msg", Constants.NO_ACCOUNT);
} catch (IncorrectCredentialsException e) {
result.put("msg", Constants.PASSWORD_ERROR);
}
} else {
// 该账号已被禁用
result.put("msg", Constants.FORBIDDEN);
}
}
} else {
result = nullcheck;
}
DEBUGGER.info("登录返回数据:" + result.toJSONString());
return result.toJSONString();
}
}