springboot Basics [v3.4.0]

Project ref: a2-sboot-ms-social-media-app


2. Property, method param or Return type validation

Project ref: a3-sboot-ms-validation


3. API documentation using openAPI, swagger-ui

Project ref: a4-sboot-ms-springdoc-swagger-openapi


4. Content negotiation for Response parsing

Project ref: a5-sboot-ms-content-negotiation


5. Internationalization (i18n)

Project ref: a6-sboot-ms-content-i18n


6. Microservice API Versioning

Project ref: a7-sboot-ms-api-versioning


7. SpringBoot HATEOAS

Project ref: a8-sboot-ms-hateoas


8. Static content filtering

Project ref: a9-sboot-ms-static-filtering


9. Dynamic content filtering

Project ref: a9-sboot-ms-static-filtering


10. Exception / Error Handling (Custom Exceptions)

Project ref: a2-sboot-ms-social-media-app


a11-sboot-ms-hal-explorer [TODO]

		<!-- Spring boot HAL explorer -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-rest-hal-explorer</artifactId>
		</dependency>
		- http://localhost:8080/explorer
		- http://localhost:8080/explorer/index.html#

##a13-sboot-ms-h2-jpa [TODO]

##a13-sboot-ms-mysql-jpa [TODO]

		docker run --detach --env MYSQL_ROOT_PASSWORD=dummypassword --env MYSQL_USER=social-media-user --env MYSQL_PASSWORD=dummypassword --env MYSQL_DATABASE=social-media-database --name mysql --publish 3306:3306 mysql:8-oracle

		mysqlsh
		\connect social-media-user@localhost:3306
		\sql
		use social-media-database
		select * from user_details;
		select * from post;
		\quit
		<!-- Use this for Spring Boot 3.1 and higher -->
		<dependency>
		    <groupId>com.mysql</groupId>
		    <artifactId>mysql-connector-j</artifactId>
		</dependency> 
		 
		<!-- Use this if you are using Spring Boot 3.0 or lower
		    <dependency>
		        <groupId>mysql</groupId>
		        <artifactId>mysql-connector-java</artifactId>
		    </dependency> 
		-->
		#spring.datasource.url=jdbc:h2:mem:testdb
		spring.jpa.show-sql=true
		spring.datasource.url=jdbc:mysql://localhost:3306/social-media-database
		spring.datasource.username=social-media-user
		spring.datasource.password=dummypassword
		spring.jpa.hibernate.ddl-auto=update
		spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

##a13-sboot-ms-mysql-jpa-JDBC-template [TODO]

##a14-sboot-sc-basic-authentication [TODO]

		<!-- Spring Security -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

		spring.security.user.name=vivek
		spring.security.user.password=welcome
		import org.springframework.context.annotation.Bean;
		import org.springframework.context.annotation.Configuration;
		import org.springframework.security.config.Customizer;
		import org.springframework.security.config.annotation.web.builders.HttpSecurity;
		import org.springframework.security.web.SecurityFilterChain;
		
		@Configuration
		public class SpringSecurityConfiguration {
		
			@Bean
			SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
		
				/* 
				 * All requests must be authorized.
				 * 
				 * Else return HTTP 403, it doesn't prompt for user creds.
				 */
				httpSecurity.authorizeHttpRequests(
						authorizationManagerRequestMatcherRegistryCustomizer -> authorizationManagerRequestMatcherRegistryCustomizer
								.anyRequest().authenticated());
		
				/* 
				 * Prompt for authentication if request is not authorized.
				 * 
				 * Using default customizer
				 */
				httpSecurity.httpBasic(Customizer.withDefaults());
		
				/*
				 * Disabling CSRF as it may cause issue with HTTP methods - POST & PUT.
				 * 
				 * if enabled, Keep prompting for user credentials for post request.
				 */
				httpSecurity.csrf(csrf -> csrf.disable());
		
				return httpSecurity.build();
			}
		}