Spring安全性当您应该忽略OPTIONS请求时忽略所有请求

时间:2016-11-01 18:50:00

标签: spring spring-security

在GET / POST请求之前,客户端发出OPTIONS请求,因此我忽略了这些调用。但是当我进行此配置时,另一个请求(GET / POST)也被忽略(但不应忽略)。

当我添加这一行时:

.antMatchers(HttpMethod.OPTIONS);

忽略所有请求,但不应忽略GET / POST。

以下是配置方法:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
        .antMatchers(HttpMethod.POST, "/login")
        .antMatchers(HttpMethod.OPTIONS); 
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
    .authorizeRequests()
        .anyRequest().authenticated()
        .antMatchers(HttpMethod.OPTIONS).permitAll()
        .antMatchers(HttpMethod.GET, "/login/authenticate").authenticated()

        .antMatchers(HttpMethod.GET, "/credenciadas**").hasRole(PermissaoEnum.CONSULTAR_CREDENCIADA.getNomeInterno())
        .antMatchers(HttpMethod.POST, "/credenciadas/validar").hasRole(PermissaoEnum.CONSULTAR_CREDENCIADA.getNomeInterno())
        .antMatchers(HttpMethod.POST, "/credenciadas").hasRole(PermissaoEnum.INCLUIR_CREDENCIADA.getNomeInterno())
        .antMatchers(HttpMethod.POST, "/credenciadas/alterar").hasRole(PermissaoEnum.ALTERAR_CREDENCIADA.getNomeInterno())
        .antMatchers(HttpMethod.DELETE, "/credenciadas/").hasRole(PermissaoEnum.EXCLUIR_CREDENCIADA.getNomeInterno())

    .and()
    .addFilterBefore(authenticationByTokenFilter(), UsernamePasswordAuthenticationFilter.class)
    .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint())
    .and()
    .csrf().disable();
}

1 个答案:

答案 0 :(得分:1)

您可以验证是否将角色名称的前缀字符串设置为:“ROLE_”?角色名称可能是错误的。