【问题标题】:Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService';创建名称为“securityConfiguration”的 bean 时出错:通过字段“myAppUserDetailsS​​ervice”表达的依赖关系不满足;
【发布时间】:2018-03-16 13:08:35
【问题描述】:

Spring boot 1.5.3 项目,在内存数据库中的 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;
    ...
}

谢谢

【问题讨论】:

    标签: security spring-boot autowired mockmvc


    【解决方案1】:

    好吧,您的异常已经很好地解释了问题所在:

    Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
    

    我假设您正在为您的测试导入一些测试数据?您的 SQL 语句中一定有错误。

    【讨论】:

    • 我只有一个带有 INSERT 语句的 src/test/resources/data.sql (没有 CREATE table 语句,而且我没有 CREATE table .sql 文件) .
    • 在Stacktrace的最后有: Caused by: org.h2.jdbc.JdbcSQLException: Table "ROLE" not found; SQL 语句: 我尝试将创建代码放入其中,但我找到了一种自动生成它的方法(ddl create)。
    • 你有它。 hibernate.hbm2ddl.auto=create 在你的属性中应该可以做到这一点
    • 我知道,但是错误总是一样的,还有一些东西给我带来了麻烦!
    • 你有角色表的实体类吗?你能提供型号代码和你的属性吗?
    【解决方案2】:

    如果您使用 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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-28
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2019-08-07
      • 2019-02-22
      • 2019-10-11
      相关资源
      最近更新 更多