Apress/pro-spring-5

Chapter 12 Using Spring Remoting / Section RESTful-WS with Spring Boot

fangtasticmrz opened this issue · 6 comments

The project is boot-rest. The current test code (RestClientTest) doesn't work because the server security, all the test cases are getting 401. To fix that, I have added the following code

@before
public void setUp() {
// restTemplate = new RestTemplate();
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
restTemplate = restTemplateBuilder.basicAuthorization("admin", "admin").build();

}

And I also add a application.properties file with following lines:

spring.security.user.name=admin
spring.security.user.password=admin

The the test code works for the non-modification ones (testFindAll, testFindById), but testCreate, testDelete, testUpdate are still getting 401. I couldn't quite figure it out why.

Then I had to change approach changing the @SpringBootApplication annotation to exclude the Security Configuration.

@SpringBootApplication(scanBasePackages = "com.apress.prospring5.ch12",
exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class}

)

This time everything works. But I would like to know why the first approach doesn't work for the modification types, what's missing?

Hello @fangtasticmrz,

You seem to have modified the dependencies of your project locally.

The boot-rest project does not use Spring Security, and thus the tests pass just fine.

Proof:
rest-proof

If you want to secure your application and write test for secured endpoints take a look at the secure-rest project. Of course you can use Spring boot security too, but it is not covered in this book.

Cheers,
Iuliana Cosmina

Thanks for the clarification. I'm not familiar with gradle so I have changed it to maven. So I might have messed it up during the transition. Thanks!

And you are opening PRs and raising issues when you are not using the original project with the recommended setup? Really? :))))

Sorry, yes this question is probably more appropriate in a "forum" rather than "issues". Well, just thought it might be helpful for people running into the similar issue. And do appreciate your time. Thanks!

@fangtasticmrz I'm not bothered by questions, and in the book you have my email and send them to me directly. When you create issues on GitHub people buying the book might think there is something wrong with the project. And thus, the book and sources are not good enough and maybe not worth their money.

Or you can make a PR with your Maven setup so I can validate it and contribute to it to align it with the original intentions of the project. ;)

@iuliana Got it, thanks!