我怎么能忽略CSRF安全的特定URL,如“/workflow/**"。除了这个URL,我需要授权和CSRF安全的所有URL的和方法。
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private RESTAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
private RESTAuthenticationFailureHandler authenticationFailureHandler;
@Autowired
private RESTAuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private PranaUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().requireCsrfProtectionMatcher(new AllExceptUrlStartedWith("/workflow"))
.and().authorizeRequests()
.antMatchers("/rest/**", "/tasklist").authenticated()
.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/index.html")
.and().exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
.and().formLogin().successHandler(authenticationSuccessHandler)
.and().formLogin().failureHandler(authenticationFailureHandler)
.and().csrf().csrfTokenRepository(csrfTokenRepository()).and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
}
private static class AllExceptUrlStartedWith implements RequestMatcher {
private static final String[] ALLOWED_METHODS =
new String[] {"GET"};
private final String[] allowedUrls;
public AllExceptUrlStartedWith(String... allowedUrls) {
this.allowedUrls = allowedUrls;
}
@Override
public boolean matches(HttpServletRequest request) {
String method = request.getMethod();
for(String allowedMethod : ALLOWED_METHODS) {
if (allowedMethod.equals(method)) {
return false;
}
}
String uri = request.getRequestURI();
for (String allowedUrl : allowedUrls) {
if (uri.startsWith(allowedUrl)) {
return false;
}
}
return true;
}
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/styles/**").antMatchers("/scripts/**");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
}
字符串
我怎么能忽略CSRF安全的特定URL,如“/workflow/**"。除了这个URL,我需要授权和CSRF安全的所有URL的和方法。
4条答案
按热度按时间3yhwsihp1#
在我的项目中,我使用了以下代码:
字符串
在本例中,我禁用了
/console
的CSRF保护。更新:自Spring Security 4.0以来,您可以将其简化为一行:
型
qyswt5oh2#
在这个线程中回答的唯一目的是解释和使用
antPathMatcher
,它的优点可以用来保护许多使用ant matchers的url。从Doc
.csrf().requireCsrfProtectionMatcher(RequestMatcher requireCsrfProtectionMatcher)
指定RequestMatcher用于确定何时应用CSRF。默认值是忽略GET、HEAD、TRACE、OPTIONS并处理所有其他请求。
注意默认情况下
GET
,HEAD
,TRACE
,OPTIONS
请求被忽略。如果你想覆盖这个默认配置requireCsrfProtectionMatcher(implementation_of_RequestMatcher)
。在RequestMatcher的实现中,定义所有需要保护的URL。您完成了
假设您希望确保URL的
/api/**
用于CSRF保护。字符串
逻辑解释
假设URL:
http://localhost:8080/csrf/api/test1
String uri = urlPathHelper.getPathWithinApplication(request);
uri =>
/api/test1
;antPathMatcher.match("/api/**", "/api/test1")
=>true
byqmnocz3#
回答我自己的问题.感谢@Slava
字符串
5sxhfpxr4#
在Spring Security 6中,可以使用
CsrfConfigurer#ignoringRequestMatchers
。举例来说:
字符串