Search in sources :

Example 6 with CustomException

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("请检查数据是否正确");
    }
}
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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with 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;
}
Also used : User(com.hfut.entity.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) CustomException(com.hfut.exception.CustomException) CustomException(com.hfut.exception.CustomException) HashSet(java.util.HashSet)

Example 8 with CustomException

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("请重新登录后重试。");
    }
}
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 9 with 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("请检查数据是否正确");
    }
}
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)

Example 10 with 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;
    }
}
Also used : User(com.hfut.entity.User) CustomException(com.hfut.exception.CustomException) UserExample(com.hfut.entity.UserExample)

Aggregations

CustomException (com.hfut.exception.CustomException)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 User (com.hfut.entity.User)5 AjaxResult (com.hfut.entity.AjaxResult)4 Subject (org.apache.shiro.subject.Subject)4 File (java.io.File)3 IOException (java.io.IOException)3 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 UserExample (com.hfut.entity.UserExample)1 Transient (java.beans.Transient)1 BufferedInputStream (java.io.BufferedInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1