【问题标题】:In Spring mvc, when i am hitting the CURL then getting error in spring MVC在 Spring MVC 中,当我点击 CURL 然后在 Spring MVC 中出现错误
【发布时间】:2018-10-04 12:05:21
【问题描述】:

我有一个 curl 类型的 url,

curl -i -X GET -H Accept:application/json http://localhost:8080/onyxcxm/admin/getDTMF/2

当我点击 url 并得到响应时,

HTTP/1.1 302

X-Content-Type-Options: nosniff

X-XSS-保护:1;模式=阻止

Cache-Control: no-cache, no-store, max-age=0,

must-revalidate Pragma: no-cache

过期:0 X-Frame-选项:SAMEORIGIN

位置:http://localhost:8080/onyxcxm/login

内容长度:0

日期:格林威治标准时间 2018 年 10 月 4 日星期四 11:36:48

这里是我的控制器,

@RequestMapping(value="/getDTMF/{dtmf}",headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> getDTMF(@PathVariable("dtmf") String dtmf) {
    System.err.println("Pressed digit is "+dtmf);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8");
    return new ResponseEntity<String>(null, headers, HttpStatus.OK);
}

我的安全配置是,

protected void configure(HttpSecurity http) throws Exception {
    http
    .headers().addHeaderWriter(
            new XFrameOptionsHeaderWriter(
            XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
            .and()
    .authorizeRequests()
    .antMatchers("/admin/**").access("hasRole('ADMIN')")
    .antMatchers("/user/**").access("hasRole('USER')")
    .and()
    .formLogin()
    .loginPage("/login")
    .loginProcessingUrl("/j_spring_security_check")
    .successHandler(authenticationSuccessHandler())
    .failureUrl("/login?error")
    .usernameParameter("username")
    .passwordParameter("password")
    .and()
    .logout()
    .logoutUrl("/j_spring_security_logout")
    .logoutSuccessHandler(logoutSuccessHandler())
    .logoutSuccessUrl("/login?logout")
    .invalidateHttpSession(true)
    .and()
    .csrf().disable();
}

我不认为我为什么会收到此错误,任何人都可以解决它吗?谢谢。

【问题讨论】:

  • 因为您没有经过身份验证。您将被重定向到登录页面。
  • 那么我如何在这里验证它并感谢快速回复。
  • 启用基本身份验证并使用 curl 发送基本身份验证标头...或禁用安全性。
  • 但是亲爱的,当我已经通过 BCryptPasswordEncoder 加密密码时,如何启用基本身份验证。如果您有任何示例,可以与我分享。谢谢
  • http.httpBasic().and().&lt;what you had&gt;。类似的东西...

标签: java spring spring-mvc curl


【解决方案1】:

您遇到的行为是您自己设计的。您正在请求路径“/admin/**”下的页面,该页面需要根据您设置的安全配置进行身份验证。

这就是 Spring 正在做的事情:

  • 检查您是否拥有有效的身份验证令牌
  • 如果存在,则允许将调用传递给 @Controller 和 @RequestMapping
  • 如果不重定向到配置的登录页面(在您的情况下为“/login”)

由于您没有提供任何安全令牌(可以通过 /login 页面上的登录获得,并且应该在标头中传递)Spring 将 CURL 请求重定向到配置的登录页面。状态码 302 的意思就是,将用户重定向到指定的页面。

对于基本身份验证,请添加一个标头“Authorization: userHash”,将 userHash 替换为 : 的 base64 编码字符串。有关如何使用标头的更多信息,请参阅 wikipedia Basic Access Authentication

【讨论】:

  • 你能提供任何例子吗,我这里有 BCryptPasswordEncoder 密码。
  • 基本认证与 BCryptEncoder 无关。它是用户名和密码的 base64 编码散列组合到 Authentication 标头中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多