这是我在webapp模块中的websecurity类
package com.ioyota.shareitnamchi.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.ioyota.core.util.AppConstants;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableJpaRepositories(basePackages="com.ioyota")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private com.ioyota.user.security.**CustomAuthenticationProvider** CustomAuthenticationProvider;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
/*http.authorizeRequests().antMatchers("/resources/**", "/registration","/greeting","/ignore/**","/*").permitAll()
.antMatchers("/admin/**")
.authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();
http.csrf().disable();*/
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
http.authorizeRequests().antMatchers(AppConstants.NAMCHI_ADMIN_URL+"/**").authenticated();
// Requests for the login page and the static assets are allowed
http.authorizeRequests()
.antMatchers("/resources/**", "/registration","/greeting","/ignore/**","/**")
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(this.CustomAuthenticationProvider);
}
}
自定义身份验证器在其他模块中定义。现在,当我使用运行方式->运行我的应用程序时,STS中的spring boot应用程序可以正常工作,但是如果我将其作为spring-boot运行:然后给出了这个错误
org.springframework.beans.factory.BeanCreationException:错误 在文件中定义名称为“ repoConf”的bean [F:\ namchi26july \ shareitnamchi系统\ shareitnamchi \ shareitnamchi-webapp \ target \ classes \ com \ ioyota \ shareitnamchi \ security \ WebSecurityConfig.class]: 合并bean定义的后处理失败;嵌套异常为 java.lang.IllegalStateException:无法自省类 来自ClassLoader的[com.ioyota.shareitnamchi.security.WebSecurityConfig] [java.net.URLClassLoader@25527c7d]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) 〜[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) 〜[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) 〜[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 com.ioyota.App.main(App.java:26)[classes /:na]在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 〜[na:1.8.0_161]在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[na:1.8.0_161]在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_161] at java.lang.reflect.Method.invoke(Method.java:498) 〜[na:1.8.0_161]在 org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.0.RELEASE.jar:2.0.0.RELEASE]位于 java.lang.Thread.run(Thread.java:748)[na:1.8.0_161]原因: java.lang.IllegalStateException:无法自省类 来自ClassLoader的[com.ioyota.shareitnamchi.security.WebSecurityConfig] [java.net.URLClassLoader@25527c7d]在 org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:759) 〜[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:691) 〜[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:410) 〜[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:394) 〜[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:332) 〜[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1016) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) 〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] ... 21个常见框架 省略的原因:java.lang.NoClassDefFoundError: Lcom / ioyota /用户/安全性/ CustomAuthenticationProvider;在 java.lang.Class.getDeclaredFields0(本机方法)〜[na:1.8.0_161]在 java.lang.Class.privateGetDeclaredFields(Class.java:2583) 〜[na:1.8.0_161] at java.lang.Class.getDeclaredFields(Class.java:1916) 〜[na:1.8.0_161]在 org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:754) 〜[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE] ... 27个常见框架 省略的原因:java.lang.ClassNotFoundException: com.ioyota.user.security.CustomAuthenticationProvider位于 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 〜[na:1.8.0_161]在 java.lang.ClassLoader.loadClass(ClassLoader.java:424)〜[na:1.8.0_161] 在java.lang.ClassLoader.loadClass(ClassLoader.java:357) 〜[na:1.8.0_161] ...省略了31个共同的框架
[警告]处的java.lang.reflect.InvocationTargetException sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:496) 在java.lang.Thread.run(Thread.java:748)造成原因: org.springframework.beans.factory.BeanCreationException:错误 在文件中定义名称为“ repoConf”的bean [F:\ namchi26july \ shareitnamchi系统\ shareitnamchi \ shareitnamchi-webapp \ target \ classes \ com \ ioyota \ shareitnamchi \ security \ WebSecurityConfig.class]: 合并bean定义的后处理失败;嵌套异常为 java.lang.IllegalStateException:无法自省类 来自ClassLoader的[com.ioyota.shareitnamchi.security.WebSecurityConfig] [java.net.URLClassLoader@25527c7d]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:327) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) 在com.ioyota.App.main(App.java:26)...上另外6个原因: java.lang.IllegalStateException:无法自省类 来自ClassLoader的[com.ioyota.shareitnamchi.security.WebSecurityConfig] [java.net.URLClassLoader@25527c7d]在 org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:759) 在 org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:691) 在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:410) 在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:394) 在 org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:332) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1016) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ... 21更多原因:java.lang.NoClassDefFoundError: Lcom / ioyota /用户/安全性/ CustomAuthenticationProvider;在 java.lang.Class.getDeclaredFields0(本机方法),位于 java.lang.Class.privateGetDeclaredFields(Class.java:2583)在 java.lang.Class.getDeclaredFields(Class.java:1916)在 org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:754) ... 27更多原因:java.lang.ClassNotFoundException: com.ioyota.user.security.CustomAuthenticationProvider位于 java.net.URLClassLoader.findClass(URLClassLoader.java:381)在 java.lang.ClassLoader.loadClass(ClassLoader.java:424)在 java.lang.ClassLoader.loadClass(ClassLoader.java:357)...还有31个 [INFO] ------------------
这是自定义身份验证文件
package com.ioyota.user.security; import java.util.HashSet; import java.util.List; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.DependsOn; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Component; import com.ioyota.core.dto.UserDTO; import com.ioyota.core.entity.RolePermission; import com.ioyota.user.repository.RolePermissionRepository; import com.ioyota.user.service.UserService;
@Component
@DependsOn("repoConf")
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserService userService;
@Autowired
private RolePermissionRepository rolePermissionRepository;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName().trim();
UserDTO user = null;
try {
user = userService.findByUsername(userName);
} catch (Exception e) {
}
if (user == null) {
throw new UsernameNotFoundException("Username or password is not correct");
}
if (!bCryptPasswordEncoder.matches(authentication.getCredentials().toString().trim(), user.getPassword())) {
throw new BadCredentialsException("Username or password is not correct");
}
// Authenticate the user based on your custom logic
List<RolePermission> rolePermissionsList = rolePermissionRepository.findByRoleId(user.getRole().getRoleId());
Set<GrantedAuthority> authList = new HashSet<GrantedAuthority>();
for (RolePermission rolePermission : rolePermissionsList) {
SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(
rolePermission.getPermissionId() + "");
authList.add(simpleGrantedAuthority);
}
return new UsernamePasswordAuthenticationToken(user, authentication.getCredentials().toString(), authList);
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
}
我尝试了许多解决方案,但没有一个起作用。 请指导