`withDefaults()` 方法对于类型 `SecurityConfiguration` 是未定义的。

huangapple go评论107阅读模式
英文:

The method withDefaults() is undefined for the type SecurityConfiguration

问题

  1. package com.example.demo;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.http.HttpMethod;
  8. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  12. import org.springframework.web.cors.CorsConfiguration;
  13. import org.springframework.web.cors.CorsConfigurationSource;
  14. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  15. @EnableWebSecurity
  16. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  17. @Autowired
  18. UserDetailsService userDetailsService;
  19. @Override
  20. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  21. auth.userDetailsService(userDetailsService);
  22. }
  23. @Override
  24. protected void configure(HttpSecurity http) throws Exception {
  25. http
  26. .cors(withDefaults()) //getting the error here
  27. .csrf().disable()
  28. .authorizeRequests()
  29. .antMatchers(HttpMethod.POST, "/login").authenticated()
  30. .antMatchers(HttpMethod.OPTIONS).permitAll()
  31. .antMatchers(HttpMethod.GET).authenticated()
  32. .anyRequest().authenticated()
  33. .and()
  34. .httpBasic();
  35. }
  36. @Bean
  37. CorsConfigurationSource corsConfigurationSource() {
  38. CorsConfiguration configuration = new CorsConfiguration();
  39. configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
  40. configuration.setAllowedHeaders(List.of("*"));
  41. configuration.setAllowedMethods(Arrays.asList("GET","POST", "OPTIONS"));
  42. configuration.setAllowCredentials(true);
  43. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  44. source.registerCorsConfiguration("/**", configuration);
  45. return source;
  46. }
  47. @Bean
  48. public PasswordEncoder getPasswordEncoder() {
  49. return NoOpPasswordEncoder.getInstance();
  50. }
  51. }
英文:

I'm building a spring application with spring security. I have added basic authentication of a username and password in the code. I want to access APIs via a reactjs web app with this basic authentication. For this, I'm trying to add the cors policy.

In the security configuration of spring security, I'm getting the error:
The method withDefaults() is undefined for the type SecurityConfiguration

I have got all the necessary dependencies installed.
What can be the possible solution?

Attaching my code below.

  1. package com.example.demo;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.http.HttpMethod;
  8. import org.springframework.security.config.Customizer;
  9. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  10. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  13. import org.springframework.security.config.annotation.web.configurers.CorsConfigurer;
  14. import org.springframework.security.core.userdetails.UserDetailsService;
  15. import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  16. import org.springframework.security.crypto.password.PasswordEncoder;
  17. import org.springframework.web.cors.CorsConfiguration;
  18. import org.springframework.web.cors.CorsConfigurationSource;
  19. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  20. @EnableWebSecurity
  21. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  22. @Autowired
  23. UserDetailsService userDetailsService;
  24. @Override
  25. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  26. auth.userDetailsService(userDetailsService);
  27. }
  28. @Override
  29. protected void configure(HttpSecurity http) throws Exception {
  30. http
  31. .cors(withDefaults()) //getting the error here
  32. .csrf().disable()
  33. .authorizeRequests()
  34. .antMatchers(HttpMethod.POST, "/login").authenticated()
  35. .antMatchers(HttpMethod.OPTIONS).permitAll()
  36. .antMatchers(HttpMethod.GET).authenticated()
  37. .anyRequest().authenticated()
  38. .and()
  39. .httpBasic();
  40. }
  41. @Bean
  42. CorsConfigurationSource corsConfigurationSource() {
  43. CorsConfiguration configuration = new CorsConfiguration();
  44. configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
  45. configuration.setAllowedHeaders(List.of("*"));
  46. configuration.setAllowedMethods(Arrays.asList("GET","POST", "OPTIONS"));
  47. configuration.setAllowCredentials(true);
  48. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  49. source.registerCorsConfiguration("/**", configuration);
  50. return source;
  51. }
  52. @Bean
  53. public PasswordEncoder getPasswordEncoder() {
  54. return NoOpPasswordEncoder.getInstance();
  55. }
  56. }

答案1

得分: 44

你的 SecurityConfigurationWebSecurityConfigurerAdapter 都没有 withDefaults() 方法。你需要添加一个静态导入:

  1. import static org.springframework.security.config.Customizer.withDefaults;
英文:

Your SecurityConfiguration, nor the WebSecurityConfigurerAdapter have a method withDefaults(). You need to add a static import:

  1. import static org.springframework.security.config.Customizer.withDefaults;

huangapple
  • 本文由 发表于 2020年10月4日 14:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64191637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定