Could not verify the provided CSRF token because your session was not found in spring security

前端 未结 7 2132
甜味超标
甜味超标 2020-12-15 03:09

I am using spring security along with java config

@Override
protected void configure(HttpSecurity http) throws Exception { 
    http
    .authorizeRequests()         


        
相关标签:
7条回答
  • 2020-12-15 04:04

    Came to same error just with POST methods, was getting 403 Forbidden "Could not verify the provided CSRF token because your session was not found."

    After exploring some time found solution by adding @EnableResourceServer annotation to config.

    Config looks like that (spring-boot.version -> 1.4.1.RELEASE, spring-security.version -> 4.1.3.RELEASE, spring.version -> 4.3.4.RELEASE)

    @Configuration
    @EnableWebSecurity
    @EnableResourceServer
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class SecurityConfig extends ResourceServerConfigurerAdapter {
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(inMemoryUserDetailsManager()).passwordEncoder(passwordEncoder());
    }
    
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.httpBasic();
        http.sessionManagement().sessionCreationPolicy(STATELESS);
        http.csrf().disable();
        http.authorizeRequests().anyRequest()
                .permitAll();
    }
    
    private InMemoryUserDetailsManager inMemoryUserDetailsManager() throws IOException {
        // load custom properties
        Properties properties = new Properties();
        return new InMemoryUserDetailsManager(properties);
    }
    
    private PasswordEncoder passwordEncoder() {
        return new TextEncryptorBasedPasswordEncoder(textEncryptor());
    }
    
    private TextEncryptor textEncryptor() {
        return new OpenSslCompatibleTextEncryptor();
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题