我在Spring Boot 1.4之前进行的OAuth集成测试如下所示(更新只是为了不使用已弃用的功能):
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
public class OAuth2IntegrationTest {
@Value("${local.server.port}")
private int port;
private static final String CLIENT_NAME = "client";
private static final String CLIENT_PASSWORD = "123456";
@Test
public void testOAuthAccessTokenIsReturned() {
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
request.set("username", "user");
request.set("password", password);
request.set("grant_type", "password");
@SuppressWarnings("unchecked")
Map<String, Object> token = new TestRestTemplate(CLIENT_NAME, CLIENT_PASSWORD)
.postForObject("http://localhost:" + port + "/oauth/token", request, Map.class);
assertNotNull("Wrong response: " + token, token.get("access_token"));
}
}
Run Code Online (Sandbox Code Playgroud)
我现在想要使用如此处所述的Autowired TestRestTemplate http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-应用程序-工作-与随机端口
@RunWith(SpringRunner.class)
@SpringBootTest(classes …Run Code Online (Sandbox Code Playgroud) 我们最近从Spring Boot 1.4.1升级到1.5.2。1.5.2的功能之一是,如果Spring Security是该软件包的一部分,那么它将受到基本身份验证的保护。/h2-console基本身份验证后,我无法访问。禁止抛出403。
application.yml:
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:file:../app-db/app_db;AUTO_SERVER=TRUE
username: sa
password: sa
initialize: false
jpa:
hibernate:
ddl-auto: validate
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
h2:
console:
enabled: true
settings:
web-allow-others: true
allowed:
resources: /h2-console/**
Run Code Online (Sandbox Code Playgroud)
我什至明确允许 /h2-console/**
httpSecurity.authorizeRequests()
.antMatchers(allowedResources)
.permitAll()
Run Code Online (Sandbox Code Playgroud)
尝试访问时,我一直收到403 localhost:8080/h2-console。我尝试了许多设置以及放置:
management.security.enabled=true
security.basic.enabled=true
Run Code Online (Sandbox Code Playgroud)
但是我无法访问h2-console。