diff --git a/jmeter/pom.xml b/jmeter/pom.xml index 199d95e0d6..b2e4197dfc 100644 --- a/jmeter/pom.xml +++ b/jmeter/pom.xml @@ -33,6 +33,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-security + diff --git a/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java b/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java new file mode 100644 index 0000000000..8700dc3df4 --- /dev/null +++ b/jmeter/src/main/java/com/baeldung/configuration/WebSecurityConfiguration.java @@ -0,0 +1,52 @@ +package com.baeldung.configuration; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.factory.PasswordEncoderFactories; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + + PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); + + auth.inMemoryAuthentication() + .withUser("admin").password(encoder.encode("admin")).roles("USER", "ADMIN") + .and() + .withUser("user1").password(encoder.encode("password1")).roles("USER") + .and() + .withUser("user2").password(encoder.encode("password2")).roles("USER") + .and() + .withUser("user3").password(encoder.encode("password3")).roles("USER") + .and() + .withUser("user4").password(encoder.encode("password4")).roles("USER") + .and() + .withUser("user5").password(encoder.encode("password5")).roles("USER") + .and() + .withUser("user6").password(encoder.encode("password6")).roles("USER") + .and() + .withUser("user7").password(encoder.encode("password7")).roles("USER") + .and() + .withUser("user8").password(encoder.encode("password8")).roles("USER") + .and() + .withUser("user9").password(encoder.encode("password9")).roles("USER") + .and() + .withUser("user10").password(encoder.encode("password10")).roles("USER"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + + http + .authorizeRequests() + .antMatchers("/secured/**").authenticated() + .anyRequest().permitAll() + .and() + .httpBasic(); + } +} diff --git a/jmeter/src/main/java/com/baeldung/controller/SecuredUuidController.java b/jmeter/src/main/java/com/baeldung/controller/SecuredUuidController.java new file mode 100644 index 0000000000..fa6570ff6f --- /dev/null +++ b/jmeter/src/main/java/com/baeldung/controller/SecuredUuidController.java @@ -0,0 +1,24 @@ +package com.baeldung.controller; + +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.model.Response; + +@RestController +public class SecuredUuidController { + + private static final Logger LOGGER = LoggerFactory.getLogger(SecuredUuidController.class); + + @GetMapping("/secured/uuid") + public Response uuid() { + + LOGGER.info("Returning response"); + + return new Response(String.format("Secured test message... %s.", UUID.randomUUID())); + } +} diff --git a/jmeter/src/main/resources/Basic Authentication - JSR223.jmx b/jmeter/src/main/resources/Basic Authentication - JSR223.jmx new file mode 100644 index 0000000000..b15b625b95 --- /dev/null +++ b/jmeter/src/main/resources/Basic Authentication - JSR223.jmx @@ -0,0 +1,164 @@ + + + + + + false + false + + + + + + + + + + host + localhost + = + Host of Webservice + + + port + 8080 + Port of web server + = + + + username + user1 + = + + + password + password1 + = + + + + + + continue + + false + 1 + + 1 + 1 + 1375525852000 + 1375525852000 + false + + + false + + + + + + + ${host} + ${port} + + + /secured/uuid + GET + false + false + false + false + + + + + + + false + + + import org.apache.commons.codec.binary.Base64; + +String username = vars.get("username"); +String password = vars.get("password"); +String credentials = username + ":" + password; +byte[] encodedUsernamePassword = Base64.encodeBase64(credentials.getBytes()); +vars.put("base64Credentials", new String(encodedUsernamePassword)); + + java + + + + + + Content-Type + text/xml; charset=utf-8 + + + Authorization + basic ${base64Credentials} + + + + + + + 200 + + + Assertion.response_code + false + 8 + + + + + Secured test message... + + Verify content in response + Assertion.response_data + false + 16 + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + false + false + true + false + false + false + false + true + false + false + true + true + 0 + true + true + true + true + + + + + + + + diff --git a/jmeter/src/main/resources/Basic Authentication - authorization manager with CSV.jmx b/jmeter/src/main/resources/Basic Authentication - authorization manager with CSV.jmx new file mode 100644 index 0000000000..8309cfd6f5 --- /dev/null +++ b/jmeter/src/main/resources/Basic Authentication - authorization manager with CSV.jmx @@ -0,0 +1,166 @@ + + + + + + false + false + + + + + + + + + + host + localhost + = + Host of Webservice + + + port + 8080 + Port of web server + = + + + csvFileLocation + D:\\work\\credentials.csv + = + + + + + + + + Content-Type + text/xml; charset=utf-8 + + + + + + continue + + false + 20 + + 1 + 0 + 1375525852000 + 1375525852000 + false + + + false + + + + , + + ${csvFileLocation} + false + false + true + shareMode.all + false + username,password + + + + + + http://${host}:${port}/secured/uuid + ${username} + ${password} + + + + + false + true + + + + + + + ${host} + ${port} + + + /secured/uuid + GET + false + false + false + false + + + + + + + + 200 + + + Assertion.response_code + false + 8 + + + + + Secured test message... + + Verify content in response + Assertion.response_data + false + 16 + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + false + false + true + false + false + false + false + true + false + false + true + true + 0 + true + true + true + true + + + + + + + + diff --git a/jmeter/src/main/resources/Basic Authentication - authorization manager.jmx b/jmeter/src/main/resources/Basic Authentication - authorization manager.jmx new file mode 100644 index 0000000000..7d48a4c097 --- /dev/null +++ b/jmeter/src/main/resources/Basic Authentication - authorization manager.jmx @@ -0,0 +1,159 @@ + + + + + + false + false + + + + + + + + + + host + localhost + = + Host of Webservice + + + port + 8080 + Port of web server + = + + + username + user1 + = + + + password + password1 + = + + + + + + + + Content-Type + text/xml; charset=utf-8 + + + + + + continue + + false + 1 + + 1 + 0 + 1375525852000 + 1375525852000 + false + + + false + + + + + + http://${host}:${port}/secured/uuid + ${username} + ${password} + + + + + false + true + + + + + + + ${host} + ${port} + + + /secured/uuid + GET + false + false + false + false + + + + + + + + 200 + + + Assertion.response_code + false + 8 + + + + + Secured test message... + + Verify content in response + Assertion.response_data + false + 16 + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + false + false + true + false + false + false + false + true + false + false + true + true + 0 + true + true + true + true + + + + + + + + diff --git a/jmeter/src/main/resources/Basic Authentication.jmx b/jmeter/src/main/resources/Basic Authentication.jmx new file mode 100644 index 0000000000..e23d47717d --- /dev/null +++ b/jmeter/src/main/resources/Basic Authentication.jmx @@ -0,0 +1,139 @@ + + + + + + false + false + + + + + + + + + + host + localhost + = + Host of Webservice + + + port + 8080 + Port of web server + = + + + + + + continue + + false + 2 + + 5 + 5 + 1375525852000 + 1375525852000 + false + + + false + + + + + + + ${host} + ${port} + + + /secured/uuid + GET + false + false + false + false + + + + + + + + + Content-Type + text/xml; charset=utf-8 + + + Authorization + dXNlcjE6cGFzc3dvcmQx + + + + + + + 200 + + + Assertion.response_code + false + 8 + + + + + Secured test message... + + Verify content in response + Assertion.response_data + false + 16 + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + false + false + true + false + false + false + false + true + false + false + true + true + 0 + true + true + true + true + + + + + + + + diff --git a/jmeter/src/main/resources/credentials.csv b/jmeter/src/main/resources/credentials.csv new file mode 100644 index 0000000000..9aba7cf181 --- /dev/null +++ b/jmeter/src/main/resources/credentials.csv @@ -0,0 +1,10 @@ +user1,password1 +user2,password2 +user3,password3 +user4,password4 +user5,password5 +user6,password6 +user7,password7 +user8,password8 +user9,password9 +user10,password10 \ No newline at end of file