Spring Framework的Rest API + 添加的Spring Security错误 + 依赖

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

Springfeamework Rest api + added spring-security bug + depdencies

问题

Sure, here's the translated code portion:

  1. java.lang.IllegalStateException: org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.propertySourcesPlaceholderConfigurer 上处理条件时出错
  2. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  3. ...
  4. Caused by: java.lang.IllegalStateException: ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6ed3ef1] 中内省 Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] 失败
  5. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  6. ...
  7. Caused by: java.lang.NoClassDefFoundError: org/springframework/security/web/access/WebInvocationPrivilegeEvaluator
  8. ...
  9. ...
  1. // build.gradle
  2. plugins {
  3. id 'org.springframework.boot' version '2.3.3.RELEASE'
  4. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  5. id 'java'
  6. }
  7. group = 'com.shopArt'
  8. version = '0.0.1-SNAPSHOT'
  9. sourceCompatibility = '11'
  10. configurations {
  11. compileOnly {
  12. extendsFrom annotationProcessor
  13. }
  14. }
  15. repositories {
  16. mavenCentral()
  17. }
  18. dependencies {
  19. // implementation statements
  20. ...
  21. }
  22. test {
  23. useJUnitPlatform()
  24. }
  1. // SecurityConfig.class
  2. import com.shopArt.shopArt.validator.UserPasswordValidator;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.security.authentication.AuthenticationManager;
  8. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  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.builders.WebSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  13. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  14. import org.springframework.security.core.userdetails.UserDetailsService;
  15. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  16. import org.springframework.security.crypto.password.PasswordEncoder;
  17. import org.springframework.validation.Validator;
  18. @Configuration
  19. @EnableWebSecurity
  20. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  21. @Autowired
  22. @Qualifier("customUserDetailsService")
  23. private UserDetailsService userDetailsService;
  24. @Bean
  25. public PasswordEncoder passwordEncoder() {
  26. return new BCryptPasswordEncoder();
  27. }
  28. @Bean
  29. public Validator userPasswordValidator() {
  30. return new UserPasswordValidator();
  31. }
  32. @Override
  33. public void configure(final WebSecurity web) throws Exception {
  34. web.ignoring().antMatchers("/resources/**");
  35. }
  36. @Bean
  37. @Override
  38. public AuthenticationManager authenticationManagerBean() throws Exception {
  39. return super.authenticationManagerBean();
  40. }
  41. @Override
  42. protected void configure(final HttpSecurity http) throws Exception {
  43. http
  44. .csrf().disable()
  45. .authorizeRequests()
  46. // antMatchers and other configurations
  47. ...
  48. .permitAll();
  49. }
  50. @Override
  51. protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
  52. auth.userDetailsService(userDetailsService);
  53. DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  54. authProvider.setUserDetailsService(userDetailsService);
  55. authProvider.setPasswordEncoder(passwordEncoder());
  56. auth.authenticationProvider(authProvider);
  57. }
  58. }

You can find the full code on this GitHub repository: https://github.com/Dergaxhtml/shopArt/tree/feature/register

英文:

> My rest api after add dependencies security spring and SecurityConfig doesnt work. I dont undestand it, because in rest api dont know which problem is It. Befor I added dependencies, app was working(confirm password, by getting json didnt work only).

  1. java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration.propertySourcesPlaceholderConfigurer
  2. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  3. at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  4. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:184) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  5. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:144) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  6. at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  7. at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  8. at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  9. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  10. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  11. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  12. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  13. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  14. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  15. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  16. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  17. at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  18. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  19. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  20. at com.shopArt.shopArt.ShopArtApplication.main(ShopArtApplication.java:9) ~[main/:na]
  21. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
  22. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
  23. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
  24. at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
  25. at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  26. Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6ed3ef1]
  27. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  28. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  29. at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  30. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:742) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  31. at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705) ~[na:na]
  32. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:741) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  33. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:680) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  34. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:648) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  35. at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1614) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  36. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:523) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  37. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:495) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  38. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:238) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  39. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:231) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  40. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:221) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  41. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:169) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  42. at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:144) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  43. at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.3.3.RELEASE.jar:2.3.3.RELEASE]
  44. ... 23 common frames omitted
  45. Caused by: java.lang.NoClassDefFoundError: org/springframework/security/web/access/WebInvocationPrivilegeEvaluator
  46. at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
  47. at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na]
  48. at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310) ~[na:na]
  49. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
  50. ... 39 common frames omitted
  51. Caused by: java.lang.ClassNotFoundException: org.springframework.security.web.access.WebInvocationPrivilegeEvaluator
  52. at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]
  53. at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
  54. at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
  55. ... 43 common frames omitted

