Merge remote-tracking branch 'origin/PR-7005' into PR-7005

This commit is contained in:
parthiv39731 2023-10-11 22:44:01 +05:30
commit dd509bfa62
13 changed files with 165 additions and 31 deletions

View File

@ -76,6 +76,11 @@
<artifactId>jersey-apache-connector</artifactId> <artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version> <version>${jersey.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -102,4 +107,4 @@
<jersey.version>3.1.1</jersey.version> <jersey.version>3.1.1</jersey.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,61 @@
package com.baeldung.jersey.server.form;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataParam;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Path("form")
public class FormExampleResource
{
@GET
@Path("/example1")
@Produces({MediaType.TEXT_HTML})
public InputStream getExample1() throws Exception
{
File f = new File("src/main/resources/html/example1.html");
return new FileInputStream(f);
}
@GET
@Path("/example2")
@Produces({MediaType.TEXT_HTML})
public InputStream getExample2() throws Exception
{
File f = new File("src/main/resources/html/example2.html");
return new FileInputStream(f);
}
@POST
@Path("/example1")
public String example1(@FormParam("first_name") String firstName,
@FormParam("last_name") String lastName,
@FormParam("age") String age)
{
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age;
}
@POST
@Path("/example2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String example2(@FormDataParam("first_name") String firstName,
@FormDataParam("last_name") String lastName,
@FormDataParam("age") String age,
@FormDataParam("photo") InputStream photo)
throws Exception
{
int len;
int size = 1024;
byte[] buf;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = photo.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
return "Got: First = " + firstName + ", Last = " + lastName + ", Age = " + age + ", Photo (# of bytes) = " + buf.length;
}
}

View File

@ -0,0 +1,16 @@
<html>
<head>
<title>Example 1 using @FormParam</title>
</head>
<body>
<form method="post" action="/form/example1">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text">
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text">
<label for="age">Age</label>
<input id="age" name="age" type="text">
<input type="submit">
</form>
</body>
</html>

View File

@ -0,0 +1,18 @@
<html>
<head>
<title>Example 2 using @FormDataParam</title>
</head>
<body>
<form method="post" action="/form/example2" enctype="multipart/form-data">
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text">
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text">
<label for="age">Age</label>
<input id="age" name="age" type="text">
<label for="photo">Profile Photo</label>
<input id="photo" name="photo" type="file">
<input type="submit">
</form>
</body>
</html>

View File

@ -1005,6 +1005,7 @@
<module>spring-cloud-modules/spring-cloud-azure</module> <module>spring-cloud-modules/spring-cloud-azure</module>
<module>spring-cloud-modules/spring-cloud-circuit-breaker</module> <module>spring-cloud-modules/spring-cloud-circuit-breaker</module>
<module>spring-cloud-modules/spring-cloud-contract</module> <module>spring-cloud-modules/spring-cloud-contract</module>
<module>spring-cloud-modules/spring-cloud-data-flow</module>
<module>spring-cloud-modules/spring-cloud-netflix-feign</module> <module>spring-cloud-modules/spring-cloud-netflix-feign</module>
<module>spring-cloud-modules/spring-cloud-stream-starters</module> <module>spring-cloud-modules/spring-cloud-stream-starters</module>
<module>spring-cloud-modules/spring-cloud-zuul-eureka-integration</module> <module>spring-cloud-modules/spring-cloud-zuul-eureka-integration</module>

View File

@ -102,7 +102,7 @@
</build> </build>
<properties> <properties>
<start-class>com.baeldung.keycloak.SpringBoot</start-class> <start-class>com.baeldung.keycloak.SpringBootKeycloakApp</start-class>
<jaxb-runtime.version>4.0.0</jaxb-runtime.version> <jaxb-runtime.version>4.0.0</jaxb-runtime.version>
<wsdl4j.version>1.6.3</wsdl4j.version> <wsdl4j.version>1.6.3</wsdl4j.version>
<jaxb2-maven-plugin.version>2.5.0</jaxb2-maven-plugin.version> <jaxb2-maven-plugin.version>2.5.0</jaxb2-maven-plugin.version>

View File

@ -6,11 +6,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@SpringBootApplication @SpringBootApplication
public class SpringBootKeycloakApp {
public class SpringBoot {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBoot.class, args); SpringApplication.run(SpringBootKeycloakApp.class, args);
} }
@Bean @Bean

