【问题标题】:Basic Authentication using RestTemplate in Spring MVC 3.1 status code 301在 Spring MVC 3.1 状态码 301 中使用 RestTemplate 进行基本身份验证
【发布时间】:2012-10-10 18:30:09
【问题描述】:

我正在使用 Spring MVC 3.1 开发示例应用程序。在这个应用程序中,我使用的是 REST API,而要使用 REST API,我使用的是 Rest Template。

我的第一个页面是简单的登录表单,用户在其中输入用户名和密码。在控制器中,我获取这些用户名和密码,对它们进行编码并发送获取基本身份验证请求。

如果状态码为 200,则用户已通过身份验证,否则不是。

但我总是收到状态码 301。

我的控制器代码如下:

@RequestMapping(value = "/loginForm", method = RequestMethod.POST)
    public String login(@ModelAttribute("loginForm") LoginForm loginForm,
            BindingResult result, ModelMap model) {

        String userName = loginForm.getUserName();
        String password = loginForm.getPassword();

        // Basic Authentication

        String token = userName + ":" +password;
        BASE64Encoder enc = new sun.misc.BASE64Encoder();
        String encodedAuthorization = enc.encode(token.getBytes());
        String authHeader = "Basic " + encodedAuthorization;

        System.out.println("authHeader :::::: " + authHeader);

        // Prepare acceptable media type
        List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
        acceptableMediaTypes.add(MediaType.APPLICATION_XML);

        HttpHeaders headers = new HttpHeaders();

        headers.setAccept(acceptableMediaTypes);
        headers.setContentType(MediaType.APPLICATION_XML);
        headers.set("Authorization", authHeader);

        HttpEntity<String> entity = new HttpEntity<String>(headers);

        ResponseEntity<String> responseEntity = restTemplate.exchange(uri,
                HttpMethod.GET, entity, String.class);

        HttpHeaders header = responseEntity.getHeaders();
        HttpStatus code = responseEntity.getStatusCode();

        System.out.println("Header :::::: " + header);
        System.out.println("Code :::::: " + code);

        // Validation on fields
        validator.validate(loginForm, result);
        if (result.hasErrors()) {
            return "login";
        }



        // Checkign status is 200 or not

        if ((Integer.parseInt(code.toString())!= 200)) {
            model.addAttribute("formError", "true");
            return "login";
        }
        model.addAttribute(loginForm);
        return "redirect:dateEntry";

    }    

我哪里错了?

【问题讨论】:

  • 301 是一个重定向。你是说 401 吗?
  • 当我在登录表单中输入任何用户名和密码并使用它们进行处理时,我会在控制台上得到这些输出: authHeader :::::: Basic YWFhYWE6YWFhYQ== Header :::::: {Server =[nginx/0.7.67], Content-Type=[text/html], Content-Length=[185], Location=[assembla.com/], Vary=[Accept-Encoding], Date=[Sat, 2012 年 10 月 20 日08:38:14 GMT], Connection=[keep-alive]} Code :::::: 301 所以状态码是 301。这是什么意思,header 也不像其他请求一样 Content-Type=[text/ html] 而我将其设置为 applicaiton/xml

标签: rest spring-mvc basic-authentication restful-authentication resttemplate


【解决方案1】:

301 表示永久重定向。您可能正在请求一个过时的 URL。 Location 响应标头告诉您新 URL 是什么。见http://en.wikipedia.org/wiki/HTTP_301

【讨论】:

  • 是的,贾莎。我得到了它。感谢您的回复:) :)
猜你喜欢
  • 2013-01-01
  • 2012-02-21
  • 2012-10-05
  • 2016-08-24
  • 2014-03-22
  • 2014-03-26
  • 2017-12-22
  • 1970-01-01
  • 2018-01-22
相关资源
最近更新 更多