build.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.3.3.RELEASE'
  3. id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.shopArt'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '11'
  9. configurations {
  10. compileOnly {
  11. extendsFrom annotationProcessor
  12. }
  13. }
  14. repositories {
  15. mavenCentral()
  16. }
  17. dependencies {
  18. implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
  19. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  20. implementation 'org.springframework.boot:spring-boot-starter-data-rest'
  21. implementation 'org.springframework.boot:spring-boot-starter-jdbc'
  22. implementation 'org.springframework.boot:spring-boot-starter-web'
  23. implementation 'org.springframework.session:spring-session-jdbc'
  24. compileOnly 'org.projectlombok:lombok'
  25. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  26. runtimeOnly 'mysql:mysql-connector-java'
  27. annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
  28. annotationProcessor 'org.projectlombok:lombok'
  29. testImplementation('org.springframework.boot:spring-boot-starter-test') {
  30. exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  31. }
  32. // // https://mvnrepository.com/artifact/org.springframework.security/spring-security-core
  33. // compile group: 'org.springframework.security', name: 'spring-security-core', version: '2.0.2'
  34. // https://mvnrepository.com/artifact/org.springframework.security/spring-security-config
  35. compile group: 'org.springframework.security', name: 'spring-security-config', version: '3.2.0.RELEASE'
  36. // https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto
  37. compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '3.1.0.RELEASE'
  38. // https://mvnrepository.com/artifact/org.springframework.security/spring-security-core
  39. compile group: 'org.springframework.security', name: 'spring-security-core', version: '5.3.4.RELEASE'
  40. // https://mvnrepository.com/artifact/javax.validation/validation-api
  41. compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
  42. // https://mvnrepository.com/artifact/org.springframework.plugin/spring-plugin-core
  43. // compile group: 'org.springframework.plugin', name: 'spring-plugin-core', version: '2.0.0.RELEASE'
  44. // https://mvnrepository.com/artifact/org.springframework/spring-webmvc
  45. // compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.0.RELEASE'
  46. }
  47. test {
  48. useJUnitPlatform()
  49. }

ScuirtyConfig.class

  1. import com.shopArt.shopArt.validator.UserPasswordValidator;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.authentication.AuthenticationManager;
  7. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  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.builders.WebSecurity;
  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.core.userdetails.UserDetailsService;
  14. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  15. import org.springframework.security.crypto.password.PasswordEncoder;
  16. import org.springframework.validation.Validator;
  17. @Configuration
  18. @EnableWebSecurity
  19. public class SecurityConfig
  20. extends WebSecurityConfigurerAdapter {
  21. @Autowired
  22. @Qualifier("customUserDetailsService")
  23. private UserDetailsService userDetailsService;
  24. @Bean
  25. public PasswordEncoder passwordEncoder() {
  26. return new BCryptPasswordEncoder();
  27. }
  28. @Bean
  29. public Validator userPasswordValidator() {
  30. return new UserPasswordValidator();
  31. }
  32. @Override
  33. public void configure(final WebSecurity web) throws Exception {
  34. web.ignoring().antMatchers("/resources/**");
  35. }
  36. @Bean
  37. @Override
  38. public AuthenticationManager authenticationManagerBean() throws Exception {
  39. return super.authenticationManagerBean();
  40. }
  41. @Override
  42. protected void configure(final HttpSecurity http) throws Exception {
  43. http
  44. .csrf().disable()
  45. .authorizeRequests()
  46. .antMatchers("/").permitAll()
  47. //.antMatchers("/invalidSession*").anonymous()
  48. //.antMatchers("/user/updatePassword*").hasAuthority("CHANGE_PASSWORD_PRIVILEGE")
  49. // .anyRequest().hasAuthority("READ_PRIVILEGE")
  50. .and()
  51. .formLogin().disable()
  52. // .loginPage("/login")
  53. // .defaultSuccessUrl("/homepage.html")
  54. // .failureUrl("/login?error=true")
  55. // .successHandler(myAuthenticationSuccessHandler)
  56. // .failureHandler(authenticationFailureHandler)
  57. // .authenticationDetailsSource(authenticationDetailsSource)
  58. // .permitAll()
  59. // .and()
  60. // .sessionManagement()
  61. // .invalidSessionUrl("/invalidSession.html")
  62. // .maximumSessions(1).sessionRegistry(sessionRegistry()).and()
  63. // .sessionFixation().none()
  64. // .and()
  65. .logout()
  66. // .logoutSuccessHandler(myLogoutSuccessHandler)
  67. // .invalidateHttpSession(false)
  68. // .logoutSuccessUrl("/logout.html?logSucc=true")
  69. // .deleteCookies("JSESSIONID")
  70. .permitAll();
  71. // .and()
  72. // .rememberMe().rememberMeServices(rememberMeServices()).key("theKey");
  73. }
  74. @Override
  75. protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
  76. auth.userDetailsService(userDetailsService);
  77. DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  78. authProvider.setUserDetailsService(userDetailsService);
  79. authProvider.setPasswordEncoder(passwordEncoder());
  80. auth.authenticationProvider(authProvider);
  81. }
  82. }

> https://github.com/Dergaxhtml/shopArt/tree/feature/register All code is here

答案1

得分: 1

'org.springframework.boot:spring-boot-starter-security'添加为依赖项

  1. dependencies {
  2. implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
  3. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  4. implementation 'org.springframework.boot:spring-boot-starter-data-rest'
  5. implementation 'org.springframework.boot:spring-boot-starter-security'
  6. implementation 'org.springframework.boot:spring-boot-starter-jdbc'
  7. implementation 'org.springframework.boot:spring-boot-starter-web'
  8. implementation 'org.springframework.session:spring-session-jdbc'
  9. }
英文:

Add 'org.springframework.boot:spring-boot-starter-security' as dependency

  1. dependencies {
  2. implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
  3. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  4. implementation 'org.springframework.boot:spring-boot-starter-data-rest'
  5. implementation 'org.springframework.boot:spring-boot-starter-security'
  6. implementation 'org.springframework.boot:spring-boot-starter-jdbc'
  7. implementation 'org.springframework.boot:spring-boot-starter-web'
  8. implementation 'org.springframework.session:spring-session-jdbc'

huangapple
  • 本文由 发表于 2020年9月12日 11:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63856599.html
匿名

发表评论

匿名网友

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

确定