使用Spring mvc跟踪WebApp的缺陷

时间:2014-09-16 05:40:47

标签: java spring spring-mvc defects

这是我的spring mvc web app的登录界面。我需要捕获每个可能的错误条件并存储到database.like错误检测系统。

例如,如果用户单击登录按钮而没有填写表单错误条件,则提供的任何内容都不应保存在错误表中。

如果没有提供密码,则应保存未提供的密码。

enter image description here

@RequestMapping(value = { "/do-login" }, method = RequestMethod.POST)
    public String login(@ModelAttribute(value = "user") User user) {
        process(user);
        return "success";
    }

    public void process(User user) {

        List<User> users = userService.findAll();
        for (User u : users) {
            if (!u.getUserName().equals(user.getUserName())) {
                IssueDetail detail = new IssueDetail();
                detail.setSummary("user name");
                detail.setDescription("wrong user name entered");
                issueDetailService.save(detail);
            } else if (!u.getPassword().equals(user.getPassword())) {
                IssueDetail detail = new IssueDetail();
                detail.setSummary("password");
                detail.setDescription("wrong password entered");
                issueDetailService.save(detail);
            } else if (!u.getUserType().equalsIgnoreCase(user.getUserType())) {
                IssueDetail detail = new IssueDetail();
                detail.setSummary("user type");
                detail.setDescription("wrong user type entered");
                issueDetailService.save(detail);
            }
        }

    }

我尝试了上面的代码.... 当上述循环运行时,如果用户没有输入任何字段,则循环在数据库中保存任何if语句3次。

有没有其他方法可以做到。

测试案例

Username  password   type
-------------------------
  N           N        N      =NULL
  Y           N        N      = WRONG PASS & TYPE
  Y           Y        N      = WRONG TYPE.
  N           Y        Y      = WRONG USER.
  N           N        Y      = WRONG PASSWORD & TYPE.

1 个答案:

答案 0 :(得分:1)

尝试以下代码行,在内部处理方法中代替for循环。

                            List<User> users = userService.findAll();
            boolean isUserNameExist = false;
            boolean isPasswordExist = false;
            boolean isUserTypeExist = false;
            for (User u : users) {
                if (u.getUserName().equals(user.getUserName())) {
                    isUserNameExist = true;
                    break;
                } 
            }

            for (User u : users) {
               if (u.getPassword().equals(user.getPassword())) {
                    isPasswordExist =true;
                    break;
               } 
            }
            for (User u : users) {
                if (u.getUserType().equalsIgnoreCase(user.getUserType())) {
                    isUserTypeExist = true;
                    break;
                }
            }
            IssueDetail detail = new IssueDetail();
            if(!isUserNameExist){
                detail.setSummary("user name");
                detail.setDescription("wrong user name entered");
            }
            if(!isPasswordExist){
                detail.setSummary(detail.getSummary()+" password");
                detail.setDescription(detail.getDescription()+" wrong password entered");
            }
            if(!isUserTypeExist){
                detail.setSummary(detail.getSummary() +" user type");
                detail.setDescription(detail.getDescription() +" wrong user type entered");
            }
            issueDetailService.save(detail);