【问题标题】:spring boot csrf and jadespring boot csrf和jade
【发布时间】:2015-10-03 18:20:02
【问题描述】:

我有一个带有 java 配置的 Spring Boot 应用程序。我只在 build.gradle 文件中引用 spring-boot-starter-jade4j 和 spring-boot-starter-security。我试图弄清楚为什么我不能让 csrf 令牌出现。 这是我的安全配置

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Autowired
  DataSource ds;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/", "/signup", "/js/**", "/css/**", "/terms", "/privacy", "/favicon.ico").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .logoutUrl("/logout")
            .permitAll()
            .and()
        .csrf();
  }
…
}

这是我的登录表单。源代码中显示了 CSRF 隐藏字段,但 csrf 令牌似乎没有计算并且值为空。

extends _base
block head

block body
  #bodContent.container-fluid
  .row
    .col-md-4
    .col-md-4
      br
      .panel.panel-default
        .panel-body
          h1 Please log in
          form(method="POST", action="/login")
            input(type="hidden", name='_csrf', value='#{_csrf}')
            .form-group
              label(for="email")
                | Email address
              input#username.form-control(name="username", type="email", value="")
            .form-group
              label(for="password")
                | Password
              input#password.form-control(name="password", type="password", value="")
            .checkbox
              label
                input(type="checkbox")
                | Remember me
            input.btn.btn-default(type="submit")
              | Submit
    .col-md-4

【问题讨论】:

  • 您是否尝试过input(type="hidden", name='#{_csrf.parameterName}, value='#{_csrf.token}') 中提到的official documentation
  • 所有这些:#{_csrf.parameterName} #{_csrf.token} #{_csrf.value} #{_csrf}

标签: java spring spring-mvc spring-security


【解决方案1】:

我的怀疑是 Spring Boot 没有将 CSRF 令牌公开为模型属性,这是可以预料的,因为令牌通常作为请求属性(而不是模型属性)公开。在典型的 Spring MVC 应用程序中,可以通过在ViewResolver 上将exposeRequestAttributes 设置为true,将请求属性公开为模型属性。但是,我不确定如何使用 Jade4j 视图解析器完成此操作。

但是有一种解决方法,即通过应用程序配置手动公开令牌。

@Configuration
@EnableAutoConfiguration
public class ApplicationConfig extends WebMvcConfigurerAdapter {
  public static void main(String... args) {
    SpringApplication.run(ApplicationConfig.class, args);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(csrfTokenAddingInterceptor());
  }

  @Bean
  public HandlerInterceptor csrfTokenAddingInterceptor() {
    return new HandlerInterceptorAdapter() {
      @Override
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView view) {
        CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName())
        if (token != null) {
          view.addObject(token.getParameterName(), token)
        }
      }
    }
  }
}

现在您的代码 #{_csrf.parameterName}#{_csrf.token} 可以使用了。

注意:特别感谢this answer

【讨论】:

  • 谢谢。对语法进行了一些更改,并且做到了。作为一种变通方法,我在控制器中手动将令牌添加到我的视图中,但这更好,看起来更合适。
  • JadeViewResolver 确实有一个名为 setExposeRequestAttributes 的方法,但将其设置为 true 并没有帮助。
  • exposeRequestAttributes 设置为true 可解决此问题并在模型中公开_csrf 对象。
猜你喜欢
  • 2017-04-17
  • 2015-05-22
  • 2016-06-19
  • 1970-01-01
  • 2016-07-21
  • 2019-07-31
  • 1970-01-01
  • 2019-12-30
  • 2019-05-18
相关资源
最近更新 更多