Merge branch 'master' into BAEL-7142-spring-ai
This commit is contained in:
commit
b99bff30d8
|
@ -7,14 +7,14 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
public class CustomWebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Autowired private RestAuthenticationEntryPoint authenticationEntryPoint;
|
@Autowired private RestAuthenticationEntryPoint authenticationEntryPoint;
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
||||||
.authorities("ROLE_USER");
|
.authorities("ROLE_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
.antMatchers("/securityNone")
|
.antMatchers("/securityNone")
|
||||||
|
@ -40,6 +40,8 @@ public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAda
|
||||||
.authenticationEntryPoint(authenticationEntryPoint);
|
.authenticationEntryPoint(authenticationEntryPoint);
|
||||||
|
|
||||||
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class);
|
||||||
|
|
||||||
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.baeldung.array.conversions;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
|
public class ByteToCharArrayUnitTest {
|
||||||
|
public static byte[] byteArray = {65, 66, 67, 68};
|
||||||
|
public static char[] expected = {'A', 'B', 'C', 'D'};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenByteArray_WhenUsingStandardCharsets_thenConvertToCharArray() {
|
||||||
|
char[] charArray = new String(byteArray, StandardCharsets.UTF_8).toCharArray();
|
||||||
|
assertArrayEquals(expected, charArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenByteArray_WhenUsingSUsingStreams_thenConvertToCharArray() throws IOException {
|
||||||
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);
|
||||||
|
InputStreamReader reader = new InputStreamReader(inputStream);
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
int data;
|
||||||
|
while ((data = reader.read()) != -1) {
|
||||||
|
char ch = (char) data;
|
||||||
|
outputStream.write(ch);
|
||||||
|
}
|
||||||
|
char[] charArray = outputStream.toString().toCharArray();
|
||||||
|
assertArrayEquals(expected, charArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenByteArray_WhenUsingCharBuffer_thenConvertToCharArray() {
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
|
||||||
|
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(byteBuffer);
|
||||||
|
char[] charArray = new char[charBuffer.remaining()];
|
||||||
|
charBuffer.get(charArray);
|
||||||
|
assertArrayEquals(expected, charArray);
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,6 +38,16 @@
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
<version>32.1.2-jre</version>
|
<version>32.1.2-jre</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>1.37</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>1.37</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.map;
|
package com.baeldung.map.incrementmapkey;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.baeldung.map.incrementmapkey;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
|
||||||
|
@State(Scope.Benchmark)
|
||||||
|
public class BenchmarkMapMethodsJMH {
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
@Fork(value = 1, warmups = 1)
|
||||||
|
public void benchMarkGuavaMap() {
|
||||||
|
IncrementMapValueWays im = new IncrementMapValueWays();
|
||||||
|
im.charFrequencyUsingAtomicMap(getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
@Fork(value = 1, warmups = 1)
|
||||||
|
public void benchContainsKeyMap() {
|
||||||
|
IncrementMapValueWays im = new IncrementMapValueWays();
|
||||||
|
im.charFrequencyUsingContainsKey(getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
@Fork(value = 1, warmups = 1)
|
||||||
|
public void benchMarkComputeMethod() {
|
||||||
|
IncrementMapValueWays im = new IncrementMapValueWays();
|
||||||
|
im.charFrequencyUsingCompute(getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
@Fork(value = 1, warmups = 1)
|
||||||
|
public void benchMarkMergeMethod() {
|
||||||
|
IncrementMapValueWays im = new IncrementMapValueWays();
|
||||||
|
im.charFrequencyUsingMerge(getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
org.openjdk.jmh.Main.main(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getString() {
|
||||||
|
return
|
||||||
|
"Once upon a time in a quaint village nestled between rolling hills and whispering forests, there lived a solitary storyteller named Elias. Elias was known for spinning tales that transported listeners to magical realms and awakened forgotten dreams.\n"
|
||||||
|
+ "\n"
|
||||||
|
+ "His favorite spot was beneath an ancient oak tree, its sprawling branches offering shade to those who sought refuge from the bustle of daily life. Villagers of all ages would gather around Elias, their faces illuminated by the warmth of his stories.\n"
|
||||||
|
+ "\n" + "One evening, as dusk painted the sky in hues of orange and purple, a curious young girl named Lily approached Elias. Her eyes sparkled with wonder as she asked for a tale unlike any other.\n" + "\n"
|
||||||
|
+ "Elias smiled, sensing her thirst for adventure, and began a story about a forgotten kingdom veiled by mist, guarded by mystical creatures and enchanted by ancient spells. With each word, the air grew thick with anticipation, and the listeners were transported into a world where magic danced on the edges of reality.\n"
|
||||||
|
+ "\n" + "As Elias weaved the story, Lily's imagination took flight. She envisioned herself as a brave warrior, wielding a shimmering sword against dark forces, her heart fueled by courage and kindness.\n" + "\n"
|
||||||
|
+ "The night wore on, but the spell of the tale held everyone captive. The villagers laughed, gasped, and held their breaths, journeying alongside the characters through trials and triumphs.\n" + "\n"
|
||||||
|
+ "As the final words lingered in the air, a sense of enchantment settled upon the listeners. They thanked Elias for the gift of his storytelling, each carrying a piece of the magical kingdom within their hearts.\n" + "\n"
|
||||||
|
+ "Lily, inspired by the story, vowed to cherish the spirit of adventure and kindness in her own life. With a newfound spark in her eyes, she bid Elias goodnight, already dreaming of the countless adventures awaiting her.\n" + "\n"
|
||||||
|
+ "Under the star-studded sky, Elias remained beneath the ancient oak, his heart aglow with the joy of sharing tales that ignited imagination and inspired dreams. And as the night embraced the village, whispers of the enchanted kingdom lingered in the breeze, promising endless possibilities to those who dared to believe.";
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.map;
|
package com.baeldung.map.incrementmapkey;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.map;
|
package com.baeldung.map.incrementmapkey;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
@ -9,6 +9,8 @@ import java.util.stream.Stream;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.map.incrementmapkey.IncrementMapValueWays;
|
||||||
|
|
||||||
public class IncrementMapValueUnitTest {
|
public class IncrementMapValueUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
|
@ -2,7 +2,7 @@ package com.baeldung.wait_synchronization;
|
||||||
|
|
||||||
public class ConditionChecker {
|
public class ConditionChecker {
|
||||||
|
|
||||||
private volatile Boolean jobIsDone;
|
private volatile boolean jobIsDone;
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
public void ensureCondition() {
|
public void ensureCondition() {
|
||||||
|
|
|
@ -159,7 +159,7 @@
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<source.version>1.8</source.version>
|
<source.version>1.8</source.version>
|
||||||
<target.version>1.8</target.version>
|
<target.version>1.8</target.version>
|
||||||
<ascii.version>0.3.2</ascii.version>
|
<ascii.version>0.3.2</ascii.version>
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<source.version>1.8</source.version>
|
<source.version>1.8</source.version>
|
||||||
<target.version>1.8</target.version>
|
<target.version>1.8</target.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -61,7 +61,7 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<wiremock.version>3.3.1</wiremock.version>
|
<wiremock.version>3.3.1</wiremock.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -134,7 +134,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-javadoc-plugin.version>3.5.0</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<hsqldb.version>2.7.1</hsqldb.version>
|
<hsqldb.version>2.7.1</hsqldb.version>
|
||||||
<!-- Mime Type Libraries -->
|
<!-- Mime Type Libraries -->
|
||||||
<tika.version>2.8.0</tika.version>
|
<tika.version>2.8.0</tika.version>
|
||||||
|
|
|
@ -274,7 +274,7 @@
|
||||||
<mockito.version>4.6.1</mockito.version>
|
<mockito.version>4.6.1</mockito.version>
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
|
||||||
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
|
||||||
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
|
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.baeldung.negate;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class NegateIntUnitTest {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(NegateIntUnitTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingUnaryMinusOperator_thenGetExpectedResult() {
|
||||||
|
int x = 42;
|
||||||
|
assertEquals(-42, -x);
|
||||||
|
|
||||||
|
int z = 0;
|
||||||
|
assertEquals(0, -z);
|
||||||
|
|
||||||
|
int n = -42;
|
||||||
|
assertEquals(42, -n);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingBitwiseComplementOperator_thenGetExpectedResult() {
|
||||||
|
int x = 42;
|
||||||
|
assertEquals(-42, ~x + 1);
|
||||||
|
|
||||||
|
int z = 0;
|
||||||
|
assertEquals(0, ~z + 1);
|
||||||
|
|
||||||
|
int n = -42;
|
||||||
|
assertEquals(42, ~n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIntMinValue_whenUsingUnaryMinusOperator_thenCannotGetExpectedResult() {
|
||||||
|
int min = Integer.MIN_VALUE;
|
||||||
|
LOG.info("The value of '-min' is: " + -min);
|
||||||
|
|
||||||
|
assertTrue((-min) < 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenIntMinValue_whenUsingBitwiseComplementOperator_thenCannotGetExpectedResult() {
|
||||||
|
int min = Integer.MIN_VALUE;
|
||||||
|
int result = ~min + 1;
|
||||||
|
LOG.info("The value of '~min + 1' is: " + result);
|
||||||
|
|
||||||
|
assertTrue(result < 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingUnaryMinusOperatorWithMinInt_thenGetExpectedResult() {
|
||||||
|
int x = 42;
|
||||||
|
assertEquals(-42, Math.negateExact(x));
|
||||||
|
|
||||||
|
int z = 0;
|
||||||
|
assertEquals(0, Math.negateExact(z));
|
||||||
|
|
||||||
|
int n = -42;
|
||||||
|
assertEquals(42, Math.negateExact(n));
|
||||||
|
|
||||||
|
int min = Integer.MIN_VALUE;
|
||||||
|
assertThrowsExactly(ArithmeticException.class, () -> Math.negateExact(min));
|
||||||
|
}
|
||||||
|
}
|
|
@ -177,7 +177,7 @@
|
||||||
<unix4j.version>0.4</unix4j.version>
|
<unix4j.version>0.4</unix4j.version>
|
||||||
<grep4j.version>1.8.7</grep4j.version>
|
<grep4j.version>1.8.7</grep4j.version>
|
||||||
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
<javamoney.moneta.version>1.1</javamoney.moneta.version>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<spring.core.version>4.3.20.RELEASE</spring.core.version>
|
<spring.core.version>4.3.20.RELEASE</spring.core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.baeldung.stringbuffer;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
import org.openjdk.jmh.runner.Runner;
|
||||||
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
import org.openjdk.jmh.runner.options.Options;
|
||||||
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.SingleShotTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
|
@Measurement(batchSize = 10000, iterations = 10)
|
||||||
|
@Warmup(batchSize = 1000, iterations = 10)
|
||||||
|
@State(Scope.Thread)
|
||||||
|
public class ComparePerformance {
|
||||||
|
|
||||||
|
String strInitial = "springframework";
|
||||||
|
String strFinal = "";
|
||||||
|
String replacement = "java-";
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public String benchmarkStringConcatenation() {
|
||||||
|
strFinal += strInitial;
|
||||||
|
return strFinal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public StringBuffer benchmarkStringBufferConcatenation() {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer(strFinal);
|
||||||
|
stringBuffer.append(strInitial);
|
||||||
|
return stringBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public String benchmarkStringReplacement() {
|
||||||
|
strFinal = strInitial.replaceFirst("spring", replacement);
|
||||||
|
return strFinal;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public StringBuffer benchmarkStringBufferReplacement() {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer(strInitial);
|
||||||
|
stringBuffer.replace(0,6, replacement);
|
||||||
|
return stringBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
Options options = new OptionsBuilder()
|
||||||
|
.include(ComparePerformance.class.getSimpleName()).threads(1)
|
||||||
|
.forks(1).shouldFailOnError(true)
|
||||||
|
.shouldDoGC(true)
|
||||||
|
.jvmArgs("-server").build();
|
||||||
|
new Runner(options).run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.stringbuffer;
|
||||||
|
|
||||||
|
public class HashCode {
|
||||||
|
|
||||||
|
public static long getHashCodeString(String string) {
|
||||||
|
return string.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long getHashCodeSBuffer(StringBuffer strBuff) {
|
||||||
|
return strBuff.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "Spring";
|
||||||
|
System.out.println("String HashCode pre concatenation :" + getHashCodeString(str));
|
||||||
|
str += "Framework";
|
||||||
|
System.out.println("String HashCode post concatenation :" + getHashCodeString(str));
|
||||||
|
|
||||||
|
StringBuffer sBuf = new StringBuffer("Spring");
|
||||||
|
System.out.println("StringBuffer HashCode pre concatenation :" + getHashCodeSBuffer(sBuf));
|
||||||
|
sBuf.append("Framework");
|
||||||
|
System.out.println("StringBuffer HashCode post concatenation :" + getHashCodeSBuffer(sBuf));
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,7 +17,7 @@ public class StringIteratorTest {
|
||||||
public void whenUseJavaForLoop_thenIterate() {
|
public void whenUseJavaForLoop_thenIterate() {
|
||||||
String input = "Hello, Baeldung!";
|
String input = "Hello, Baeldung!";
|
||||||
String expectedOutput = "Hello, Baeldung!";
|
String expectedOutput = "Hello, Baeldung!";
|
||||||
String result = StringIterator.javaForLoop(input);
|
String result = StringIterator.javaforLoop(input);
|
||||||
assertEquals(expectedOutput, result);
|
assertEquals(expectedOutput, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ public class StringIteratorTest {
|
||||||
public void whenUseForEachMethod_thenIterate() {
|
public void whenUseForEachMethod_thenIterate() {
|
||||||
String input = "Hello, Baeldung!";
|
String input = "Hello, Baeldung!";
|
||||||
String expectedOutput = "Hello, Baeldung!";
|
String expectedOutput = "Hello, Baeldung!";
|
||||||
String result = StringIterator.java8ForEach(input);
|
String result = StringIterator.java8forEach(input);
|
||||||
assertEquals(expectedOutput, result);
|
assertEquals(expectedOutput, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.stringbuffer;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
||||||
|
public class ComparePerformanceTest {
|
||||||
|
|
||||||
|
ComparePerformance cp = new ComparePerformance();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringConcatenated_thenResultAsExpected() {
|
||||||
|
assertThat(cp.benchmarkStringConcatenation()).isEqualTo("springframework");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringBufferConcatenated_thenResultAsExpected() {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer("springframework");
|
||||||
|
assertThat(cp.benchmarkStringBufferConcatenation()).isEqualToIgnoringCase(stringBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringReplaced_thenResultAsExpected() {
|
||||||
|
assertThat(cp.benchmarkStringReplacement()).isEqualTo("java-framework");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringBufferReplaced_thenResultAsExpected() {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer("java-framework");
|
||||||
|
assertThat(cp.benchmarkStringBufferReplacement()).isEqualToIgnoringCase(stringBuffer);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.stringbuffer;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class HashCodeTest {
|
||||||
|
|
||||||
|
String str = "Spring";
|
||||||
|
StringBuffer sBuf = new StringBuffer("Spring");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStringConcat_thenHashCodeChanges() {
|
||||||
|
HashCode hc = new HashCode();
|
||||||
|
|
||||||
|
long initialStringHashCode = hc.getHashCodeString(str);
|
||||||
|
long initialSBufHashCode = hc.getHashCodeSBuffer(sBuf);
|
||||||
|
|
||||||
|
str += "Framework";
|
||||||
|
sBuf.append("Framework");
|
||||||
|
|
||||||
|
assertThat(initialStringHashCode).isNotEqualTo(hc.getHashCodeString(str));
|
||||||
|
assertThat(initialSBufHashCode).isEqualTo(hc.getHashCodeSBuffer(sBuf));
|
||||||
|
}
|
||||||
|
}
|
|
@ -154,7 +154,7 @@
|
||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package com.baeldung.uuid;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Methods are called by reflection in the unit test
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class UUIDPositiveLongGenerator {
|
||||||
|
public long getLeastSignificantBits() {
|
||||||
|
return Math.abs(UUID.randomUUID().getLeastSignificantBits());
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getMostSignificantBits() {
|
||||||
|
return Math.abs(UUID.randomUUID().getMostSignificantBits());
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combineByteBuffer() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
||||||
|
bb.putLong(uuid.getMostSignificantBits());
|
||||||
|
bb.putLong(uuid.getLeastSignificantBits());
|
||||||
|
bb.rewind();
|
||||||
|
return Math.abs(bb.getLong());
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combineBitwise() {
|
||||||
|
UUID uniqueUUID = UUID.randomUUID();
|
||||||
|
long mostSignificantBits = uniqueUUID.getMostSignificantBits();
|
||||||
|
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
||||||
|
return Math.abs((mostSignificantBits << 32) | (leastSignificantBits & 0xFFFFFFFFL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combineDirect() {
|
||||||
|
UUID uniqueUUID = UUID.randomUUID();
|
||||||
|
long mostSignificantBits = uniqueUUID.getMostSignificantBits();
|
||||||
|
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
||||||
|
return Math.abs(mostSignificantBits ^ (leastSignificantBits >> 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combinePermutation() {
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
long mostSigBits = uuid.getMostSignificantBits();
|
||||||
|
long leastSigBits = uuid.getLeastSignificantBits();
|
||||||
|
byte[] uuidBytes = new byte[16];
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
uuidBytes[i] = (byte) (mostSigBits >>> (8 * (7 - i)));
|
||||||
|
uuidBytes[i + 8] = (byte) (leastSigBits >>> (8 * (7 - i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
long result = 0;
|
||||||
|
for (byte b : uuidBytes) {
|
||||||
|
result = (result << 8) | (b & 0xFF);
|
||||||
|
}
|
||||||
|
return Math.abs(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combineWithSecureRandom() {
|
||||||
|
UUID uniqueUUID = UUID.randomUUID();
|
||||||
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
|
long randomBits = secureRandom.nextLong();
|
||||||
|
|
||||||
|
long mostSignificantBits = uniqueUUID.getMostSignificantBits() ^ randomBits;
|
||||||
|
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
||||||
|
|
||||||
|
return Math.abs((mostSignificantBits << 32) | (leastSignificantBits & 0xFFFFFFFFL));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long combineWithNanoTime() {
|
||||||
|
UUID uniqueUUID = UUID.randomUUID();
|
||||||
|
long nanoTime = System.nanoTime();
|
||||||
|
|
||||||
|
long mostSignificantBits = uniqueUUID.getMostSignificantBits() ^ nanoTime;
|
||||||
|
long leastSignificantBits = uniqueUUID.getLeastSignificantBits();
|
||||||
|
|
||||||
|
return Math.abs((mostSignificantBits << 32) | (leastSignificantBits & 0xFFFFFFFFL));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.uuid;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class UUIDPositiveLongGeneratorUnitTest {
|
||||||
|
|
||||||
|
private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator();
|
||||||
|
|
||||||
|
private final Set<Long> uniqueValues = new HashSet<>();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenForeachMethods_thenRetryWhileNotUnique() throws Exception {
|
||||||
|
for (Method method : uuidLongGenerator.getClass().getDeclaredMethods()) {
|
||||||
|
long uniqueValue;
|
||||||
|
do uniqueValue = (long) method.invoke(uuidLongGenerator); while (!isUnique(uniqueValue));
|
||||||
|
assertThat(uniqueValue).isPositive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGivenLongValue_thenCheckUniqueness() {
|
||||||
|
long uniqueValue = generateUniqueLong();
|
||||||
|
assertThat(uniqueValue).isPositive();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long generateUniqueLong() {
|
||||||
|
long uniqueValue;
|
||||||
|
do uniqueValue = uuidLongGenerator.combineBitwise(); while (!isUnique(uniqueValue));
|
||||||
|
return uniqueValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isUnique(long value) {
|
||||||
|
// Implement uniqueness checking logic, for example, by checking in the database
|
||||||
|
return uniqueValues.add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,8 +2,7 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder>
|
<encoder>
|
||||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
</pattern>
|
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,17 @@
|
||||||
<version>${wire.mock.version}</version>
|
<version>${wire.mock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.xml.bind</groupId>
|
||||||
|
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||||
|
<version>4.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.xml.bind</groupId>
|
||||||
|
<artifactId>jaxb-impl</artifactId>
|
||||||
|
<version>4.0.0</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -85,7 +96,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>jaxb2-maven-plugin</artifactId>
|
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||||
<version>2.5.0</version>
|
<version>3.1.0</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>xjc</id>
|
<id>xjc</id>
|
||||||
|
@ -131,6 +142,7 @@
|
||||||
<feign.form.spring.version>3.8.0</feign.form.spring.version>
|
<feign.form.spring.version>3.8.0</feign.form.spring.version>
|
||||||
<spring.cloud.openfeign.version>3.1.2</spring.cloud.openfeign.version>
|
<spring.cloud.openfeign.version>3.1.2</spring.cloud.openfeign.version>
|
||||||
<wire.mock.version>2.33.2</wire.mock.version>
|
<wire.mock.version>2.33.2</wire.mock.version>
|
||||||
|
<jakarta.xml.bind.version>4.0.0</jakarta.xml.bind.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,46 +1,36 @@
|
||||||
package com.baeldung.configuration;
|
package com.baeldung.configuration;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class WebSecurityConfiguration {
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public InMemoryUserDetailsManager userDetailsService() {
|
||||||
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||||
|
|
||||||
auth.inMemoryAuthentication()
|
Set<UserDetails> users = new HashSet<>();
|
||||||
.withUser("admin").password(encoder.encode("admin")).roles("USER", "ADMIN")
|
users.add(User.withUsername("admin").password(encoder.encode("admin")).roles("USER", "ADMIN").build());
|
||||||
.and()
|
for(int i=1;i<=10;i++){
|
||||||
.withUser("user1").password(encoder.encode("password1")).roles("USER")
|
users.add(User.withUsername("user"+i).password(encoder.encode("password")+i).roles("USER").build());
|
||||||
.and()
|
}
|
||||||
.withUser("user2").password(encoder.encode("password2")).roles("USER")
|
|
||||||
.and()
|
return new InMemoryUserDetailsManager(users);
|
||||||
.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
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilter(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
http
|
http
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
|
@ -48,5 +38,7 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
.anyRequest().permitAll()
|
.anyRequest().permitAll()
|
||||||
.and()
|
.and()
|
||||||
.httpBasic();
|
.httpBasic();
|
||||||
|
|
||||||
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,7 +164,7 @@
|
||||||
<shade.plugin.version>3.2.4</shade.plugin.version>
|
<shade.plugin.version>3.2.4</shade.plugin.version>
|
||||||
<install.version>3.0.0-M1</install.version>
|
<install.version>3.0.0-M1</install.version>
|
||||||
<jar.plugin.version>3.2.0</jar.plugin.version>
|
<jar.plugin.version>3.2.0</jar.plugin.version>
|
||||||
<javadoc.plugin.version>3.2.0</javadoc.plugin.version>
|
<javadoc.plugin.version>3.6.2</javadoc.plugin.version>
|
||||||
<resources.plugin.version>3.1.0</resources.plugin.version>
|
<resources.plugin.version>3.1.0</resources.plugin.version>
|
||||||
<site.plugin.version>3.9.1</site.plugin.version>
|
<site.plugin.version>3.9.1</site.plugin.version>
|
||||||
<source.plugin.version>3.2.1</source.plugin.version>
|
<source.plugin.version>3.2.1</source.plugin.version>
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>persistence-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
|
@ -162,7 +162,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<datasource-proxy.version>1.6</datasource-proxy.version>
|
<datasource-proxy.version>1.6</datasource-proxy.version>
|
||||||
<hibernate-types.version>2.9.7</hibernate-types.version>
|
<hibernate-types.version>2.9.7</hibernate-types.version>
|
||||||
<hibernate.version>5.4.14.Final</hibernate.version>
|
<hibernate.version>5.6.15.Final</hibernate.version>
|
||||||
<javassist.version>3.27.0-GA</javassist.version>
|
<javassist.version>3.27.0-GA</javassist.version>
|
||||||
<jaxb.version>2.3.1</jaxb.version>
|
<jaxb.version>2.3.1</jaxb.version>
|
||||||
<log4jdbc.version>2.0.0</log4jdbc.version>
|
<log4jdbc.version>2.0.0</log4jdbc.version>
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
<module>apache-bookkeeper</module>
|
<module>apache-bookkeeper</module>
|
||||||
<module>apache-cayenne</module>
|
<module>apache-cayenne</module>
|
||||||
<module>apache-derby</module>
|
<module>apache-derby</module>
|
||||||
|
<module>atomikos</module>
|
||||||
<module>blaze-persistence</module>
|
<module>blaze-persistence</module>
|
||||||
<module>core-java-persistence</module>
|
<module>core-java-persistence</module>
|
||||||
<module>core-java-persistence-2</module>
|
<module>core-java-persistence-2</module>
|
||||||
|
|
|
@ -131,7 +131,7 @@
|
||||||
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||||
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
<hibernate.version>5.6.15.Final</hibernate.version>
|
||||||
<hibernatesearch.version>5.8.2.Final</hibernatesearch.version>
|
<hibernatesearch.version>5.8.2.Final</hibernatesearch.version>
|
||||||
<mysql-connector-java.version>8.0.7-dmr</mysql-connector-java.version>
|
<mysql-connector-java.version>8.0.7-dmr</mysql-connector-java.version>
|
||||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||||
|
|
10
pom.xml
10
pom.xml
|
@ -701,7 +701,7 @@
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
<module>apache-libraries-2</module>
|
<module>apache-libraries-2</module>
|
||||||
<module>apache-libraries</module>
|
<module>apache-libraries</module>
|
||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module><!-- apache-olingo wasn't updated to boot-3 because a workaround for jakarta namespace wasn't found JAVA-27818 -->
|
||||||
<module>apache-poi-2</module>
|
<module>apache-poi-2</module>
|
||||||
<module>apache-poi-3</module>
|
<module>apache-poi-3</module>
|
||||||
<module>apache-poi</module>
|
<module>apache-poi</module>
|
||||||
|
@ -710,7 +710,6 @@
|
||||||
<module>apache-velocity</module>
|
<module>apache-velocity</module>
|
||||||
<module>asciidoctor</module>
|
<module>asciidoctor</module>
|
||||||
<module>asm</module>
|
<module>asm</module>
|
||||||
<module>atomikos</module>
|
|
||||||
<module>atomix</module>
|
<module>atomix</module>
|
||||||
<module>aws-modules</module>
|
<module>aws-modules</module>
|
||||||
<module>azure</module>
|
<module>azure</module>
|
||||||
|
@ -727,7 +726,7 @@
|
||||||
<module>dozer</module>
|
<module>dozer</module>
|
||||||
<module>drools</module>
|
<module>drools</module>
|
||||||
<module>dubbo</module>
|
<module>dubbo</module>
|
||||||
<!-- <module>feign</module> --> <!-- JAVA-20337 -->
|
<module>feign</module>
|
||||||
<module>gcp-firebase</module>
|
<module>gcp-firebase</module>
|
||||||
<module>geotools</module>
|
<module>geotools</module>
|
||||||
<module>google-auto-project</module>
|
<module>google-auto-project</module>
|
||||||
|
@ -947,7 +946,7 @@
|
||||||
<module>apache-kafka</module>
|
<module>apache-kafka</module>
|
||||||
<module>apache-libraries-2</module>
|
<module>apache-libraries-2</module>
|
||||||
<module>apache-libraries</module>
|
<module>apache-libraries</module>
|
||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module><!-- apache-olingo wasn't updated to boot-3 because a workaround for jakarta namespace wasn't found JAVA-27818 -->
|
||||||
<module>apache-poi-2</module>
|
<module>apache-poi-2</module>
|
||||||
<module>apache-poi-3</module>
|
<module>apache-poi-3</module>
|
||||||
<module>apache-poi</module>
|
<module>apache-poi</module>
|
||||||
|
@ -956,7 +955,6 @@
|
||||||
<module>apache-velocity</module>
|
<module>apache-velocity</module>
|
||||||
<module>asciidoctor</module>
|
<module>asciidoctor</module>
|
||||||
<module>asm</module>
|
<module>asm</module>
|
||||||
<module>atomikos</module>
|
|
||||||
<module>atomix</module>
|
<module>atomix</module>
|
||||||
<module>aws-modules</module>
|
<module>aws-modules</module>
|
||||||
<module>azure</module>
|
<module>azure</module>
|
||||||
|
@ -973,7 +971,7 @@
|
||||||
<module>dozer</module>
|
<module>dozer</module>
|
||||||
<module>drools</module>
|
<module>drools</module>
|
||||||
<module>dubbo</module>
|
<module>dubbo</module>
|
||||||
<!-- <module>feign</module> --> <!-- JAVA-20337 -->
|
<module>feign</module>
|
||||||
<module>gcp-firebase</module>
|
<module>gcp-firebase</module>
|
||||||
<module>geotools</module>
|
<module>geotools</module>
|
||||||
<module>google-auto-project</module>
|
<module>google-auto-project</module>
|
||||||
|
|
|
@ -4,9 +4,11 @@ import io.jsonwebtoken.JwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.jjwtfun.service.SecretService;
|
import io.jsonwebtoken.jjwtfun.service.SecretService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.csrf.CsrfFilter;
|
import org.springframework.security.web.csrf.CsrfFilter;
|
||||||
import org.springframework.security.web.csrf.CsrfToken;
|
import org.springframework.security.web.csrf.CsrfToken;
|
||||||
import org.springframework.security.web.csrf.CsrfTokenRepository;
|
import org.springframework.security.web.csrf.CsrfTokenRepository;
|
||||||
|
@ -21,19 +23,19 @@ import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
public class WebSecurityConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
CsrfTokenRepository jwtCsrfTokenRepository;
|
private CsrfTokenRepository jwtCsrfTokenRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
SecretService secretService;
|
private SecretService secretService;
|
||||||
|
|
||||||
// ordered so we can use binary search below
|
// ordered so we can use binary search below
|
||||||
private String[] ignoreCsrfAntMatchers = { "/dynamic-builder-compress", "/dynamic-builder-general", "/dynamic-builder-specific", "/set-secrets" };
|
private final String[] ignoreCsrfAntMatchers = { "/dynamic-builder-compress", "/dynamic-builder-general", "/dynamic-builder-specific", "/set-secrets" };
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http.addFilterAfter(new JwtCsrfValidatorFilter(), CsrfFilter.class)
|
http.addFilterAfter(new JwtCsrfValidatorFilter(), CsrfFilter.class)
|
||||||
.csrf()
|
.csrf()
|
||||||
.csrfTokenRepository(jwtCsrfTokenRepository)
|
.csrfTokenRepository(jwtCsrfTokenRepository)
|
||||||
|
@ -42,6 +44,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
.antMatchers("/**")
|
.antMatchers("/**")
|
||||||
.permitAll();
|
.permitAll();
|
||||||
|
|
||||||
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class JwtCsrfValidatorFilter extends OncePerRequestFilter {
|
private class JwtCsrfValidatorFilter extends OncePerRequestFilter {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung.app</groupId>
|
<groupId>com.baeldung.app</groupId>
|
||||||
<artifactId>spring-boot-groovy</artifactId>
|
<artifactId>spring-boot-groovy</artifactId>
|
||||||
|
@ -10,9 +10,10 @@
|
||||||
<description>Spring Boot Todo Application with Groovy</description>
|
<description>Spring Boot Todo Application with Groovy</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.spring-boot-modules</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-boot-modules</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -25,7 +26,7 @@
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.codehaus.groovy</groupId>
|
<groupId>org.apache.groovy</groupId>
|
||||||
<artifactId>groovy</artifactId>
|
<artifactId>groovy</artifactId>
|
||||||
<version>${groovy.version}</version>
|
<version>${groovy.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
@ -39,6 +40,11 @@
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.rest-assured</groupId>
|
||||||
|
<artifactId>rest-assured</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -71,8 +77,8 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<start-class>com.baeldung.springwithgroovy.SpringBootGroovyApplication</start-class>
|
<start-class>com.baeldung.springwithgroovy.SpringBootGroovyApplication</start-class>
|
||||||
<groovy.version>3.0.13</groovy.version>
|
<groovy.version>4.0.11</groovy.version>
|
||||||
<gmavenplus-plugin.version>1.9.0</gmavenplus-plugin.version>
|
<gmavenplus-plugin.version>3.0.2</gmavenplus-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,11 +1,11 @@
|
||||||
package com.baeldung.springwithgroovy.entity
|
package com.baeldung.springwithgroovy.entity
|
||||||
|
|
||||||
import javax.persistence.Column
|
import jakarta.persistence.Column
|
||||||
import javax.persistence.Entity
|
import jakarta.persistence.Entity
|
||||||
import javax.persistence.GeneratedValue
|
import jakarta.persistence.GeneratedValue
|
||||||
import javax.persistence.GenerationType
|
import jakarta.persistence.GenerationType
|
||||||
import javax.persistence.Id
|
import jakarta.persistence.Id
|
||||||
import javax.persistence.Table
|
import jakarta.persistence.Table
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = 'todo')
|
@Table(name = 'todo')
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.caffeine;
|
package com.baeldung.caffeine;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Because the POM imports Spring Security, we need a simple security
|
* Because the POM imports Spring Security, we need a simple security
|
||||||
|
@ -11,14 +12,14 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
public class SecurityConfiguration {
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilter(HttpSecurity http) throws Exception {
|
||||||
http.csrf().disable();
|
http.csrf().disable();
|
||||||
|
|
||||||
http.authorizeRequests()
|
return http.authorizeRequests()
|
||||||
.antMatchers("/**")
|
.antMatchers("/**")
|
||||||
.permitAll();
|
.permitAll().and().build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,10 @@
|
||||||
<artifactId>commons-configuration</artifactId>
|
<artifactId>commons-configuration</artifactId>
|
||||||
<version>${commons-configuration.version}</version>
|
<version>${commons-configuration.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -61,6 +65,14 @@
|
||||||
<layout>JAR</layout>
|
<layout>JAR</layout>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>9</source>
|
||||||
|
<target>9</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.modifyrequest;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication(scanBasePackages = "com.baeldung.modifyrequest")
|
||||||
|
public class ModifyRequestApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ModifyRequestApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.baeldung.modifyrequest.aop;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpInputMessage;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Profile("aspectExample")
|
||||||
|
public class EscapeHtmlAspect implements RequestBodyAdvice {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(EscapeHtmlAspect.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
//Apply this to all Controllers
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||||
|
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
||||||
|
logger.info("beforeBodyRead called");
|
||||||
|
InputStream inputStream = inputMessage.getBody();
|
||||||
|
return new HttpInputMessage() {
|
||||||
|
@Override
|
||||||
|
public InputStream getBody() throws IOException {
|
||||||
|
return new ByteArrayInputStream(escapeHtml(inputStream).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpHeaders getHeaders() {
|
||||||
|
return inputMessage.getHeaders();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||||
|
Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
// Return the modified object after reading the body
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||||
|
Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
|
//return the original body
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String escapeHtml(InputStream inputStream) throws IOException {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try (inputStream) {
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
char[] charBuffer = new char[128];
|
||||||
|
int bytesRead = -1;
|
||||||
|
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||||
|
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String input = stringBuilder.toString();
|
||||||
|
// Escape HTML characters
|
||||||
|
return input.replaceAll("&", "&")
|
||||||
|
.replaceAll("<", "<")
|
||||||
|
.replaceAll(">", ">");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.modifyrequest.config;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.interceptor.EscapeHtmlRequestInterceptor;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("interceptorExample")
|
||||||
|
public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(WebMvcConfiguration.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
logger.info("addInterceptors() called");
|
||||||
|
registry.addInterceptor(new EscapeHtmlRequestInterceptor())
|
||||||
|
.addPathPatterns("/save");
|
||||||
|
|
||||||
|
WebMvcConfigurer.super.addInterceptors(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.modifyrequest.controller;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/")
|
||||||
|
public class UserController {
|
||||||
|
Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||||
|
|
||||||
|
@PostMapping(value = "save")
|
||||||
|
public ResponseEntity<String> saveUser(@RequestBody String user) {
|
||||||
|
logger.info("save user info into database");
|
||||||
|
ResponseEntity<String> responseEntity = new ResponseEntity<>(user, HttpStatus.CREATED);
|
||||||
|
return responseEntity;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.modifyrequest.filter;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.requestwrapper.EscapeHtmlRequestWrapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.*;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(1)
|
||||||
|
@Profile("filterExample")
|
||||||
|
public class EscapeHtmlFilter implements Filter {
|
||||||
|
Logger logger = LoggerFactory.getLogger(EscapeHtmlFilter.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
logger.info("Modify the request");
|
||||||
|
|
||||||
|
filterChain.doFilter(new EscapeHtmlRequestWrapper((HttpServletRequest) servletRequest), servletResponse);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.modifyrequest.interceptor;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.requestwrapper.EscapeHtmlRequestWrapper;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public class EscapeHtmlRequestInterceptor implements HandlerInterceptor {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(EscapeHtmlRequestInterceptor.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
EscapeHtmlRequestWrapper escapeHtmlRequestWrapper = new EscapeHtmlRequestWrapper(request);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.baeldung.modifyrequest.requestwrapper;
|
||||||
|
|
||||||
|
import javax.servlet.ReadListener;
|
||||||
|
import javax.servlet.ServletInputStream;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletRequestWrapper;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class EscapeHtmlRequestWrapper extends HttpServletRequestWrapper {
|
||||||
|
private String body = null;
|
||||||
|
public EscapeHtmlRequestWrapper(HttpServletRequest request) throws IOException {
|
||||||
|
super(request);
|
||||||
|
this.body = this.escapeHtml(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String escapeHtml(HttpServletRequest request) throws IOException {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try (InputStream inputStream = request.getInputStream()) {
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
char[] charBuffer = new char[128];
|
||||||
|
int bytesRead = -1;
|
||||||
|
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||||
|
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String input = stringBuilder.toString();
|
||||||
|
// Escape HTML characters
|
||||||
|
return input.replaceAll("&", "&")
|
||||||
|
.replaceAll("<", "<")
|
||||||
|
.replaceAll(">", ">")
|
||||||
|
.replaceAll("'", "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletInputStream getInputStream() {
|
||||||
|
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
|
||||||
|
ServletInputStream servletInputStream = new ServletInputStream() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() {
|
||||||
|
return byteArrayInputStream.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isFinished() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReady() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setReadListener(ReadListener listener) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return servletInputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BufferedReader getReader() throws IOException {
|
||||||
|
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
@startuml
|
||||||
|
'https://plantuml.com/sequence-diagram
|
||||||
|
skinparam sequenceMessageAlign direction
|
||||||
|
skinparam handwritten true
|
||||||
|
skinparam sequence {
|
||||||
|
ParticipantBackgroundColor beige
|
||||||
|
ParticipantPadding 50
|
||||||
|
}
|
||||||
|
|
||||||
|
autonumber
|
||||||
|
|
||||||
|
Browser -[#63b175]> Filter: HTTP Request
|
||||||
|
activate Browser
|
||||||
|
activate Filter
|
||||||
|
Filter -[#63b175]> Filter: doFilter()
|
||||||
|
Filter -[#63b175]> DispatcherServlet: HTTP Request
|
||||||
|
activate DispatcherServlet
|
||||||
|
|
||||||
|
|
||||||
|
DispatcherServlet -[#63b175]> Controller: HTTP Request
|
||||||
|
activate Controller
|
||||||
|
Controller --[#63b175]> DispatcherServlet: HTTP Response
|
||||||
|
deactivate Controller
|
||||||
|
|
||||||
|
DispatcherServlet --[#63b175]> Filter: HTTP Response
|
||||||
|
deactivate DispatcherServlet
|
||||||
|
|
||||||
|
Filter --[#63b175]> Browser: HTTP Response
|
||||||
|
deactivate Filter
|
||||||
|
deactivate Browser
|
||||||
|
@enduml
|
|
@ -0,0 +1,33 @@
|
||||||
|
@startuml
|
||||||
|
'https://plantuml.com/sequence-diagram
|
||||||
|
skinparam sequenceMessageAlign direction
|
||||||
|
skinparam handwritten true
|
||||||
|
skinparam sequence {
|
||||||
|
ParticipantBackgroundColor beige
|
||||||
|
ParticipantPadding 50
|
||||||
|
}
|
||||||
|
|
||||||
|
autonumber
|
||||||
|
|
||||||
|
Browser -[#63b175]> Filter: Http Request
|
||||||
|
activate Browser
|
||||||
|
activate Filter
|
||||||
|
Filter -[#63b175]> DispatcherServlet: Http Request
|
||||||
|
activate DispatcherServlet
|
||||||
|
|
||||||
|
DispatcherServlet -[#63b175]> Interceptor: Http Request
|
||||||
|
activate Interceptor
|
||||||
|
Interceptor -[#63b175]> Interceptor: preHandle()
|
||||||
|
Interceptor -[#63b175]> Controller: Http Request
|
||||||
|
activate Controller
|
||||||
|
Controller --[#63b175]> Interceptor: Http Response
|
||||||
|
deactivate Controller
|
||||||
|
Interceptor --[#63b175]> DispatcherServlet: Http Response
|
||||||
|
deactivate Interceptor
|
||||||
|
DispatcherServlet --[#63b175]> Filter: Http Response
|
||||||
|
deactivate DispatcherServlet
|
||||||
|
|
||||||
|
Filter --[#63b175]> Browser: Http Response
|
||||||
|
deactivate Filter
|
||||||
|
deactivate Browser
|
||||||
|
@enduml
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.modifyrequest;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.controller.UserController;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
@WebMvcTest(UserController.class)
|
||||||
|
@ActiveProfiles("aspectExample")
|
||||||
|
public class EscapeHtmlAspectIntegrationTest {
|
||||||
|
Logger logger = LoggerFactory.getLogger(EscapeHtmlAspectIntegrationTest.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
@Test
|
||||||
|
void givenAspect_whenEscapeHtmlAspect_thenEscapeHtml() throws Exception {
|
||||||
|
|
||||||
|
Map<String, String> requestBody = Map.of(
|
||||||
|
"name", "James Cameron",
|
||||||
|
"email", "<script>alert()</script>james@gmail.com"
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, String> expectedResponseBody = Map.of(
|
||||||
|
"name", "James Cameron",
|
||||||
|
"email", "<script>alert()</script>james@gmail.com"
|
||||||
|
);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(objectMapper.writeValueAsString(requestBody)))
|
||||||
|
.andExpect(MockMvcResultMatchers.status()
|
||||||
|
.isCreated())
|
||||||
|
.andExpect(MockMvcResultMatchers.content()
|
||||||
|
.json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.modifyrequest;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.controller.UserController;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
@WebMvcTest(UserController.class)
|
||||||
|
@ActiveProfiles("filterExample")
|
||||||
|
public class EscapeHtmlFilterIntegrationTest {
|
||||||
|
Logger logger = LoggerFactory.getLogger(EscapeHtmlFilterIntegrationTest.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
@Test
|
||||||
|
void givenFilter_whenEscapeHtmlFilter_thenEscapeHtml() throws Exception {
|
||||||
|
Map<String, String> requestBody = Map.of(
|
||||||
|
"name", "James Cameron",
|
||||||
|
"email", "<script>alert()</script>james@gmail.com"
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, String> expectedResponseBody = Map.of(
|
||||||
|
"name", "James Cameron",
|
||||||
|
"email", "<script>alert()</script>james@gmail.com"
|
||||||
|
);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(objectMapper.writeValueAsString(requestBody))).andExpect(MockMvcResultMatchers.status()
|
||||||
|
.isCreated()).andExpect(MockMvcResultMatchers.content()
|
||||||
|
.json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.baeldung.modifyrequest;
|
||||||
|
|
||||||
|
import com.baeldung.modifyrequest.controller.UserController;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
@WebMvcTest(UserController.class)
|
||||||
|
@ActiveProfiles("interceptorExample")
|
||||||
|
public class EscapeHtmlInterceptorIntegrationTest {
|
||||||
|
Logger logger = LoggerFactory.getLogger(EscapeHtmlInterceptorIntegrationTest.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInterceptor_whenEscapeHtmlInterceptor_thenEscapeHtml() throws Exception {
|
||||||
|
Map<String, String> requestBody = Map.of(
|
||||||
|
"name", "James Cameron",
|
||||||
|
"email", "<script>alert()</script>james@gmail.com"
|
||||||
|
);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(objectMapper.writeValueAsString(requestBody))).andExpect(MockMvcResultMatchers.status()
|
||||||
|
.is4xxClientError());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,16 @@
|
||||||
package com.baeldung.spring.boot.management.logging;
|
package com.baeldung.spring.boot.management.logging;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig {
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilter(HttpSecurity http) throws Exception {
|
||||||
http.csrf()
|
return http.csrf()
|
||||||
.ignoringAntMatchers("/actuator/**");
|
.ignoringAntMatchers("/actuator/**").and()
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package com.baeldung.cloud.openfeign.oauthfeign;
|
package com.baeldung.cloud.openfeign.oauthfeign;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class OAuth2WebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
public class OAuth2WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
.csrf()
|
.csrf()
|
||||||
.disable()
|
.disable()
|
||||||
.oauth2Client();
|
.oauth2Client();
|
||||||
|
|
||||||
http
|
http
|
||||||
.authorizeRequests().anyRequest().permitAll();
|
.authorizeRequests().anyRequest().permitAll();
|
||||||
|
|
||||||
|
return http.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,9 +55,9 @@
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>dev.aspectj</groupId>
|
||||||
<artifactId>aspectj-maven-plugin</artifactId>
|
<artifactId>aspectj-maven-plugin</artifactId>
|
||||||
<version>${aspectj-plugin.version}</version>
|
<version>${aspectj-maven-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<complianceLevel>${java.version}</complianceLevel>
|
<complianceLevel>${java.version}</complianceLevel>
|
||||||
<aspectLibraries>
|
<aspectLibraries>
|
||||||
|
@ -80,7 +80,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring-boot.version>3.1.2</spring-boot.version>
|
<spring-boot.version>3.1.2</spring-boot.version>
|
||||||
<aspectj-plugin.version>1.14.0</aspectj-plugin.version>
|
<aspectj-maven-plugin.version>1.13.1</aspectj-maven-plugin.version>
|
||||||
<log4j2.version>2.17.1</log4j2.version>
|
<log4j2.version>2.17.1</log4j2.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<relativePath>../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -35,6 +35,11 @@
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-orm</artifactId>
|
<artifactId>spring-orm</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.orm</groupId>
|
||||||
|
<artifactId>hibernate-core</artifactId>
|
||||||
|
<version>${hibernate-core.version}</version>
|
||||||
|
</dependency>
|
||||||
<!-- Testing -->
|
<!-- Testing -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
@ -58,7 +63,8 @@
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jinq.version>1.8.29</jinq.version>
|
<jinq.version>2.0.1</jinq.version>
|
||||||
|
<hibernate-core.version>6.4.0.Final</hibernate-core.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.spring.jinq.config;
|
package com.baeldung.spring.jinq.config;
|
||||||
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
import jakarta.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
import org.jinq.jpa.JinqJPAStreamProvider;
|
import org.jinq.jpa.JinqJPAStreamProvider;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package com.baeldung.spring.jinq.entities;
|
package com.baeldung.spring.jinq.entities;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import javax.persistence.JoinColumn;
|
import jakarta.persistence.JoinColumn;
|
||||||
import javax.persistence.OneToOne;
|
import jakarta.persistence.ManyToOne;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Entity(name = "CAR")
|
@Entity(name = "CAR")
|
||||||
public class Car {
|
public class Car {
|
||||||
|
@Id
|
||||||
private String model;
|
private String model;
|
||||||
private String description;
|
private String description;
|
||||||
private int year;
|
private int year;
|
||||||
private String engine;
|
private String engine;
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "name")
|
||||||
private Manufacturer manufacturer;
|
private Manufacturer manufacturer;
|
||||||
|
|
||||||
@Id
|
|
||||||
public String getModel() {
|
public String getModel() {
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
@ -46,8 +50,6 @@ public class Car {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@OneToOne
|
|
||||||
@JoinColumn(name = "name")
|
|
||||||
public Manufacturer getManufacturer() {
|
public Manufacturer getManufacturer() {
|
||||||
return manufacturer;
|
return manufacturer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,19 @@ package com.baeldung.spring.jinq.entities;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
import javax.persistence.OneToMany;
|
import jakarta.persistence.OneToMany;
|
||||||
|
|
||||||
@Entity(name = "MANUFACTURER")
|
@Entity(name = "MANUFACTURER")
|
||||||
public class Manufacturer {
|
public class Manufacturer {
|
||||||
|
|
||||||
|
@Id
|
||||||
private String name;
|
private String name;
|
||||||
private String city;
|
private String city;
|
||||||
|
@OneToMany(mappedBy = "model")
|
||||||
private List<Car> cars;
|
private List<Car> cars;
|
||||||
|
|
||||||
@Id
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -30,7 +31,6 @@ public class Manufacturer {
|
||||||
this.city = city;
|
this.city = city;
|
||||||
}
|
}
|
||||||
|
|
||||||
@OneToMany(mappedBy = "model")
|
|
||||||
public List<Car> getCars() {
|
public List<Car> getCars() {
|
||||||
return cars;
|
return cars;
|
||||||
}
|
}
|
||||||
|
@ -38,5 +38,4 @@ public class Manufacturer {
|
||||||
public void setCars(List<Car> cars) {
|
public void setCars(List<Car> cars) {
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.baeldung.spring.jinq.repositories;
|
package com.baeldung.spring.jinq.repositories;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
|
||||||
import org.jinq.jpa.JPAJinqStream;
|
import org.jinq.jpa.JPAJinqStream;
|
||||||
import org.jinq.jpa.JinqJPAStreamProvider;
|
import org.jinq.jpa.JinqJPAStreamProvider;
|
||||||
|
|
|
@ -4,7 +4,6 @@ import com.baeldung.spring.jinq.JinqApplication;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import com.baeldung.spring.jinq.JinqApplication;
|
|
||||||
|
|
||||||
@SpringBootTest(classes = JinqApplication.class)
|
@SpringBootTest(classes = JinqApplication.class)
|
||||||
public class SpringContextTest {
|
public class SpringContextTest {
|
||||||
|
|
|
@ -276,7 +276,7 @@
|
||||||
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
|
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
|
||||||
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
|
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
|
||||||
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
|
||||||
<maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
|
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
|
||||||
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
|
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -196,7 +196,7 @@
|
||||||
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
|
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
|
||||||
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
|
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
|
||||||
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
|
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
|
||||||
<maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.6.2</maven-javadoc-plugin.version>
|
||||||
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
|
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -2,42 +2,37 @@ package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
||||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
||||||
public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
|
public class WebMVCSecurity {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Override
|
public InMemoryUserDetailsManager userDetailsService() {
|
||||||
public AuthenticationManager authenticationManagerBean() throws Exception {
|
UserDetails user = User.withUsername("user1")
|
||||||
return super.authenticationManagerBean();
|
.password("{noop}user1Pass")
|
||||||
|
.authorities("USER")
|
||||||
|
.build();
|
||||||
|
return new InMemoryUserDetailsManager(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WebMVCSecurity() {
|
@Bean
|
||||||
super();
|
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||||
|
return (web) -> web.ignoring().antMatchers("/resources/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Bean
|
||||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception {
|
||||||
auth.inMemoryAuthentication().withUser("user1").password("{noop}user1Pass").authorities("ROLE_USER");
|
return http.authorizeRequests().anyRequest().authenticated().and().httpBasic().and().build();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configure(final WebSecurity web) throws Exception {
|
|
||||||
web.ignoring().antMatchers("/resources/**");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void configure(final HttpSecurity http) throws Exception {
|
|
||||||
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ public class Xml2CsvExample {
|
||||||
protected static void convertXml2CsvXslt(String xslPath, String xmlPath, String csvPath) throws IOException, TransformerException {
|
protected static void convertXml2CsvXslt(String xslPath, String xmlPath, String csvPath) throws IOException, TransformerException {
|
||||||
StreamSource styleSource = new StreamSource(new File(xslPath));
|
StreamSource styleSource = new StreamSource(new File(xslPath));
|
||||||
Transformer transformer = TransformerFactory.newInstance()
|
Transformer transformer = TransformerFactory.newInstance()
|
||||||
.newTransformer(styleSource);
|
.newTransformer(styleSource);
|
||||||
Source source = new StreamSource(new File(xmlPath));
|
Source source = new StreamSource(new File(xmlPath));
|
||||||
Result outputTarget = new StreamResult(new File(csvPath));
|
Result outputTarget = new StreamResult(new File(csvPath));
|
||||||
transformer.transform(source, outputTarget);
|
transformer.transform(source, outputTarget);
|
||||||
|
@ -68,26 +68,26 @@ public class Xml2CsvExample {
|
||||||
if ("Bookstore".equals(currentElement)) {
|
if ("Bookstore".equals(currentElement)) {
|
||||||
bookstoreInfo.setLength(0);
|
bookstoreInfo.setLength(0);
|
||||||
bookstoreInfo.append(reader.getAttributeValue(null, "id"))
|
bookstoreInfo.append(reader.getAttributeValue(null, "id"))
|
||||||
.append(",");
|
.append(",");
|
||||||
}
|
}
|
||||||
if ("Book".equals(currentElement)) {
|
if ("Book".equals(currentElement)) {
|
||||||
csvRow.append(bookstoreInfo)
|
csvRow.append(bookstoreInfo)
|
||||||
.append(reader.getAttributeValue(null, "id"))
|
.append(reader.getAttributeValue(null, "id"))
|
||||||
.append(",")
|
.append(",")
|
||||||
.append(reader.getAttributeValue(null, "category"))
|
.append(reader.getAttributeValue(null, "category"))
|
||||||
.append(",");
|
.append(",");
|
||||||
}
|
}
|
||||||
if ("Author".equals(currentElement)) {
|
if ("Author".equals(currentElement)) {
|
||||||
csvRow.append(reader.getAttributeValue(null, "id"))
|
csvRow.append(reader.getAttributeValue(null, "id"))
|
||||||
.append(",");
|
.append(",");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case XMLStreamConstants.CHARACTERS:
|
case XMLStreamConstants.CHARACTERS:
|
||||||
if (!reader.isWhiteSpace()) {
|
if (!reader.isWhiteSpace()) {
|
||||||
csvRow.append(reader.getText()
|
csvRow.append(reader.getText()
|
||||||
.trim())
|
.trim())
|
||||||
.append(",");
|
.append(",");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -91,7 +91,7 @@ public class Xml2CsvExampleUnitTest {
|
||||||
Xml2CsvExample.convertXml2CsvXslt(STYLE_XSL, DATA_XML, TEMP_OUTPUT_CSV);
|
Xml2CsvExample.convertXml2CsvXslt(STYLE_XSL, DATA_XML, TEMP_OUTPUT_CSV);
|
||||||
|
|
||||||
File csvFile = new File(TEMP_OUTPUT_CSV);
|
File csvFile = new File(TEMP_OUTPUT_CSV);
|
||||||
try(BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
|
||||||
String line;
|
String line;
|
||||||
boolean isFirstLine = true;
|
boolean isFirstLine = true;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
@ -113,7 +113,7 @@ public class Xml2CsvExampleUnitTest {
|
||||||
Xml2CsvExample.convertXml2CsvStax(DATA_XML, TEMP_OUTPUT_CSV);
|
Xml2CsvExample.convertXml2CsvStax(DATA_XML, TEMP_OUTPUT_CSV);
|
||||||
|
|
||||||
File csvFile = new File(TEMP_OUTPUT_CSV);
|
File csvFile = new File(TEMP_OUTPUT_CSV);
|
||||||
try(BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
|
||||||
String line;
|
String line;
|
||||||
boolean isFirstLine = true;
|
boolean isFirstLine = true;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
|
|
Loading…
Reference in New Issue