【问题标题】:@SessionAttribute confusing@SessionAttribute 令人困惑
【发布时间】:2022-01-21 01:07:05
【问题描述】:

我有一个具有登录功能的 Spring Controller 类。它有两种方法:getCustomer 从数据库中获取客户信息以保持会话, submitLoginForm 检查客户(或用户)是否成功登录(如果 getCustomer 返回 null 并运行到错误页面,则登录失败)。但让我感到困惑的一件事是当我输入真实的用户名和密码时

@Controller
@SessionAttributes("customer")
public class LoginController {
    
    @Autowired
    CustomerService customService;
    
    // get username and password to log in
    @ModelAttribute("customer")
    public Customer getCustomer(@RequestParam String username, @RequestParam String password) {

            Customer c = customService.getCustomer(username, password);
            return c;
    }
    
    @RequestMapping(method=RequestMethod.POST,value="/login")
    public String submitLoginForm(@SessionAttribute(required=false) Customer customer, Model model) {
        
        if(customer != null) {
            // login successfully
            return "redirect:/";
        } else {
            // login unsuccessfully
            model.addAttribute("message","wrong username or password");
            return "error";
        }
    }
}

当我使用真实的用户名和密码登录时,我得到的是错误页面而不是主页。但是当我输入主页网址时,主页会显示我已成功登录的用户名

这是我的登录表单

<form action="login" method="POST">
    <input type="text" placeholder="Name" name="username" required/>
    <input type="text" placeholder="Password" name="password" required/>
                            
    <button type="submit" class="btn btn-default">Login</button>
</form>

【问题讨论】:

  • 显示您的表格! (听起来总是customer==null(在后期映射中)?)
  • ..当页面第一次加载时,@RequestParam String username, @RequestParam String password 是什么? (没有查询/参数!?););)
  • @xerx593 我已经在我的问题上添加了我的登录表单
  • @xerx593 不,参数中的客户似乎最初具有空值(这导致错误页面和登录失败)。但是当我通过输入url跑到主页时,客户不再为空(登录突然成功)

标签: java spring-mvc


【解决方案1】:

做到这一点(未经测试):

@Controller
@SessionAttributes("customer")
public class LoginController {

    @Autowired
    CustomerService customService;

    @RequestMapping(method=RequestMethod.POST,value="/login")
    public String submitLoginForm(
      @RequestParam String username, 
      @RequestParam String password,
      Model model
    ) {
        Customer customer = customService.getCustomer(username, password);
        if(customer != null) {
            // login successfully
            model.addAtribute("customer", customer); // !!
            return "redirect:/";
        } else {
            // login unsuccessfully
            model.addAttribute("message","wrong username or password");
            return "error";
        }
    }
}
  • 不要“暴露”任何@ModelAttribute,因为我们的表单(“view”!)不需要/使用它。 (所见即所得)
  • 但是:
    • 发送@RequestParams到submitLoginForm()(根据html形式)
    • 在成功登录时添加模型/会话属性(即在表单提交时)。

在此 (model.addAttribute) 之后,我们可以从该会话中的任何视图/控制器访问 "customer" (模型/会话) 属性(由于/感谢 SessionAttributes 注释)。


为此工作:

@ModelAttribute("customer")
public Customer getCustomer(@RequestParam String username, @RequestParam String password) {
    Customer c = customService.getCustomer(username, password);
    return c;
}

我们已经必须像这样调用(GET)它:/login?username=admin&amp;password=se3cret(超过 submitLoginForm()..)。

【讨论】:

  • 所以当我调用这个控制器时,它会先运行 submitLoginForm 方法,然后运行 ​​getCustomer 对吗?我认为反之亦然
  • 抱歉,这是哪个?使您的(OP)控制器工作(它工作!):我们需要GET /login?username=admin&amp;password=se3cret(使用正确的凭据)(..之后它是无关紧要的,我们在表单中输入的内容!!!!)
  • 与我提议的(回答)控制器:我们GET /login(无查询),然后必须在表单中输入正确的凭据并点击提交...;)(假设 GET 映射很简单...返回“登录”(或者这样......如果有的话!:):)。)
猜你喜欢
  • 2022-01-17
  • 2018-02-26
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
相关资源
最近更新 更多