相关疑难解决方法(0)

使用spring boot禁用单元测试的安全性

我正在尝试用安全性创建一个简单的spring boot web项目.我可以正常启动应用程序,安全性正常.但是,我有一些我想要测试的组件没有安全性(或者根本没有测试 - 我根本无法进行测试).

我得到一个异常,表明它找不到ObjectPostProcessor,因此无法启动容器.

引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.springframework.security.config.annotation.ObjectPostProcessor]的限定bean用于依赖

14:01:50.937 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fmpdfApplication.ApplicationSecurity': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at …

spring-test spring-boot

34
推荐指数
5
解决办法
4万
查看次数

Spring Boot 2.0.x禁用某些配置文件的安全性

在Spring Boot 1.5.x中,我已经配置了安全性,并且在某些配置文件中(例如本地),我已经security.basic.enabled=false在.properties文件中添加了一行来禁用该配置文件的所有安全性.我正在尝试迁移到新的Spring Boot 2,其中删除了该配置属性.如何在Spring Boot 2.0.x中实现相同的行为(不使用此属性)?

我已经阅读过Spring-Boot-Security-2.0security-change-in-spring-boot-2-0-m4,这个属性没什么.

java spring-security spring-boot

10
推荐指数
3
解决办法
2万
查看次数

Spring Boot 集成测试结果在 401

我们有一个暴露 REST api 的 Spring Boot 1.4 Web 应用程序,我们想要运行一些集成测试。

在我们的测试中,我们让 Spring Boot 在本地启动 Web 应用程序,然后我们对 api 进行一些调用。

如果我们简单地运行 Web 应用程序本身并点击端点,我们会得到200/OK响应,这是预期的。然而,运行测试,我们得到了401/Unauthorized服务器响应。

我应该在哪里查看可能导致授权错误的原因?

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplication3IT {
    private final Logger log = LoggerFactory.getLogger(DemoApplication3IT.class);

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void getFunky() throws IOException {
        ResponseEntity<String> response =
            this.restTemplate.getForEntity("http://localhost:8080/api/funky", String.class);
        log.info("TestRestTemplate response Code: " + response.getStatusCode());
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); // Expected :200, Actual   :401
    }
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,使用浏览器访问端点工作正常,但集成测试失败。

spring-boot

6
推荐指数
1
解决办法
6790
查看次数

Spring Boot集成测试忽略了AutoConfigureMockMvc注释中的secure = false,得到401

不能让我的@SpringBootTest工作.它说身份验证已启用,我不想要.

我已经设置好了 @AutoConfigureMockMvc(secure = false)

我提交了一个带有一些JSON的模拟请求,我的集成测试应该测试整个堆栈,通过Web层将SDR带到JPA然后进入内存数据库,这样我就可以使用它进行测试JdbcTemplate.

但响应是401,需要身份验证.为什么@AutoConfigureMockMvc(secure = false)不够?少了什么东西?

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        classes = { TestDataSourceConfig.class })
@EnableAutoConfiguration
@AutoConfigureMockMvc(secure = false)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@Transactional
public class SymbolRestTests  {

    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SymbolRepository symbolRepository;
    @PersistenceContext
    private EntityManager entityManager;  

    @Test
    public void shouldCreateEntity() throws Exception {

        String testTitle = "TEST.CODE.1";
        String testExtra = "Test for SymbolRestTests.java";
        String json = createJsonExample(testTitle, testExtra, true);
        log.debug(String.format("JSON==%s", json));
        MockHttpServletRequestBuilder …
Run Code Online (Sandbox Code Playgroud)

tdd spring-mvc spring-security spring-data-rest spring-boot

5
推荐指数
2
解决办法
4192
查看次数