【问题标题】:Spring MVC binding to same @RequestMappingSpring MVC 绑定到相同的@RequestMapping
【发布时间】:2013-01-21 06:17:34
【问题描述】:

假设我们有这个用于登录的代码,如果凭据用于管理页面,则 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

???

【问题讨论】:

    标签: spring jakarta-ee spring-mvc


    【解决方案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 来检查用户角色。

    【讨论】:

    • thanx AnthonY,解决了这个问题,你能告诉我你如何在这里使用 Spring Security
    • 好吧,要使用 Spring Security,您还必须使用它来验证用户。这里有一些工作。如果你想这样做,你可以使用 Spring Roo 创建一个骨架项目并弄清楚如何将它应用到你的应用程序中。
    猜你喜欢
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    相关资源
    最近更新 更多