I am trying to build Spring Boot test to test rest api, so that I can get Principal from request and use that to identify user. Server returns {"timestamp":1502014507361,"status":403,"error":"Forbidden","message":"Access Denied","path":"/hello"}
What am I missing here?
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestTemplateTest {
@Autowired
TestRestTemplate testRestTemplate;
@LocalServerPort
private int serverPort;
@Test
public void testit() {
testRestTemplate.withBasicAuth("user", "password");
String responsePayload = testRestTemplate.getForObject("/hello", String.class);
}
@RestController
public class GreetingController {
@RequestMapping("/hello")
public String heipat(Principal principal) {
String string = "hello there";
return string;
}
@Configuration
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().anyRequest().hasRole("USER");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
Aucun commentaire:
Enregistrer un commentaire