diff --git a/core-java/pom.xml b/core-java/pom.xml index 27daf5f0f8..131a328834 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -146,11 +146,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 2.4.1 @@ -181,7 +181,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/experiments/pom.xml b/experiments/pom.xml index f814928f4a..b7794ef2f4 100644 --- a/experiments/pom.xml +++ b/experiments/pom.xml @@ -256,11 +256,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -288,7 +288,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/gson/.classpath b/gson/.classpath new file mode 100644 index 0000000000..0720e4851b --- /dev/null +++ b/gson/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/gson/.gitignore b/gson/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/gson/.project b/gson/.project new file mode 100644 index 0000000000..ea8ae1a67f --- /dev/null +++ b/gson/.project @@ -0,0 +1,36 @@ + + + jackson + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/gson/.springBeans b/gson/.springBeans new file mode 100644 index 0000000000..a79097f40d --- /dev/null +++ b/gson/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/api-servlet.xml + + + + diff --git a/gson/README.md b/gson/README.md new file mode 100644 index 0000000000..559e536d81 --- /dev/null +++ b/gson/README.md @@ -0,0 +1,6 @@ +========= + +## GSON Cookbooks and Examples + +### Relevant Articles: + diff --git a/gson/pom.xml b/gson/pom.xml new file mode 100644 index 0000000000..9c041f0cc1 --- /dev/null +++ b/gson/pom.xml @@ -0,0 +1,151 @@ + + 4.0.0 + org.baeldung + gson + 0.1-SNAPSHOT + + gson + + + + + + + com.google.guava + guava + 17.0 + + + commons-io + commons-io + 2.4 + + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + + + com.google.code.gson + gson + ${gson.version} + + + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + gson + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 4.0.6.RELEASE + 3.2.5.RELEASE + + + 4.3.6.Final + 5.1.32 + + + 2.3 + + + 1.7.7 + 1.1.2 + + + 5.1.2.Final + + + 17.0 + 3.3.2 + + + 1.3 + 4.11 + 1.9.5 + + 4.3.2 + 4.3.5 + + 2.3.2 + + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.9 + + + + \ No newline at end of file diff --git a/gson/src/main/resources/logback.xml b/gson/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/gson/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/api-servlet.xml b/gson/src/main/webapp/WEB-INF/api-servlet.xml new file mode 100644 index 0000000000..a675fc6d95 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/api-servlet.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/web.xml b/gson/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..48d4b8fe61 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + Spring MVC Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.config + + + + org.springframework.web.context.ContextLoaderListener + + + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + + + + + + + \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java new file mode 100644 index 0000000000..64720f63f9 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/Foo.java @@ -0,0 +1,51 @@ +package org.baeldung.gson.deserialization; + +public class Foo { + public int intValue; + public String stringValue; + + public Foo(final int intValue, final String stringValue) { + this.intValue = intValue; + this.stringValue = stringValue; + } + + public Foo() { + super(); + } + + // API + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + intValue; + result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Foo other = (Foo) obj; + if (intValue != other.intValue) + return false; + if (stringValue == null) { + if (other.stringValue != null) + return false; + } else if (!stringValue.equals(other.stringValue)) + return false; + return true; + } + + @Override + public String toString() { + return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java new file mode 100644 index 0000000000..17c1dfae19 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializer.java @@ -0,0 +1,26 @@ +package org.baeldung.gson.deserialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +public class FooDeserializer implements JsonDeserializer { + + @Override + public Foo[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { + final JsonArray jArray = json.getAsJsonArray(); + final Foo[] scArray = new Foo[jArray.size()]; + int index = 0; + for (final JsonElement jElement : jArray) { + final int i = jElement.getAsJsonObject().get("intValue").getAsInt(); + final String s = jElement.getAsJsonObject().get("stringValue").getAsString(); + scArray[index++] = new Foo(i, s); + } + return scArray; + } + +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java new file mode 100644 index 0000000000..d4eaa39ac5 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/FooDeserializerFromJsonWithDifferentFields.java @@ -0,0 +1,21 @@ +package org.baeldung.gson.deserialization; + +import java.lang.reflect.Type; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +public class FooDeserializerFromJsonWithDifferentFields implements JsonDeserializer { + + @Override + public Foo deserialize(final JsonElement jElement, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { + final JsonObject jObject = jElement.getAsJsonObject(); + final int intValue = jObject.get("valueInt").getAsInt(); + final String stringValue = jObject.get("valueString").getAsString(); + return new Foo(intValue, stringValue); + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java new file mode 100644 index 0000000000..954cf007cf --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericFoo.java @@ -0,0 +1,18 @@ +package org.baeldung.gson.deserialization; + +public class GenericFoo { + + public T theValue; + + public GenericFoo(final T value) { + theValue = value; + } + + // + + @Override + public final String toString() { + return "GenericTargetClass{" + "intField=" + theValue + '}'; + } + +} diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java new file mode 100644 index 0000000000..342b3096d7 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationTest.java @@ -0,0 +1,111 @@ +package org.baeldung.gson.deserialization.test; + +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; + +import org.baeldung.gson.deserialization.Foo; +import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields; +import org.baeldung.gson.deserialization.GenericFoo; +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationTest { + + // tests - single element + + @Test + public final void whenDeserializingToSimpleObject_thenCorrect() { + final String json = "{\"intValue\":1,\"stringValue\":\"one\"}"; + + final Foo targetObject = new Gson().fromJson(json, Foo.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + } + + @Test + public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect() { + final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}"; + final Foo targetObject = new Gson().fromJson(json, Foo.class); + + assertEquals(targetObject.intValue, 1); + assertEquals(targetObject.stringValue, "one"); + } + + @Test + public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect() { + final String json = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + + final GsonBuilder gsonBldr = new GsonBuilder(); + gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields()); + final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class); + + assertEquals(targetObject.intValue, 7); + assertEquals(targetObject.stringValue, "seven"); + } + + @Test + public final void whenDeserializingToGenericObject_thenCorrect() { + final Type typeToken = new TypeToken>() { + }.getType(); + final String json = "{\"theValue\":1}"; + + final GenericFoo targetObject = new Gson().fromJson(json, typeToken); + + assertEquals(targetObject.theValue, new Integer(1)); + } + + // tests - multiple elements + + @Test + public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() { + final String json = "[{\"intValue\":1,\"stringValue\":\"one\"}," + "{\"intValue\":2,\"stringValue\":\"two\"}]"; + final Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class); + + assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one"))); + assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two"))); + assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two")))); + } + + @Test + public final void givenJsonArrayOfFoos_whenDeserializingCollection_thenCorrect() { + final String json = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]"; + final Type targetClassType = new TypeToken>() { + }.getType(); + + final Collection targetCollection = new Gson().fromJson(json, targetClassType); + assertThat(targetCollection, instanceOf(ArrayList.class)); + } + + // + + @Test + public void whenDeserializingJsonIntoElements_thenCorrect() { + final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}"; + final JsonParser jParser = new JsonParser(); + final JsonElement jElement = jParser.parse(jsonSourceObject); + final JsonObject jObject = jElement.getAsJsonObject(); + final int intValue = jObject.get("valueInt").getAsInt(); + final String stringValue = jObject.get("valueString").getAsString(); + + final Foo targetObject = new Foo(intValue, stringValue); + + assertEquals(targetObject.intValue, 7); + assertEquals(targetObject.stringValue, "seven"); + } + +} diff --git a/gson/src/test/resources/.gitignore b/gson/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/guava/pom.xml b/guava/pom.xml index 18142c12f2..0297b706b6 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -95,11 +95,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -127,7 +127,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 6aa5f96344..529fdb2c38 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -150,11 +150,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -182,7 +182,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/jackson/pom.xml b/jackson/pom.xml index d017c7b65c..7d7b6421a4 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -1,150 +1,157 @@ - - 4.0.0 - org.baeldung - jackson - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + jackson + 0.1-SNAPSHOT - jackson + jackson - + - + - - com.google.guava - guava - 16.0.1 - - - commons-io - commons-io - 2.4 - + + com.google.guava + guava + 17.0 + + + commons-io + commons-io + 2.4 + - - org.apache.commons - commons-collections4 - 4.0 - + + org.apache.commons + commons-collections4 + 4.0 + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + + com.google.code.gson + gson + 2.3 + - - junit - junit-dep - ${junit.version} - test - + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + junit + junit-dep + ${junit.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - + + org.mockito + mockito-core + ${mockito.version} + test + - - jackson - - - src/main/resources - true - - + - + + jackson + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - - - 4.0.6.RELEASE - 3.2.4.RELEASE + - - 4.3.6.Final - 5.1.31 + + + 4.0.6.RELEASE + 3.2.5.RELEASE - - 2.4.1 + + 4.3.6.Final + 5.1.32 - - 1.7.7 - 1.1.2 + + 2.4.1 - - 5.1.2.Final + + 1.7.7 + 1.1.2 - - 17.0 - 3.3.2 + + 5.1.2.Final - - 1.3 - 4.11 - 1.9.5 + + 17.0 + 3.3.2 - 4.3.2 - 4.3.5 + + 1.3 + 4.11 + 1.9.5 - 2.3.2 + 4.3.2 + 4.3.5 - - 3.1 - 2.4 - 2.17 - 2.6 - 1.4.8 + 2.3.2 - + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.9 + + \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java b/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java new file mode 100644 index 0000000000..8e3a8d749b --- /dev/null +++ b/jackson/src/test/java/org/baeldung/gson/GenericSourceClass.java @@ -0,0 +1,10 @@ +package org.baeldung.gson; + +public class GenericSourceClass { + int intField; + + public GenericSourceClass(final int i) { + intField = i; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java b/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java new file mode 100644 index 0000000000..dabc861ce1 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/gson/GsonDeserializationUnitTest.java @@ -0,0 +1,32 @@ +package org.baeldung.gson; + +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationUnitTest { + + private Gson gson; + + @Before + public final void before() { + gson = new Gson(); + } + + // tests + + @Test + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final java.lang.reflect.Type genericSourceClassType = new TypeToken() { + }.getType(); + final GenericSourceClass sourceObject = new GenericSourceClass(1); + final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType); + + final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType); + + System.out.println(targetObject); + } + +} diff --git a/mockito/pom.xml b/mockito/pom.xml index 8829e2e2d1..3ad055690a 100644 --- a/mockito/pom.xml +++ b/mockito/pom.xml @@ -90,11 +90,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -122,7 +122,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/sandbox/pom.xml b/sandbox/pom.xml index 4f7fb39e2f..57734d7393 100644 --- a/sandbox/pom.xml +++ b/sandbox/pom.xml @@ -134,7 +134,7 @@ 4.0.3.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 9932b68278..07c842aaad 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -193,13 +193,13 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 1.2 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -227,7 +227,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-exceptions/pom.xml b/spring-exceptions/pom.xml index 0025469340..1c0c06062f 100644 --- a/spring-exceptions/pom.xml +++ b/spring-exceptions/pom.xml @@ -205,13 +205,13 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 1.2 4.3.6.Final - 5.1.31 + 5.1.32 7.0.42 @@ -241,7 +241,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-hibernate3/pom.xml b/spring-hibernate3/pom.xml index cdcc2277f9..23ab319e24 100644 --- a/spring-hibernate3/pom.xml +++ b/spring-hibernate3/pom.xml @@ -164,12 +164,12 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 3.6.10.Final - 5.1.31 + 5.1.32 7.0.47 @@ -197,7 +197,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-hibernate4/pom.xml b/spring-hibernate4/pom.xml index c16fff764a..47c5c13d7e 100644 --- a/spring-hibernate4/pom.xml +++ b/spring-hibernate4/pom.xml @@ -171,12 +171,12 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 3.18.1-GA 4.3.6.Final - 5.1.31 + 5.1.32 7.0.42 @@ -204,7 +204,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index ddffa5101e..b2d3525dcc 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -166,11 +166,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -197,7 +197,7 @@ 3.1 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 7e966a699e..d9c27481d4 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -142,11 +142,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -174,7 +174,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-no-xml/pom.xml b/spring-mvc-no-xml/pom.xml index f2ab27e251..264879437c 100644 --- a/spring-mvc-no-xml/pom.xml +++ b/spring-mvc-no-xml/pom.xml @@ -166,7 +166,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index c957869c12..df23cf654c 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -168,7 +168,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp index e0ae61dd7b..588678cdcf 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -8,7 +8,7 @@ Welcome, Enter The Employee Details - + Name diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index a5044a675b..f59a8128da 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -203,11 +203,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 @@ -238,7 +238,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-basic-auth/pom.xml b/spring-security-basic-auth/pom.xml index fea2ae9ce5..6ce841c954 100644 --- a/spring-security-basic-auth/pom.xml +++ b/spring-security-basic-auth/pom.xml @@ -227,11 +227,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -259,7 +259,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-login-error-handling/pom.xml b/spring-security-login-error-handling/pom.xml index f3dd7e2fb9..a98aceed2e 100644 --- a/spring-security-login-error-handling/pom.xml +++ b/spring-security-login-error-handling/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.1.4.RELEASE + 1.1.5.RELEASE @@ -125,7 +125,7 @@ 1.7 3.1.1.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 1.6.10 1.7.6 @@ -135,7 +135,7 @@ 1 - 1.4.1.RELEASE + 1.4.5.RELEASE 17.0 diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java index ceec68a9f3..7fcfce196a 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/model/Role.java @@ -18,9 +18,12 @@ public class Role { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "user_id") private User user; + + @Column(name = "role") private Integer role; diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java index fbd968a834..bc2bd020c9 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserDto.java @@ -20,36 +20,44 @@ public class UserDto { @NotNull @NotEmpty private String username; - - private Integer role; - + private Integer role; + public Integer getRole() { return role; } + public void setRole(Integer role) { this.role = role; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getFirstName() { return firstName; } + public void setFirstName(String firstName) { this.firstName = firstName; } + public String getLastName() { return lastName; } + public void setLastName(String lastName) { this.lastName = lastName; } + public String getPassword() { return password; } + public void setPassword(String password) { this.password = password; } @@ -59,11 +67,11 @@ public class UserDto { public void setMatchingPassword(String matchingPassword) { this.matchingPassword = matchingPassword; } + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("User [firstName=").append(firstName).append("]"). - append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]"); + builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]"); return builder.toString(); } } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java new file mode 100644 index 0000000000..45d453b934 --- /dev/null +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/persistence/service/UserValidator.java @@ -0,0 +1,33 @@ +package org.baeldung.persistence.service; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; + +public class UserValidator implements Validator { + private Pattern pattern; + private Matcher matcher; + private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; + + @Override + public boolean supports(Class> clazz) { + return UserDto.class.isAssignableFrom(clazz); + } + + @Override + public void validate(Object obj, Errors errors) { + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "Firstname is required."); + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "message.lastName", "LastName is required."); + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "message.password", "LastName is required."); + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "message.username", "UserName is required."); + } + + public boolean validateEmail(String email) { + pattern = Pattern.compile(EMAIL_PATTERN); + matcher = pattern.matcher(email); + return matcher.matches(); + + } +} diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java index 41c27e2a9c..464a747683 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -22,33 +22,33 @@ import org.springframework.transaction.annotation.Transactional; public class MyUserDetailsService implements UserDetailsService { private static final Logger LOGGER = LoggerFactory.getLogger(UserDetailsService.class); - + private UserRepository userRepository; @Autowired public MyUserDetailsService(UserRepository repository) { this.userRepository = repository; } - + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { try { LOGGER.debug("Loading user by username: {}", username); User user = userRepository.findByUsername(username); LOGGER.debug("Found user: {}", user); if (user == null) { - //throw new UsernameNotFoundException("No user found with username: " + username); - boolean enabled = false; - return new org.springframework.security.core.userdetails.User(" ", " ", enabled, true, true, true, getAuthorities(new Integer(1))); + // throw new UsernameNotFoundException("No user found with username: " + username); + boolean enabled = false; + return new org.springframework.security.core.userdetails.User(" ", " ", enabled, true, true, true, getAuthorities(new Integer(1))); } boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; - return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRole().getRole())); + return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRole().getRole())); } catch (Exception e) { throw new RuntimeException(e); - } + } } public Collection extends GrantedAuthority> getAuthorities(Integer role) { @@ -60,7 +60,7 @@ public class MyUserDetailsService implements UserDetailsService { List roles = new ArrayList(); if (role.intValue() == 2) { - // roles.add("ROLE_USER"); + // roles.add("ROLE_USER"); roles.add("ROLE_ADMIN"); } else if (role.intValue() == 1) { @@ -69,7 +69,7 @@ public class MyUserDetailsService implements UserDetailsService { return roles; } - + public static List getGrantedAuthorities(List roles) { List authorities = new ArrayList(); for (String role : roles) { diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java index 6047130a69..73daab8f86 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/AppConfig.java @@ -12,7 +12,7 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Import({ MvcConfig.class, PersistenceJPAConfig.class, SecSecurityConfig.class }) @PropertySource("classpath:application.properties") public class AppConfig { - + @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java index 1aa0a4e27f..4dc7f039fe 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/MvcConfig.java @@ -21,11 +21,8 @@ import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; - @Configuration -@ComponentScan(basePackages = { - "org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao" -}) +@ComponentScan(basePackages = { "org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao" }) @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @@ -46,9 +43,9 @@ public class MvcConfig extends WebMvcConfigurerAdapter { registry.addViewController("/console.html"); registry.addViewController("/admin.html"); registry.addViewController("/registration.html"); - registry.addViewController("/successRegister.html"); + registry.addViewController("/successRegister.html"); } - + @Bean public ViewResolver viewResolver() { final InternalResourceViewResolver bean = new InternalResourceViewResolver(); @@ -57,11 +54,10 @@ public class MvcConfig extends WebMvcConfigurerAdapter { bean.setSuffix(".jsp"); return bean; } - + @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**") - .addResourceLocations("/","/resources/"); + registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/"); } @Override @@ -93,11 +89,11 @@ public class MvcConfig extends WebMvcConfigurerAdapter { UsernameValidator userNameValidator = new UsernameValidator(); return userNameValidator; } + @Bean public PasswordMatchesValidator passwordMatchesValidator() { PasswordMatchesValidator passwordMatchesValidator = new PasswordMatchesValidator(); return passwordMatchesValidator; } - - + } \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java index e4c3d4835f..0baac30ec1 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/PersistenceJPAConfig.java @@ -22,7 +22,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @ComponentScan({ "org.baeldung.persistence.model" }) @EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") public class PersistenceJPAConfig { - + @Autowired private Environment env; @@ -69,5 +69,5 @@ public class PersistenceJPAConfig { hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); return hibernateProperties; } - + } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java index 3e793a33f6..4da114c78b 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -10,5 +10,5 @@ public class SecSecurityConfig { public SecSecurityConfig() { super(); } - + } diff --git a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java index 443f6b0a91..e41cb26783 100644 --- a/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java +++ b/spring-security-login-error-handling/src/main/java/org/baeldung/web/controller/RegistrationController.java @@ -66,4 +66,5 @@ public class RegistrationController { } return registered; } + } diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp index 3474f523d9..923971cb08 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/admin.jsp @@ -1,6 +1,5 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> @@ -9,6 +8,10 @@ " rel="stylesheet"> +<<<<<<< HEAD +======= + +>>>>>>> FETCH_HEAD Hello Admin diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp index 87aeedc662..2243df768c 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/console.jsp @@ -1,7 +1,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="security" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%> + " rel="stylesheet"> @@ -18,4 +18,5 @@ ">Logout ">Administrator Page + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp index 058d67a4ba..79461bef1d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/home.jsp @@ -1,6 +1,7 @@ <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page session="true"%> + " rel="stylesheet"> Home @@ -8,4 +9,5 @@ Welcome back home! + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp index c6135a3372..9b6502c907 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/homepage.jsp @@ -1,6 +1,10 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<<<<<<< HEAD <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> +======= +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> +>>>>>>> FETCH_HEAD <%@ page session="true"%> @@ -23,4 +27,8 @@ ">Home ">Administrator Page +<<<<<<< HEAD +======= + +>>>>>>> FETCH_HEAD \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp index 3b03726733..59e9cdc22b 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/invalidSession.jsp @@ -3,11 +3,12 @@ " rel="stylesheet"> - Home +Home - - - + + + + diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp index a6a828515a..ecc8965f7d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/login.jsp @@ -1,6 +1,5 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> @@ -9,6 +8,7 @@ + " rel="stylesheet"> @@ -29,7 +29,6 @@ document.f.j_username.focus(); return false; } - if (document.f.j_password.value == "") { alert("${noPass}"); document.f.j_password.focus(); @@ -42,8 +41,7 @@ Login English | Spanish - + User: @@ -57,9 +55,11 @@ + Current Locale : ${pageContext.response.locale} ">Sign Up + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp index ed05de3458..d87e41fc6c 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/logout.jsp @@ -1,9 +1,9 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> + " rel="stylesheet"> @@ -11,15 +11,17 @@ + +Logged Out + + + - -Logged Out - - Login + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp index a4c40d670b..ae90159475 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/registration.jsp @@ -11,28 +11,29 @@ Registration + This is the registration page - + First Name: - + Last Name: - + Username (your e-mail address): - + Password: - + @@ -46,4 +47,5 @@ ">Back to Login + \ No newline at end of file diff --git a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp index d937128a59..323780263d 100644 --- a/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp +++ b/spring-security-login-error-handling/src/main/webapp/WEB-INF/view/successRegister.jsp @@ -1,22 +1,22 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> -<%@ taglib prefix="sec" - uri="http://www.springframework.org/security/tags"%> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ page session="true"%> + " rel="stylesheet"> Registration Success - + - - "> + + "> + \ No newline at end of file diff --git a/spring-security-mvc-custom/pom.xml b/spring-security-mvc-custom/pom.xml index 0dd81b1edc..b8e7f4b5a8 100644 --- a/spring-security-mvc-custom/pom.xml +++ b/spring-security-mvc-custom/pom.xml @@ -232,11 +232,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -264,7 +264,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-digest-auth/pom.xml b/spring-security-mvc-digest-auth/pom.xml index 8dc1748fb4..cf6a001b12 100644 --- a/spring-security-mvc-digest-auth/pom.xml +++ b/spring-security-mvc-digest-auth/pom.xml @@ -227,11 +227,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -259,7 +259,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-login/pom.xml b/spring-security-mvc-login/pom.xml index 65f14c0432..fa16d2c393 100644 --- a/spring-security-mvc-login/pom.xml +++ b/spring-security-mvc-login/pom.xml @@ -224,11 +224,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -256,7 +256,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-persisted-remember-me/pom.xml b/spring-security-mvc-persisted-remember-me/pom.xml index 8e0491d78b..caf00b6d98 100644 --- a/spring-security-mvc-persisted-remember-me/pom.xml +++ b/spring-security-mvc-persisted-remember-me/pom.xml @@ -261,11 +261,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -293,7 +293,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-mvc-session/pom.xml b/spring-security-mvc-session/pom.xml index 9285a243c4..95dfcf9fdf 100644 --- a/spring-security-mvc-session/pom.xml +++ b/spring-security-mvc-session/pom.xml @@ -232,11 +232,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -264,7 +264,7 @@ 2.4 2.17 2.6 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-basic-auth/pom.xml b/spring-security-rest-basic-auth/pom.xml index b44169f368..fc273049ba 100644 --- a/spring-security-rest-basic-auth/pom.xml +++ b/spring-security-rest-basic-auth/pom.xml @@ -288,11 +288,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 4.3.2 @@ -320,7 +320,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-custom/pom.xml b/spring-security-rest-custom/pom.xml index 2189396535..80a37c9080 100644 --- a/spring-security-rest-custom/pom.xml +++ b/spring-security-rest-custom/pom.xml @@ -252,11 +252,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -283,7 +283,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-digest-auth/pom.xml b/spring-security-rest-digest-auth/pom.xml index c9c6617645..6a8124879c 100644 --- a/spring-security-rest-digest-auth/pom.xml +++ b/spring-security-rest-digest-auth/pom.xml @@ -89,7 +89,7 @@ com.fasterxml.jackson.core jackson-databind - 2.2.2 + ${jackson.version} @@ -227,13 +227,13 @@ source - + org.apache.maven.plugins maven-war-plugin ${maven-war-plugin.version} - + org.apache.maven.plugins maven-surefire-plugin @@ -276,11 +276,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 4.3.2 @@ -290,6 +290,9 @@ 1.7.7 1.1.2 + + 2.4.2 + 5.1.2.Final @@ -308,7 +311,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java index d9d89db224..698ab4cb92 100644 --- a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java +++ b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryDigestAuth.java @@ -4,6 +4,7 @@ import java.net.URI; import org.apache.http.HttpHost; import org.apache.http.client.AuthCache; +import org.apache.http.client.HttpClient; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.BasicAuthCache; @@ -15,8 +16,8 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory { HttpHost host; - public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host) { - super(); + public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host, final HttpClient httpClient) { + super(httpClient); this.host = host; } diff --git a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java index 615be8f524..9a3b177500 100644 --- a/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java +++ b/spring-security-rest-digest-auth/src/main/java/org/baeldung/client/spring/ClientConfig.java @@ -1,9 +1,13 @@ package org.baeldung.client.spring; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; import org.apache.http.client.HttpClient; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -12,6 +16,8 @@ import org.springframework.web.client.RestTemplate; @Configuration public class ClientConfig { + private static final String DEFAULT_USER = "user1"; + private static final String DEFAULT_PASS = "user1Pass"; public ClientConfig() { super(); @@ -22,7 +28,9 @@ public class ClientConfig { @Bean public RestTemplate restTemplate() { final HttpHost host = new HttpHost("localhost", 8080, "http"); - final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build(); + + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client); final RestTemplate restTemplate = new RestTemplate(requestFactory); final int timeout = 5; @@ -43,9 +51,16 @@ public class ClientConfig { // httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000); // - note: timeout via the API - final HttpParams httpParams = httpClient.getParams(); - HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout - HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout + // final HttpParams httpParams = httpClient.getParams(); + // HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout + // HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout + } + + private final CredentialsProvider provider() { + final CredentialsProvider provider = new BasicCredentialsProvider(); + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS); + provider.setCredentials(AuthScope.ANY, credentials); + return provider; } } \ No newline at end of file diff --git a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java new file mode 100644 index 0000000000..245d5d0a41 --- /dev/null +++ b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientNoSpringLiveTest.java @@ -0,0 +1,64 @@ +package org.baeldung.client; + +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.baeldung.web.dto.Foo; +import org.junit.Test; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +public class ClientNoSpringLiveTest { + private static final String DEFAULT_USER = "user1"; + private static final String DEFAULT_PASS = "user1Pass"; + + // tests - no Spring + + @Test + public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() { + final HttpHost host = new HttpHost("localhost", 8080, "http"); + + final CredentialsProvider credentialsProvider = provider(); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build(); + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client); + final RestTemplate restTemplate = new RestTemplate(requestFactory); + + // credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); + + final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; + final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + + System.out.println(responseEntity.getStatusCode()); + } + + @Test + public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() { + final CredentialsProvider credentialsProvider = provider(); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build(); + final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client); + final RestTemplate restTemplate = new RestTemplate(requestFactory); + + // credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); + + final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; + final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + + System.out.println(responseEntity.getStatusCode()); + } + + // UTIL + + private final CredentialsProvider provider() { + final CredentialsProvider provider = new BasicCredentialsProvider(); + final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS); + provider.setCredentials(AuthScope.ANY, credentials); + return provider; + } + +} diff --git a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java similarity index 62% rename from spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java rename to spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java index 9ab6854f61..b40f9ef472 100644 --- a/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientLiveTest.java +++ b/spring-security-rest-digest-auth/src/test/java/org/baeldung/client/ClientWithSpringLiveTest.java @@ -1,8 +1,5 @@ package org.baeldung.client; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.impl.client.DefaultHttpClient; import org.baeldung.client.spring.ClientConfig; import org.baeldung.web.dto.Foo; import org.junit.Test; @@ -10,7 +7,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @@ -18,21 +14,18 @@ import org.springframework.web.client.RestTemplate; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class) -public class ClientLiveTest { +public class ClientWithSpringLiveTest { @Autowired private RestTemplate restTemplate; - // tests + // tests - no Spring @Test public final void whenSecuredRestApiIsConsumed_then200OK() { - final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory(); - final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient(); - httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); - final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"; final ResponseEntity responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class); + System.out.println(responseEntity.getStatusCode()); } diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index 1ffe7244a6..d76b90b4f2 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -372,11 +372,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.6.2.RELEASE @@ -408,7 +408,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9 diff --git a/spring-security-rest/pom.xml b/spring-security-rest/pom.xml index c23901bda6..b85fd3565a 100644 --- a/spring-security-rest/pom.xml +++ b/spring-security-rest/pom.xml @@ -239,11 +239,11 @@ 4.0.6.RELEASE - 3.2.4.RELEASE + 3.2.5.RELEASE 4.3.6.Final - 5.1.31 + 5.1.32 1.7.7 @@ -270,7 +270,7 @@ 3.1 2.4 2.17 - 1.4.8 + 1.4.9