Search in sources :

Example 61 with AjaxResult

use of com.hfut.entity.AjaxResult in project Workload by amoxu.

the class UserController method forget.

@RequestMapping(value = "/auth/find", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8", method = { RequestMethod.POST })
@ResponseBody
public // 注册
String forget(HttpServletRequest request) throws Exception {
    // System.out.println(account.getName() + " " + account.getPsw() + " " + account.getMail());
    Subject currentUser = SecurityUtils.getSubject();
    Session session = currentUser.getSession();
    String username = request.getParameter("user");
    String captcha = request.getParameter("captcha");
    if (session.getAttribute("rand") == null || !session.getAttribute("rand").toString().equalsIgnoreCase(captcha)) {
        return "{\"status\":1,\"msg\":\"请重新输入验证码!\"}";
    }
    AjaxResult result = new AjaxResult();
    User user = userService.findByName(username);
    if (null != user) {
        result.ok();
        String md5 = Encryp.encryptionStr(new Random().nextInt(256) + new Date().getTime() + username, Encryp.MD5);
        result.setMsg(md5);
        session.setAttribute("md5", md5);
        session.setAttribute("anser", user.getAnswer());
        List list = new ArrayList();
        list.add(user.getQuestion());
        result.setData(list);
    }
    return JSON.toJSONString(result);
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) User(com.hfut.entity.User) Random(java.util.Random) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Subject(org.apache.shiro.subject.Subject) Date(java.util.Date) Session(org.apache.shiro.session.Session) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 62 with AjaxResult

use of com.hfut.entity.AjaxResult 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("请重新登录后重试。");
    }
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) User(com.hfut.entity.User) CustomException(com.hfut.exception.CustomException) Subject(org.apache.shiro.subject.Subject) CustomException(com.hfut.exception.CustomException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 63 with AjaxResult

use of com.hfut.entity.AjaxResult 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("请检查数据是否正确");
    }
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) User(com.hfut.entity.User) CustomException(com.hfut.exception.CustomException) Subject(org.apache.shiro.subject.Subject) CustomException(com.hfut.exception.CustomException) Session(org.apache.shiro.session.Session) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

AjaxResult (com.hfut.entity.AjaxResult)63 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)56 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)56 JSONObject (com.alibaba.fastjson.JSONObject)30 User (com.hfut.entity.User)6 Subject (org.apache.shiro.subject.Subject)6 CustomException (com.hfut.exception.CustomException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Expriment (com.hfut.entity.Expriment)3 LGraDesignWorkload (com.hfut.entity.LGraDesignWorkload)3 LGraPracticeWorkload (com.hfut.entity.LGraPracticeWorkload)3 LMatchWorkload (com.hfut.entity.LMatchWorkload)3 LNetWorkload (com.hfut.entity.LNetWorkload)3 LPracticeWorkload (com.hfut.entity.LPracticeWorkload)3 LProjectWorkload (com.hfut.entity.LProjectWorkload)3 LocalCourseWorkload (com.hfut.entity.LocalCourseWorkload)3 LocalDesignWorkload (com.hfut.entity.LocalDesignWorkload)3 LocalExpWorkload (com.hfut.entity.LocalExpWorkload)3 RemoteDesignWorkload (com.hfut.entity.RemoteDesignWorkload)3