diff --git a/spring-boot-modules/spring-boot-cli/pom.xml b/spring-boot-modules/spring-boot-cli/pom.xml new file mode 100644 index 0000000000..d2c50590ab --- /dev/null +++ b/spring-boot-modules/spring-boot-cli/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + spring-boot-cli + spring-boot-cli + jar + + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/Application.java b/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..c0490d50c6 --- /dev/null +++ b/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/controller/LoginController.java b/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/controller/LoginController.java new file mode 100644 index 0000000000..b678f63623 --- /dev/null +++ b/spring-boot-modules/spring-boot-cli/src/main/java/com/baeldung/controller/LoginController.java @@ -0,0 +1,13 @@ +package com.baeldung.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class LoginController { + + @GetMapping("/") + public String hello() { + return "Hello World!"; + } +} diff --git a/spring-boot-modules/spring-boot-cli/src/main/resources/application.properties b/spring-boot-modules/spring-boot-cli/src/main/resources/application.properties new file mode 100644 index 0000000000..4b341094a1 --- /dev/null +++ b/spring-boot-modules/spring-boot-cli/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.security.user.name=baeldung +# Encoded password with SpringBoot CLI, the decoded password is baeldungPassword +spring.security.user.password={bcrypt}$2y$10$R8VIwFiQ7aUST17YqMaWJuxjkCYqk3jjPlSxyDLLzqCTOwFuJNq2a \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-cli/src/test/java/com/baeldung/encoding/PasswordEncodingUnitTest.java b/spring-boot-modules/spring-boot-cli/src/test/java/com/baeldung/encoding/PasswordEncodingUnitTest.java new file mode 100644 index 0000000000..8939029f71 --- /dev/null +++ b/spring-boot-modules/spring-boot-cli/src/test/java/com/baeldung/encoding/PasswordEncodingUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.encoding; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureMockMvc +public class PasswordEncodingUnitTest { + private final static String userName = "baeldung"; + private final static String passwordDecoded = "baeldungPassword"; + + private MockMvc mvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setup() { + mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(springSecurity()) + .build(); + } + + @Test + public void givenRequestWithWrongPassword_shouldFailWith401() throws Exception { + mvc.perform(get("/").with(httpBasic(userName, "wrongPassword"))) + .andExpect(status().isUnauthorized()); + + } + + @Test + public void givenRequestWithCorrectDecodedPassword_houldSucceedWith200() throws Exception { + mvc.perform(get("/").with(httpBasic(userName, passwordDecoded))) + .andExpect(status().isOk()); + + } +} \ No newline at end of file