创建名称为 'securityConfiguration' 的 bean 时出错:通过字段 'myAppUserDetailsService' 表达的依赖关系不满足;
Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService';
Spring 启动 1.5.3 项目,在内存 DB
上的 H2 上测试用户注册表
这是错误堆栈跟踪
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SMRTUserService': Unsatisfied dependency expressed through field 'userInfoDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMRTUserDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
有人可以帮助我理解这个问题吗?我无法解决这个错误。
测试控制器
public class CustomerControllerTest extends AbstractControllerTest {
@Test
@WithMockUser(roles = "ADMIN")
public void testShow() throws Exception {
mockMvc.perform(get("/customer/list")
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}
AbstractControllerTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
public abstract class AbstractControllerTest extends AbstractTest {
@Autowired protected MockMvc mockMvc;
@Autowired private FilterChainProxy filterChainProxy;
@Autowired private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(webApplicationContext)
.dispatchOptions(true)
.addFilters(filterChainProxy).build();
}
}
安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired private SMRTUserService myAppUserDetailsService;
@Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myAppUserDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
}
SMRTUSerService
@Service
@Slf4j
public class SMRTUserService implements UserDetailsService {
@Autowired private ISMRTUserDAO userInfoDAO;
@Autowired private SMRTUserRepository smrtuserRepository;
...
}
谢谢
好吧,你的异常已经很好地解释了问题所在:
Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
我假设您正在为您的测试导入一些测试数据?您的 SQL 语句中一定有错误。
如果您使用 keycloak 进行身份验证。你可能会得到这个错误。
jhipster 默认给你 8090 作为认证服务器,所以你必须改变它
解决方案:1 - 启动您的 keycloak 服务器
2 - go to your jhipster project main->ressources->config->application.yml
通过您的 keycloak 服务器 运行 所在的端口更改 issuer ui:例如:issuer-uri: http://localhost:8080 /auth/realms/demo
希望对您有所帮助
Spring 启动 1.5.3 项目,在内存 DB
上的 H2 上测试用户注册表这是错误堆栈跟踪
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SMRTUserService': Unsatisfied dependency expressed through field 'userInfoDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMRTUserDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
有人可以帮助我理解这个问题吗?我无法解决这个错误。
测试控制器
public class CustomerControllerTest extends AbstractControllerTest {
@Test
@WithMockUser(roles = "ADMIN")
public void testShow() throws Exception {
mockMvc.perform(get("/customer/list")
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}
AbstractControllerTest
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
public abstract class AbstractControllerTest extends AbstractTest {
@Autowired protected MockMvc mockMvc;
@Autowired private FilterChainProxy filterChainProxy;
@Autowired private WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(webApplicationContext)
.dispatchOptions(true)
.addFilters(filterChainProxy).build();
}
}
安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired private SMRTUserService myAppUserDetailsService;
@Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myAppUserDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
}
SMRTUSerService
@Service
@Slf4j
public class SMRTUserService implements UserDetailsService {
@Autowired private ISMRTUserDAO userInfoDAO;
@Autowired private SMRTUserRepository smrtuserRepository;
...
}
谢谢
好吧,你的异常已经很好地解释了问题所在:
Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
我假设您正在为您的测试导入一些测试数据?您的 SQL 语句中一定有错误。
如果您使用 keycloak 进行身份验证。你可能会得到这个错误。 jhipster 默认给你 8090 作为认证服务器,所以你必须改变它
解决方案:1 - 启动您的 keycloak 服务器
2 - go to your jhipster project main->ressources->config->application.yml
通过您的 keycloak 服务器 运行 所在的端口更改 issuer ui:例如:issuer-uri: http://localhost:8080 /auth/realms/demo
希望对您有所帮助