use of com.hfut.exception.CustomException in project Workload by amoxu.
the class UserController method password.
@RequestMapping(value = "/user/password", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8", method = { RequestMethod.POST })
@ResponseBody
public // 修改密码
String password(@RequestParam(name = "name") String name, @RequestParam(name = "oldPsw") String old, @RequestParam(name = "newPsw") String newpsw) throws Exception {
Subject currentUser = SecurityUtils.getSubject();
old = ToolKit.psw2pwd(old);
newpsw = ToolKit.psw2pwd(newpsw);
AjaxResult result = new AjaxResult();
String username = currentUser.getPrincipal().toString();
User user = userService.findByName(username);
if (null == user || !username.equals(name)) {
result.failed();
result.setMsg("修改用户名与当前用户名不匹配。");
return JSON.toJSONString(result);
} else if (!user.getPassword().equals(old)) {
result.failed();
result.setMsg("旧密码不正确。");
return JSON.toJSONString(result);
}
try {
user.setPassword(newpsw);
System.out.println(user);
userService.alterPassword(user);
result.ok();
result.setMsg("修改成功,重新登录。");
return JSON.toJSONString(result);
} catch (Exception e) {
throw new CustomException("请检查数据是否正确");
}
}
use of com.hfut.exception.CustomException in project Workload by amoxu.
the class loginRealm method doGetAuthorizationInfo.
/**
* 获取身份信息,我们可以在这个方法中,从数据库获取该用户的权限和角色信息
* 当调用权限验证时,就会调用此方法
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String teacherName = (String) getAvailablePrincipal(principalCollection);
com.hfut.entity.UserRole role = null;
try {
User userlogin = userService.findByName(teacherName);
// 获取角色对象
role = userRoleService.findByid(userlogin.getLevel());
} catch (Exception e) {
try {
throw new CustomException("用户不存在或密码错误");
} catch (CustomException e1) {
e1.printStackTrace();
}
}
// 通过用户名从数据库获取权限/角色信息
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Set<String> r = new HashSet<String>();
if (role != null) {
r.add(role.getName());
info.setRoles(r);
}
return info;
}
use of com.hfut.exception.CustomException in project Workload by amoxu.
the class UserController method update.
@RequestMapping(value = "/zone/update", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8", method = { RequestMethod.POST })
@ResponseBody
public // 重置密码
String update(HttpServletRequest request) throws Exception {
Subject currentUser = SecurityUtils.getSubject();
String name = request.getParameter("name");
String mail = request.getParameter("mail");
String question = request.getParameter("question");
String answer = request.getParameter("answer");
AjaxResult result = new AjaxResult();
String username = currentUser.getPrincipal().toString();
User user = userService.findByName(username);
if (null == user) {
result.failed();
result.setMsg("请重新登录后重试。");
return JSON.toJSONString(result);
}
user.setQuestion(question);
user.setAnswer(answer);
user.setMail(mail);
user.setUser(name);
try {
userService.updateUser(user);
currentUser.logout();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUser(), user.getPassword());
Subject subject = SecurityUtils.getSubject();
// 如果获取不到用户名就是登录失败,但登录失败的话,会直接抛出异常
subject.login(token);
result.ok();
result.setMsg("修改成功。");
return JSON.toJSONString(result);
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("请重新登录后重试。");
}
}
use of com.hfut.exception.CustomException in project Workload by amoxu.
the class UserController method reset.
@RequestMapping(value = "/auth/anser", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8", method = { RequestMethod.POST })
@ResponseBody
public // 重置密码
String reset(HttpServletRequest request) throws Exception {
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
String username = request.getParameter("user");
String captcha = request.getParameter("captcha");
String md5 = request.getParameter("md5");
String question = request.getParameter("question");
String answer = request.getParameter("answer");
String password = request.getParameter("password");
if (session.getAttribute("rand") == null || !session.getAttribute("rand").toString().equalsIgnoreCase(captcha)) {
return "{\"status\":1,\"msg\":\"请重新输入验证码!\"}";
}
boolean ss = !session.getAttribute("md5").toString().equals(md5);
System.out.println(session.getAttribute("md5").toString());
System.out.println(request.getParameter("md5"));
if (session.getAttribute("MD5") != null && ss) {
return "{\"status\":1,\"msg\":\"请刷新重试!\"}";
}
String psw = Encryp.strDec(password, "amoxu", "amoxu", "amoxu");
System.out.println(psw);
// 加密MD5 32
psw = Encryp.encryptionStr(psw + "amoxu", Encryp.MD5);
AjaxResult result = new AjaxResult();
try {
User user = userService.findByName(username);
if (user.getAnswer() != null && user.getAnswer().equals(answer) && user.getQuestion().equals(question)) {
user.setPassword(psw);
userService.updateUser(user);
currentUser.logout();
System.out.println(psw);
result.ok();
result.setMsg("修改成功,重新登录。");
return JSON.toJSONString(result);
} else {
result.failed();
result.setMsg("答案错误。");
return JSON.toJSONString(result);
}
} catch (Exception e) {
throw new CustomException("请检查数据是否正确");
}
}
use of com.hfut.exception.CustomException in project Workload by amoxu.
the class UserServiceImpl method register.
@Override
public boolean register(User user) throws Exception {
System.out.println("user register:" + user.getUser() + " " + user.getPassword() + " " + user.getMail());
if (StringUtils.isEmpty(user.getUser()) || StringUtils.isEmpty(user.getPassword())) {
throw new CustomException("用户或密码不能为空!");
} else if (StringUtils.isEmpty(user.getMail())) {
throw new CustomException("邮箱不能为空!");
} else if (user.getPassword().length() < 6) {
throw new CustomException("密码过短!");
}
UserExample userExample = new UserExample();
UserExample.Criteria criteria = userExample.createCriteria();
criteria.andUserEqualTo(user.getUser());
List<User> list = userMapper.selectByExample(userExample);
if (list.size() != 0) {
throw new CustomException("账号已存在!");
}
userExample = new UserExample();
criteria = userExample.createCriteria();
criteria.andMailEqualTo(user.getMail());
list = userMapper.selectByExample(userExample);
if (list.size() != 0) {
throw new CustomException("邮箱已存在!");
}
int ret = userMapper.insert(user);
if (ret > 0) {
return true;
} else {
return false;
}
}
Aggregations