相关疑难解决方法(0)

Spring Security 3.2针对特定URL的CSRF禁用

使用Spring security 3.2在我的Spring MVC应用程序中启用CSRF.

我的spring-security.xml

<http>
 <intercept-url pattern="/**/verify"  requires-channel="https"/>
 <intercept-url pattern="/**/login*"  requires-channel="http"/>
 ...
 ...
 <csrf />
</http>
Run Code Online (Sandbox Code Playgroud)

尝试为请求URL中包含"verify"的请求禁用CSRF.

MySecurityConfig.java

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

private CsrfMatcher csrfRequestMatcher = new CsrfMatcher();

@Override
public void configure(HttpSecurity http) throws Exception {

    http.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher);

}

class CsrfMatcher implements RequestMatcher {
    @Override
    public boolean matches(HttpServletRequest request) {

        if (request.getRequestURL().indexOf("verify") != -1)
            return false;
        else if (request.getRequestURL().indexOf("homePage") != -1)         
            return false;

        return true;
    }
}

}
Run Code Online (Sandbox Code Playgroud)

Csrf过滤器验证从"verify"提交的CSRF令牌,并且当我从http向https提交请求时,抛出无效令牌异常(403).如何在这种情况下禁用csrf令牌身份验证?

spring spring-mvc spring-security

26
推荐指数
4
解决办法
3万
查看次数

标签 统计

spring ×1

spring-mvc ×1

spring-security ×1