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