View File

@ -4,10 +4,9 @@ import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.baeldung.keycloak.SpringBoot;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { SpringBoot.class }) @SpringBootTest(classes = { SpringBootKeycloakApp.class })
public class KeycloakContextIntegrationTest { public class KeycloakContextIntegrationTest {
@Test @Test

View File

@ -3,10 +3,11 @@ package com.baeldung.swaggerkeycloak;
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.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
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.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.session.SessionRegistryImpl; import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
@ -24,16 +25,19 @@ public class GlobalSecurityConfig {
@Bean @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf()
.disable() http.csrf(AbstractHttpConfigurer::disable)
.authorizeRequests() .authorizeHttpRequests((requests) -> requests.requestMatchers(HttpMethod.OPTIONS)
.requestMatchers(HttpMethod.OPTIONS)
.permitAll() .permitAll()
.requestMatchers("/api/**") .requestMatchers("/api/**")
.authenticated() .authenticated()
.anyRequest() .anyRequest()
.permitAll(); .permitAll());
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
http.oauth2ResourceServer((oauth2) -> oauth2
.jwt(Customizer.withDefaults())
);
return http.build(); return http.build();
} }

View File

@ -41,28 +41,35 @@
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId> <artifactId>spring-cloud-contract-wiremock</artifactId>
<version>${spring-cloud.version}</version> <version>${spring-cloud-contract.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-stub-runner</artifactId> <artifactId>spring-cloud-contract-stub-runner</artifactId>
<version>${spring-cloud.version}</version> <version>${spring-cloud-contract.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId> <artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${spring-cloud.version}</version> <version>${spring-cloud-contract.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<properties> <properties>
<spring-cloud.version>4.0.3</spring-cloud.version> <spring-cloud.version>4.0.3</spring-cloud.version>
<spring-cloud-contract.version>4.0.4</spring-cloud-contract.version>
<spring-boot.version>2.1.4.RELEASE</spring-boot.version> <spring-boot.version>2.1.4.RELEASE</spring-boot.version>
<log4j2.version>2.17.1</log4j2.version> <log4j2.version>2.17.1</log4j2.version>
<groovy.version>2.5.6</groovy.version>
</properties> </properties>
</project> </project>

View File

@ -16,6 +16,15 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId> <artifactId>spring-cloud-contract-wiremock</artifactId>
@ -26,20 +35,13 @@
<artifactId>spring-cloud-contract-stub-runner</artifactId> <artifactId>spring-cloud-contract-stub-runner</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.baeldung.spring.cloud</groupId> <groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud-contract-producer</artifactId> <artifactId>spring-cloud-contract-producer</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.rest-assured</groupId> <groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId> <artifactId>rest-assured</artifactId>
@ -50,6 +52,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -16,19 +16,17 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId> <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.rest-assured</groupId> <groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId> <artifactId>rest-assured</artifactId>
@ -39,6 +37,18 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -46,7 +56,7 @@
<plugin> <plugin>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId> <artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version> <version>${spring-cloud-contract.version}</version>
<extensions>true</extensions> <extensions>true</extensions>
<configuration> <configuration>
<baseClassForTests>com.baeldung.spring.cloud.springcloudcontractproducer.BaseTestClass <baseClassForTests>com.baeldung.spring.cloud.springcloudcontractproducer.BaseTestClass

View File

@ -9,6 +9,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
@ -39,5 +40,10 @@ public class DataFlowServerApplicationIntegrationTest {
return factory; return factory;
} }
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
} }
} }