1

假设我们有这个用于登录的代码,如果凭据用于管理页面,则 RequestMapping 用于管理员,如果用于用户凭据,则用户重定向到用户面板。现在,两者的主页都反对我在下面的代码中定义的相同网址,例如:

http://localhost:8080/project/{username}/main

我的问题是:

在Controller类中完成登录检查后,当它们具有相同的RequestMapping“main”时,我们如何将这两个方法分开?

@RequestMapping(value = "/login")
public String welcome(@RequestParam("j_username") String username, @RequestParam("j_password") String password, HttpSession session, Model model) throws RemoteException, NotBoundException {

    int checkAccount = uiClient.checkAdminAccount(username, password);
    if (checkAccount == 1) {
        session.setAttribute("username", username);
        return "redirect:/" + username + "/main";
    } else if (checkAccount == 0) {
        checkAccount = uiClient.checkAccount(username, password);
        if (checkAccount == 1) {
            session.setAttribute("username", username);
            return "redirect:/" + username + "/main";
        } else if (checkAccount == 0) {
            return "login";
        }
    } else {
        return "databaseError";
    }
    return "login";
}

@RequestMapping(value = "/{username}/main")
public String indexPage(@PathVariable("username") String username) {
    return "/user/userPanel";
}

@RequestMapping(value = "{username}/main")
public String adminIndexPage(@PathVariable("username") String username){
    return "/admin/adminPanel";
}

我的意思是,有什么方法可以像特殊标签或我们可以为每个映射放置并在登录过程完成后将它们分开,以便管理员重定向到 adminPanel 并且用户也重定向到 userPanel 但两者都具有相同的 url:

http://localhost:8080/project/{username}/main

???

4

1 回答 1

1

这种方式怎么样:

@RequestMapping(value = "/login")
public String welcome(@RequestParam("j_username") String username, @RequestParam("j_password") String password, HttpSession session, Model model) throws RemoteException, NotBoundException {

    int checkAccount = uiClient.checkAdminAccount(username, password);
    if (checkAccount == 1) {
        session.setAttribute("username", username);
        session.setAttribute("userrole", "Admin");
        return "redirect:/" + username + "/main";
    } else if (checkAccount == 0) {
        checkAccount = uiClient.checkAccount(username, password);
        if (checkAccount == 1) {
            session.setAttribute("username", username);
            session.setAttribute("userrole", "Admin");
            return "redirect:/" + username + "/main";
        } else if (checkAccount == 0) {
            return "login";
        }
    } else {
        return "databaseError";
    }
    return "login";
}

@RequestMapping(value = "/{username}/main")
public String indexPage(@PathVariable("username") String username) {
    if ("Admin".equals(session.getAttribute("userrole"))) {
        return "/admin/adminPanel";
    } else {
        return "/user/userPanel";
    }
}

为简单起见,这里我没有使用 Spring Security 来检查用户角色。

于 2013-01-21T06:49:53.037 回答