changed code indent by space instead of tab
This commit is contained in:
parent
061023b683
commit
81b2fbf150
@ -13,41 +13,41 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
|
|
||||||
public class ConversionClassUtil {
|
public class ConversionClassUtil {
|
||||||
|
|
||||||
/* Generating Secret key */
|
/* Generating Secret key */
|
||||||
|
|
||||||
// Generating Secret Key using KeyGenerator class with 256
|
// Generating Secret Key using KeyGenerator class with 256
|
||||||
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
|
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
|
||||||
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
||||||
keyGenerator.init(n);
|
keyGenerator.init(n);
|
||||||
SecretKey originalKey = keyGenerator.generateKey();
|
SecretKey originalKey = keyGenerator.generateKey();
|
||||||
return originalKey;
|
return originalKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generating Secret Key using password and salt
|
// Generating Secret Key using password and salt
|
||||||
public static SecretKey getKeyFromPassword(String password, String salt)
|
public static SecretKey getKeyFromPassword(String password, String salt)
|
||||||
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
||||||
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
|
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
|
||||||
SecretKey originalKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
|
SecretKey originalKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
|
||||||
return originalKey;
|
return originalKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Converting Secret key into String */
|
/* Converting Secret key into String */
|
||||||
public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException {
|
public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException {
|
||||||
// Converting the Secret Key into byte array
|
// Converting the Secret Key into byte array
|
||||||
byte[] rawData = secretKey.getEncoded();
|
byte[] rawData = secretKey.getEncoded();
|
||||||
// Getting String - Base64 encoded version of the Secret Key
|
// Getting String - Base64 encoded version of the Secret Key
|
||||||
String encodedKey = Base64.getEncoder().encodeToString(rawData);
|
String encodedKey = Base64.getEncoder().encodeToString(rawData);
|
||||||
return encodedKey;
|
return encodedKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Converting String into Secret key into */
|
/* Converting String into Secret key into */
|
||||||
public static SecretKey convertStringToSecretKeyto(String encodedKey) {
|
public static SecretKey convertStringToSecretKeyto(String encodedKey) {
|
||||||
// Decoding the Base64 encoded string into byte array
|
// Decoding the Base64 encoded string into byte array
|
||||||
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
|
byte[] decodedKey = Base64.getDecoder().decode(encodedKey);
|
||||||
// Rebuilding the Secret Key using SecretKeySpec Class
|
// Rebuilding the Secret Key using SecretKeySpec Class
|
||||||
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
|
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
|
||||||
return originalKey;
|
return originalKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,35 +10,35 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
public class ConversionClassUtilUnitTest {
|
public class ConversionClassUtilUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess()
|
void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess()
|
||||||
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||||
// given
|
// given
|
||||||
String password = "Baeldung@2021";
|
String password = "Baeldung@2021";
|
||||||
String salt = "@$#baelDunG@#^$*";
|
String salt = "@$#baelDunG@#^$*";
|
||||||
|
|
||||||
// when
|
// when
|
||||||
SecretKey encodedKey = ConversionClassUtil.getKeyFromPassword(password, salt);
|
SecretKey encodedKey = ConversionClassUtil.getKeyFromPassword(password, salt);
|
||||||
String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey);
|
String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey);
|
||||||
SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString);
|
SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Assertions.assertEquals(encodedKey, decodeKey);
|
Assertions.assertEquals(encodedKey, decodeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess()
|
void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess()
|
||||||
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||||
// given
|
// given
|
||||||
int size = 256;
|
int size = 256;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
SecretKey encodedKey = ConversionClassUtil.generateKey(size);
|
SecretKey encodedKey = ConversionClassUtil.generateKey(size);
|
||||||
String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey);
|
String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey);
|
||||||
SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString);
|
SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Assertions.assertEquals(encodedKey, decodeKey);
|
Assertions.assertEquals(encodedKey, decodeKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
@ComponentScan({ "com.baeldung.sampleapp.web" })
|
@ComponentScan({ "com.baeldung.sampleapp.web" })
|
||||||
public class MaxHTTPHeaderSizeConfig implements WebMvcConfigurer {
|
public class MaxHTTPHeaderSizeConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
public MaxHTTPHeaderSizeConfig() {
|
public MaxHTTPHeaderSizeConfig() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RequestMapping(value = "/request-header-test")
|
@RequestMapping(value = "/request-header-test")
|
||||||
public class MaxHttpHeaderSizeController {
|
public class MaxHttpHeaderSizeController {
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) {
|
public boolean testMaxHTTPHeaderSize(@RequestHeader(value = "token") String token) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,26 +23,26 @@ import com.baeldung.sampleapp.config.WebConfig;
|
|||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
public class MaxHttpHeaderSizeControllerIntegrationTest {
|
public class MaxHttpHeaderSizeControllerIntegrationTest {
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext webApplicationContext;
|
private WebApplicationContext webApplicationContext;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception {
|
public void givenTokenWithLessThan8KBLegth_whenSendGetRequest_thenReturnsOK() throws Exception {
|
||||||
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
|
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||||
.with(httpBasic("user", "password")).header("token", "token")).andExpect(status().isOk());
|
.with(httpBasic("user", "password")).header("token", "token")).andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTokenIsMissingInHeade_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
|
public void givenTokenIsMissingInHeade_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
|
||||||
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
|
mockMvc.perform(get("/request-header-test").contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||||
.with(httpBasic("user", "password"))).andExpect(status().isBadRequest());
|
.with(httpBasic("user", "password"))).andExpect(status().isBadRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,29 +25,29 @@ import com.baeldung.sampleapp.config.MaxHTTPHeaderSizeConfig;
|
|||||||
// Start MaxHttpHeaderSizeController Spring Boot App(MainApplication) first
|
// Start MaxHttpHeaderSizeController Spring Boot App(MainApplication) first
|
||||||
public class MaxHttpHeaderSizeControllerLiveTest {
|
public class MaxHttpHeaderSizeControllerLiveTest {
|
||||||
|
|
||||||
@Test(expected = HttpClientErrorException.class)
|
@Test(expected = HttpClientErrorException.class)
|
||||||
public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
|
public void givenTokenWithGreaterThan8KBLegth_whenSendGetRequest_thenThrowsBadRequest() throws Exception {
|
||||||
final String url = "http://localhost:8080/request-header-test";
|
final String url = "http://localhost:8080/request-header-test";
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.set("token", readRandomStringFromFile());
|
headers.set("token", readRandomStringFromFile());
|
||||||
|
|
||||||
HttpEntity entity = new HttpEntity(headers);
|
HttpEntity entity = new HttpEntity(headers);
|
||||||
final ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class);
|
final ResponseEntity<String> response = new RestTemplate().exchange(url, HttpMethod.GET, entity, String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String readRandomStringFromFile() throws IOException {
|
static String readRandomStringFromFile() throws IOException {
|
||||||
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt"));
|
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/randomSringForheader.txt"));
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
String line = null;
|
String line = null;
|
||||||
String ls = System.getProperty("line.separator");
|
String ls = System.getProperty("line.separator");
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
stringBuilder.append(line);
|
stringBuilder.append(line);
|
||||||
stringBuilder.append(ls);
|
stringBuilder.append(ls);
|
||||||
}
|
}
|
||||||
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
|
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
|
||||||
reader.close();
|
reader.close();
|
||||||
String content = stringBuilder.toString();
|
String content = stringBuilder.toString();
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user