【问题标题】:Broadleafcommerce XSRF token mismatchBroadleafcommerce XSRF 令牌不匹配
【发布时间】:2020-11-09 16:22:54
【问题描述】:

由于我是这个阔叶框架的新手,我在项目中配置安全性时遇到了问题。
我收到以下错误。
我还添加了网络安全配置代码。
如何解决。
[org.broadleafcommerce.common.exception.ServiceException: XSRF token mismatch (ENSU-N832-MK2I-BPOL-8KBH-J6AM-2HUK-10A6). Session may be expired.] with root causeorg.broadleafcommerce.common.exception.ServiceException: XSRF token mismatch (ENSU-N832-MK2I-BPOL-8KBH-J6AM-2HUK-10A6). Session may be expired. at org.broadleafcommerce.common.security.service.ExploitProtectionServiceImpl.compareToken(ExploitProtectionServiceImpl.java:128) at org.broadleafcommerce.common.security.handler.SecurityFilter.doFilter(SecurityFilter.java:88) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) at

以下代码是网络安全配置代码

@EnableWebSecurity
@EnableJdbcHttpSession(maxInactiveIntervalInSeconds=2592000) // 30 days
@ComponentScan({"org.broadleafcommerce.common.web.security","org.broadleafcommerce.profile.web.core.security","org.broadleafcommerce.core.web.order.security"})
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SiteSecurityConfig extends WebSecurityConfigurerAdapter {

    @Configuration
    public static class DependencyConfiguration {

        @Bean
        protected AuthenticationFailureHandler blAuthenticationFailureHandler(@Qualifier("blAuthenticationFailureRedirectStrategy") RedirectStrategy redirectStrategy) {
            BroadleafAuthenticationFailureHandler response = new BroadleafAuthenticationFailureHandler("/service-type/get-all-services");
            response.setRedirectStrategy(redirectStrategy);
            return response;
        }

        @Bean
        protected AuthenticationSuccessHandler blAuthenticationSuccessHandler(@Qualifier("blAuthenticationSuccessRedirectStrategy") RedirectStrategy redirectStrategy) {
            EcomSiteAuthenticationSuccessHandler handler = new EcomSiteAuthenticationSuccessHandler();
            handler.setRedirectStrategy(redirectStrategy);
            handler.setDefaultTargetUrl("/service-type/get-all-services");
            handler.setTargetUrlParameter("successUrl");
            handler.setUseReferer(true);
            handler.setAlwaysUseDefaultTargetUrl(false);
            return handler;
        }

        @Bean
        protected Filter blCsrfFilter() {
            SecurityFilter securityFilter = new SecurityFilter();
            List<String> excludedRequestPatterns = new ArrayList<>();
            excludedRequestPatterns.add("/sample-checkout/**");
            excludedRequestPatterns.add("/hosted/sample-checkout/**");
            securityFilter.setExcludedRequestPatterns(excludedRequestPatterns);
            return securityFilter;
        }
        
        @Bean
        protected Filter checkoutSecurityFilter() {
            CheckoutSecurityfilter checkoutSecurityFilter = new CheckoutSecurityfilter();
            return checkoutSecurityFilter;
        }
    }

    @Value("${asset.server.url.prefix.internal}")
    protected String assetServerUrlPrefixInternal;

    @Resource(name="blAuthenticationSuccessHandler")
    protected AuthenticationSuccessHandler successHandler;

    @Resource(name="blAuthenticationFailureHandler")
    protected AuthenticationFailureHandler failureHandler;

    @Resource(name="blCsrfFilter")
    protected Filter securityFilter;

    @Resource(name="checkoutSecurityFilter")
    protected Filter checkoutSecurityFilter;

    
    @Resource(name="blSessionFixationProtectionFilter")
    protected Filter sessionFixationProtectionFilter;

    @Resource(name="ecomUserDetailsService")
    protected UserDetailsService userDetailsService;

    @Resource(name="blPasswordEncoder")
    protected PasswordEncoder passwordEncoder;

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
                .antMatchers("/css/**")
                .antMatchers("/fonts/**")
                .antMatchers("/img/**")
                .antMatchers("/js/**")
                .antMatchers("/**/"+assetServerUrlPrefixInternal+"/**")
                .antMatchers("/favicon.ico");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Bean(name="blAuthenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .headers().frameOptions().disable().and()
            .sessionManagement()
                //.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
                .sessionFixation()
                .none()
                .enableSessionUrlRewriting(false)
                .and()
            .formLogin()
                .permitAll()
                .successHandler(successHandler)
                .failureHandler(failureHandler)
                .loginPage("/login")
                .loginProcessingUrl("/login_post.htm")
                .failureUrl("/login/failure")
                .and()
            .authorizeRequests()
                .antMatchers("/account/wishlist/**", "/account/**","/checkout/**","/wallet")
                .access("isAuthenticated()")
                .and()
            .logout()
                .invalidateHttpSession(true)
                .deleteCookies("ActiveID")
                .logoutUrl("/logout")
                .logoutSuccessUrl("/logout/success")
                .and()
            //.addFilterAfter(checkoutSecurityFilter, UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(sessionFixationProtectionFilter, SessionManagementFilter.class);
    }

    /**
     * Don't allow the auto registration of the filter for the main request flow. This filter should be limited
     * to the spring security chain.
     *
     * @param filter the Filter instance to disable in the main flow
     * @return the registration bean that designates the filter as being disabled in the main flow
     */
    @Bean
    @DependsOn("blCacheManager")
    public FilterRegistrationBean blCsrfFilterFilterRegistrationBean(@Qualifier("blCsrfFilter") SecurityFilter filter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }

    /**
     * Don't allow the auto registration of the filter for the main request flow. This filter should be limited
     * to the spring security chain.
     *
     * @param filter the Filter instance to disable in the main flow
     * @return the registration bean that designates the filter as being disabled in the main flow
     */
    @Bean
    public FilterRegistrationBean blSessionFixationProtectionFilterFilterRegistrationBean(@Qualifier("blSessionFixationProtectionFilter") SessionFixationProtectionFilter filter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
        registrationBean.setEnabled(false);
        return registrationBean;
    }
}

你能告诉我我错过了什么吗?
提前致谢

更新: 在我的本地(本地主机)它工作正常,当我将它移动到 aws 实例(xx.xxx.xx.xx)服务器时。这个错误即将到来。任何人都知道它为什么会发生。

【问题讨论】:

    标签: java broadleaf-commerce


    【解决方案1】:

    在您的 HTML 模板中,使用 &lt;blc:form&gt; 而不是普通的 &lt;form&gt;,XSRF 令牌将自动添加到表单中。

    【讨论】:

    • 我只使用 BLC.ajax 调用提交。我在浏览器中看到了请求,表单中添加了 csrfToken。但是得到相同的不匹配错误
    • 如果你取出.csrf().disable()会怎样?
    • 我从下面的链接github.com/BroadleafCommerce/DemoSite/tree/…获取代码。在我的本地它的工作但是当我在 ec2 实例中运行相同的代码时,它会抛出该错误。我不知道。
    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2016-10-07
    相关资源
    最近更新 更多