├── LearnFileManager ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── fm │ │ │ ├── FilaManagerController.java │ │ │ ├── FileManagerGuiController.java │ │ │ ├── FileStorageService.java │ │ │ └── OnlineFileManagerApplication.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── list_files.html │ │ └── uploader.html │ └── test │ └── java │ └── com │ └── genuinecoder │ └── fm │ └── OnlineFileManagerApplicationTests.java ├── LearnJWT ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── jwt │ │ │ ├── ContentController.java │ │ │ ├── JwtApplication.java │ │ │ ├── JwtAuthenticationFilter.java │ │ │ ├── RegistrationController.java │ │ │ ├── SecurityConfiguration.java │ │ │ ├── model │ │ │ ├── MyUser.java │ │ │ ├── MyUserDetailService.java │ │ │ └── MyUserRepository.java │ │ │ └── webtoken │ │ │ ├── JwtService.java │ │ │ └── LoginForm.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── genuinecoder │ └── jwt │ ├── JwtApplicationTests.java │ └── JwtSecretMakerTest.java ├── LearnJavaMailSender ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── java_mail │ │ │ ├── EmailController.java │ │ │ └── SpringBootEmailTutorialApplication.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── email-content.html │ └── test │ └── java │ └── com │ └── genuinecoder │ └── java_mail │ └── SpringBootEmailTutorialApplicationTests.java ├── LearnMicroservices ├── api-gateway │ ├── .gitignore │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── genuinecoder │ │ │ │ └── apigateway │ │ │ │ └── ApiGatewayApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── genuinecoder │ │ └── apigateway │ │ └── ApiGatewayApplicationTests.java ├── config-server │ ├── .gitignore │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── genuinecoder │ │ │ │ └── configserver │ │ │ │ └── ConfigServerApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── genuinecoder │ │ └── configserver │ │ └── ConfigServerApplicationTests.java ├── config │ ├── movie-catalog-service.properties │ ├── movie-streaming-service-dev.properties │ └── movie-streaming-service.properties ├── movie-catalog-service │ ├── .gitignore │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── genuinecoder │ │ │ │ └── moviecatalogservice │ │ │ │ ├── MovieCatalogServiceApplication.java │ │ │ │ ├── controller │ │ │ │ └── MovieInfoController.java │ │ │ │ └── model │ │ │ │ ├── MovieInfo.java │ │ │ │ └── MovieInfoRepository.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── genuinecoder │ │ └── moviecatalogservice │ │ └── MovieCatalogServiceApplicationTests.java ├── movie-streaming-service │ ├── .gitignore │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── genuinecoder │ │ │ │ └── moviestreamingservice │ │ │ │ ├── MovieStreamingServiceApplication.java │ │ │ │ └── controller │ │ │ │ ├── MovieCatalogService.java │ │ │ │ ├── MovieStreamController.java │ │ │ │ └── RestConfiguration.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── genuinecoder │ │ └── moviestreamingservice │ │ └── MovieStreamingServiceApplicationTests.java ├── service-registry │ ├── .gitignore │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── genuinecoder │ │ │ │ └── serviceregistry │ │ │ │ └── ServiceRegistryApplication.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── genuinecoder │ │ └── serviceregistry │ │ └── ServiceRegistryApplicationTests.java └── webapp.html ├── LearnReactiveProgramming ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── learnreactiveprogramming │ │ │ ├── BackpressureTutorial.java │ │ │ ├── DataController.java │ │ │ ├── DatabaseConfiguration.java │ │ │ ├── LearnReactiveProgrammingApplication.java │ │ │ ├── MainController.java │ │ │ ├── ReactiveTutorial.java │ │ │ └── model │ │ │ ├── Customer.java │ │ │ └── Order.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── home.html │ └── test │ └── java │ └── com │ └── genuinecoder │ └── learnreactiveprogramming │ └── LearnReactiveProgrammingApplicationTests.java ├── LearnSpringAI ├── .gitattributes ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── spring_ai │ │ │ ├── LearnSpringAiApplication.java │ │ │ ├── configuration │ │ │ └── ChatClientConfiguration.java │ │ │ └── controller │ │ │ └── ChatController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── genuinecoder │ └── spring_ai │ └── LearnSpringAiApplicationTests.java ├── LearnSpringBoot ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── log │ ├── myLog.log │ └── myLog.log.2024-03-03.0.gz ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── LearnSpringBoot │ │ │ ├── Calculator.java │ │ │ ├── LearnSpringBootApplication.java │ │ │ ├── MySimpleController.java │ │ │ ├── MySimpleDevController.java │ │ │ ├── StudentController.java │ │ │ └── model │ │ │ ├── Student.java │ │ │ └── StudentRepository.java │ └── resources │ │ ├── application-dev.properties │ │ ├── application-prod.properties │ │ ├── application.properties │ │ └── static │ │ ├── error │ │ └── 404.html │ │ ├── index.html │ │ └── test_me.html │ └── test │ └── java │ └── com │ └── genuinecoder │ └── LearnSpringBoot │ └── LearnSpringBootApplicationTests.java ├── LearnSpringOAuth ├── .gitignore ├── .gradle │ ├── 8.8 │ │ ├── checksums │ │ │ ├── checksums.lock │ │ │ ├── md5-checksums.bin │ │ │ └── sha1-checksums.bin │ │ ├── dependencies-accessors │ │ │ └── gc.properties │ │ ├── executionHistory │ │ │ ├── executionHistory.bin │ │ │ └── executionHistory.lock │ │ ├── fileChanges │ │ │ └── last-build.bin │ │ ├── fileHashes │ │ │ ├── fileHashes.bin │ │ │ ├── fileHashes.lock │ │ │ └── resourceHashesCache.bin │ │ └── gc.properties │ ├── buildOutputCleanup │ │ ├── buildOutputCleanup.lock │ │ ├── cache.properties │ │ └── outputFiles.bin │ ├── file-system.probe │ └── vcs-1 │ │ └── gc.properties ├── .idea │ ├── .gitignore │ ├── .name │ ├── compiler.xml │ ├── gradle.xml │ ├── jarRepositories.xml │ └── misc.xml ├── HELP.md ├── build.gradle ├── build │ ├── classes │ │ └── java │ │ │ └── main │ │ │ └── com │ │ │ └── genuine │ │ │ └── oauth │ │ │ ├── LearnSpringOAuthApplication.class │ │ │ ├── MainController.class │ │ │ ├── SpringConfig.class │ │ │ └── ViewController.class │ ├── resources │ │ └── main │ │ │ ├── application.properties │ │ │ └── templates │ │ │ ├── custom_login.html │ │ │ └── user-profile.html │ └── tmp │ │ └── compileJava │ │ ├── compileTransaction │ │ └── stash-dir │ │ │ ├── SpringConfig.class.uniqueId1 │ │ │ └── ViewController.class.uniqueId0 │ │ └── previous-compilation-data.bin ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuine │ │ │ └── oauth │ │ │ ├── LearnSpringOAuthApplication.java │ │ │ ├── MainController.java │ │ │ ├── SpringConfig.java │ │ │ └── ViewController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── custom_login.html │ │ └── user-profile.html │ └── test │ └── java │ └── com │ └── genuine │ └── oauth │ └── LearnSpringOAuthApplicationTests.java ├── LearnSpringSecurity ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── learnspringsecurity │ │ │ ├── AuthenticationSuccessHandler.java │ │ │ ├── ContentController.java │ │ │ ├── LearnSpringSecurityApplication.java │ │ │ ├── RegistrationController.java │ │ │ ├── SecurityConfiguration.java │ │ │ └── model │ │ │ ├── MyUser.java │ │ │ ├── MyUserDetailService.java │ │ │ └── MyUserRepository.java │ └── resources │ │ ├── application.properties │ │ ├── public │ │ └── error │ │ │ ├── 403.html │ │ │ └── 404.html │ │ └── templates │ │ ├── custom_login.html │ │ ├── home.html │ │ ├── home_admin.html │ │ └── home_user.html │ └── test │ └── java │ └── com │ └── genuinecoder │ └── learnspringsecurity │ └── LearnSpringSecurityApplicationTests.java ├── LearnUnitTest ├── .gitattributes ├── .gitignore ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── genuinecoder │ │ │ └── springboottest │ │ │ ├── SpringBootTestApplication.java │ │ │ ├── config │ │ │ ├── AuthenticationSuccessHandler.java │ │ │ └── SecurityConfiguration.java │ │ │ ├── controller │ │ │ ├── ContentController.java │ │ │ └── RegistrationController.java │ │ │ ├── model │ │ │ ├── MyUser.java │ │ │ ├── MyUserDetailService.java │ │ │ └── MyUserRepository.java │ │ │ └── service │ │ │ ├── DiscountService.java │ │ │ └── StringProcessorService.java │ └── resources │ │ ├── application.properties │ │ ├── error │ │ ├── 403.html │ │ └── 404.html │ │ └── templates │ │ ├── custom_login.html │ │ ├── home.html │ │ ├── home_admin.html │ │ └── home_user.html │ └── test │ ├── java │ └── com │ │ └── genuinecoder │ │ └── springboottest │ │ ├── controller │ │ ├── ContentControllerTest.java │ │ └── RegistrationControllerTest.java │ │ └── service │ │ ├── DiscountServiceTest.java │ │ └── StringProcessorServiceTest.java │ └── resources │ └── application.properties └── README.md /LearnFileManager/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnFileManager/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.3.2' 4 | id 'io.spring.dependency-management' version '1.1.6' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | toolchain { 12 | languageVersion = JavaLanguageVersion.of(21) 13 | } 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 22 | implementation 'org.springframework.boot:spring-boot-starter-web' 23 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 24 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 25 | } 26 | 27 | tasks.named('test') { 28 | useJUnitPlatform() 29 | } 30 | -------------------------------------------------------------------------------- /LearnFileManager/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnFileManager/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnFileManager/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnFileManager/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnFileManager/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'fm' 2 | -------------------------------------------------------------------------------- /LearnFileManager/src/main/java/com/genuinecoder/fm/FileManagerGuiController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.fm; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.nio.file.DirectoryStream; 10 | import java.nio.file.Files; 11 | import java.nio.file.Path; 12 | import java.util.stream.Collectors; 13 | import java.util.stream.StreamSupport; 14 | 15 | @Controller 16 | public class FileManagerGuiController { 17 | 18 | @GetMapping("/uploader") 19 | public String uploader() { 20 | return "uploader"; 21 | } 22 | 23 | @GetMapping("/list-files") 24 | public String listFiles(Model model) throws IOException { 25 | try (DirectoryStream stream = Files.newDirectoryStream(new File(FileStorageService.STORAGE_DIRECTORY).toPath())) { 26 | model.addAttribute("files", 27 | StreamSupport.stream(stream.spliterator(), false) 28 | .map(Path::getFileName) 29 | .map(Path::toString) 30 | .collect(Collectors.toList())); 31 | } 32 | return "list_files"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LearnFileManager/src/main/java/com/genuinecoder/fm/FileStorageService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.fm; 2 | 3 | import org.springframework.stereotype.Service; 4 | import org.springframework.web.multipart.MultipartFile; 5 | 6 | import java.io.File; 7 | import java.io.FileNotFoundException; 8 | import java.io.IOException; 9 | import java.nio.file.Files; 10 | import java.nio.file.StandardCopyOption; 11 | import java.util.Objects; 12 | 13 | @Service 14 | public class FileStorageService { 15 | 16 | public static final String STORAGE_DIRECTORY = "D:\\Storage"; 17 | 18 | public void saveFile(MultipartFile fileToSave) throws IOException { 19 | if (fileToSave == null) { 20 | throw new NullPointerException("fileToSave is null"); 21 | } 22 | var targetFile = new File(STORAGE_DIRECTORY + File.separator + fileToSave.getOriginalFilename()); 23 | if (!Objects.equals(targetFile.getParent(), STORAGE_DIRECTORY)) { 24 | throw new SecurityException("Unsupported filename!"); 25 | } 26 | Files.copy(fileToSave.getInputStream(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 27 | } 28 | 29 | public File getDownloadFile(String fileName) throws Exception { 30 | if (fileName == null) { 31 | throw new NullPointerException("fileName is null"); 32 | } 33 | var fileToDownload = new File(STORAGE_DIRECTORY + File.separator + fileName); 34 | if (!Objects.equals(fileToDownload.getParent(), STORAGE_DIRECTORY)) { 35 | throw new SecurityException("Unsupported filename!"); 36 | } 37 | if (!fileToDownload.exists()) { 38 | throw new FileNotFoundException("No file named: " + fileName); 39 | } 40 | return fileToDownload; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LearnFileManager/src/main/java/com/genuinecoder/fm/OnlineFileManagerApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.fm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class OnlineFileManagerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(OnlineFileManagerApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnFileManager/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=OnlineFileManager 2 | 3 | spring.servlet.multipart.max-file-size=2GB 4 | spring.servlet.multipart.max-request-size=2GB -------------------------------------------------------------------------------- /LearnFileManager/src/main/resources/templates/list_files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | File List 7 | 55 | 56 | 57 |
58 |

File List

59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 |
File NameAction
Download
73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /LearnFileManager/src/main/resources/templates/uploader.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | File Upload 7 | 53 | 54 | 55 |
56 |

Upload Your File

57 |
58 | 59 | 60 |
61 |
62 |
63 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /LearnFileManager/src/test/java/com/genuinecoder/fm/OnlineFileManagerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.fm; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class OnlineFileManagerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnJWT/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnJWT/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.3.0' 4 | id 'io.spring.dependency-management' version '1.1.5' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '17' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 20 | implementation 'org.springframework.boot:spring-boot-starter-security' 21 | implementation 'org.springframework.boot:spring-boot-starter-web' 22 | implementation 'com.mysql:mysql-connector-j' 23 | 24 | implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.12.5' 25 | implementation group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.12.5' 26 | implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.12.5' 27 | 28 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 29 | testImplementation 'org.springframework.security:spring-security-test' 30 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 31 | } 32 | 33 | tasks.named('test') { 34 | useJUnitPlatform() 35 | } 36 | -------------------------------------------------------------------------------- /LearnJWT/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnJWT/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnJWT/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnJWT/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnJWT/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'jwt' 2 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/ContentController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import com.genuinecoder.jwt.model.MyUserDetailService; 4 | import com.genuinecoder.jwt.webtoken.JwtService; 5 | import com.genuinecoder.jwt.webtoken.LoginForm; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.security.authentication.AuthenticationManager; 8 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 9 | import org.springframework.security.core.Authentication; 10 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | @RestController 17 | public class ContentController { 18 | 19 | @Autowired 20 | private AuthenticationManager authenticationManager; 21 | @Autowired 22 | private JwtService jwtService; 23 | @Autowired 24 | private MyUserDetailService myUserDetailService; 25 | 26 | @GetMapping("/home") 27 | public String handleWelcome() { 28 | return "Welcome to home!"; 29 | } 30 | 31 | @GetMapping("/admin/home") 32 | public String handleAdminHome() { 33 | return "Welcome to ADMIN home!"; 34 | } 35 | 36 | @GetMapping("/user/home") 37 | public String handleUserHome() { 38 | return "Welcome to USER home!"; 39 | } 40 | 41 | @PostMapping("/authenticate") 42 | public String authenticateAndGetToken(@RequestBody LoginForm loginForm) { 43 | Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken( 44 | loginForm.username(), loginForm.password() 45 | )); 46 | if (authentication.isAuthenticated()) { 47 | return jwtService.generateToken(myUserDetailService.loadUserByUsername(loginForm.username())); 48 | } else { 49 | throw new UsernameNotFoundException("Invalid credentials"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/JwtApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class JwtApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(JwtApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/JwtAuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import com.genuinecoder.jwt.model.MyUserDetailService; 4 | import com.genuinecoder.jwt.webtoken.JwtService; 5 | import jakarta.servlet.FilterChain; 6 | import jakarta.servlet.ServletException; 7 | import jakarta.servlet.http.HttpServletRequest; 8 | import jakarta.servlet.http.HttpServletResponse; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 12 | import org.springframework.security.core.context.SecurityContextHolder; 13 | import org.springframework.security.core.userdetails.UserDetails; 14 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; 15 | import org.springframework.web.filter.OncePerRequestFilter; 16 | 17 | import java.io.IOException; 18 | 19 | @Configuration 20 | public class JwtAuthenticationFilter extends OncePerRequestFilter { 21 | 22 | @Autowired 23 | private JwtService jwtService; 24 | @Autowired 25 | private MyUserDetailService myUserDetailService; 26 | 27 | @Override 28 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 29 | String authHeader = request.getHeader("Authorization"); 30 | if (authHeader == null || !authHeader.startsWith("Bearer ")) { 31 | filterChain.doFilter(request, response); 32 | return; 33 | } 34 | 35 | String jwt = authHeader.substring(7); 36 | String username = jwtService.extractUsername(jwt); 37 | if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) { 38 | UserDetails userDetails = myUserDetailService.loadUserByUsername(username); 39 | if (userDetails != null && jwtService.isTokenValid(jwt)) { 40 | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( 41 | username, 42 | userDetails.getPassword(), 43 | userDetails.getAuthorities() 44 | ); 45 | authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); 46 | SecurityContextHolder.getContext().setAuthentication(authenticationToken); 47 | } 48 | } 49 | filterChain.doFilter(request, response); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/RegistrationController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import com.genuinecoder.jwt.model.MyUser; 4 | import com.genuinecoder.jwt.model.MyUserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | public class RegistrationController { 13 | 14 | @Autowired 15 | private MyUserRepository myUserRepository; 16 | @Autowired 17 | private PasswordEncoder passwordEncoder; 18 | 19 | @PostMapping("/register/user") 20 | public MyUser createUser(@RequestBody MyUser user) { 21 | user.setPassword(passwordEncoder.encode(user.getPassword())); 22 | return myUserRepository.save(user); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/model/MyUser.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | 8 | @Entity 9 | public class MyUser { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.AUTO) 13 | private Long id; 14 | private String username; 15 | private String password; 16 | private String role; //Eg: ADMIN,USER 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getUsername() { 27 | return username; 28 | } 29 | 30 | public void setUsername(String username) { 31 | this.username = username; 32 | } 33 | 34 | public String getPassword() { 35 | return password; 36 | } 37 | 38 | public void setPassword(String password) { 39 | this.password = password; 40 | } 41 | 42 | public String getRole() { 43 | return role; 44 | } 45 | 46 | public void setRole(String role) { 47 | this.role = role; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/model/MyUserDetailService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt.model; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.User; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | import org.springframework.security.core.userdetails.UserDetailsService; 7 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.Optional; 11 | 12 | @Service 13 | public class MyUserDetailService implements UserDetailsService { 14 | 15 | @Autowired 16 | private MyUserRepository repository; 17 | 18 | @Override 19 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 20 | Optional user = repository.findByUsername(username); 21 | if (user.isPresent()) { 22 | var userObj = user.get(); 23 | return User.builder() 24 | .username(userObj.getUsername()) 25 | .password(userObj.getPassword()) 26 | .roles(getRoles(userObj)) 27 | .build(); 28 | } else { 29 | throw new UsernameNotFoundException(username); 30 | } 31 | } 32 | 33 | private String[] getRoles(MyUser user) { 34 | if (user.getRole() == null) { 35 | return new String[]{"USER"}; 36 | } 37 | return user.getRole().split(","); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/model/MyUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt.model; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import java.util.Optional; 6 | 7 | public interface MyUserRepository extends JpaRepository { 8 | 9 | Optional findByUsername(String username); 10 | } 11 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/webtoken/JwtService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt.webtoken; 2 | 3 | import io.jsonwebtoken.Claims; 4 | import io.jsonwebtoken.Jwts; 5 | import io.jsonwebtoken.security.Keys; 6 | import org.springframework.security.core.userdetails.UserDetails; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.crypto.SecretKey; 10 | import java.time.Instant; 11 | import java.util.Base64; 12 | import java.util.Date; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import java.util.concurrent.TimeUnit; 16 | 17 | @Service 18 | public class JwtService { 19 | 20 | private static final String SECRET = "638CBE3A90E0303BF3808F40F95A7F02A24B4B5D029C954CF553F79E9EF1DC0384BE681C249F1223F6B55AA21DC070914834CA22C8DD98E14A872CA010091ACC"; 21 | private static final long VALIDITY = TimeUnit.MINUTES.toMillis(30); 22 | 23 | public String generateToken(UserDetails userDetails) { 24 | Map claims = new HashMap<>(); 25 | claims.put("iss", "https://secure.genuinecoder.com"); 26 | return Jwts.builder() 27 | .claims(claims) 28 | .subject(userDetails.getUsername()) 29 | .issuedAt(Date.from(Instant.now())) 30 | .expiration(Date.from(Instant.now().plusMillis(VALIDITY))) 31 | .signWith(generateKey()) 32 | .compact(); 33 | } 34 | 35 | private SecretKey generateKey() { 36 | byte[] decodedKey = Base64.getDecoder().decode(SECRET); 37 | return Keys.hmacShaKeyFor(decodedKey); 38 | } 39 | 40 | public String extractUsername(String jwt) { 41 | Claims claims = getClaims(jwt); 42 | return claims.getSubject(); 43 | } 44 | 45 | private Claims getClaims(String jwt) { 46 | return Jwts.parser() 47 | .verifyWith(generateKey()) 48 | .build() 49 | .parseSignedClaims(jwt) 50 | .getPayload(); 51 | } 52 | 53 | public boolean isTokenValid(String jwt) { 54 | Claims claims = getClaims(jwt); 55 | return claims.getExpiration().after(Date.from(Instant.now())); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /LearnJWT/src/main/java/com/genuinecoder/jwt/webtoken/LoginForm.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt.webtoken; 2 | 3 | public record LoginForm (String username, String password) { 4 | } 5 | -------------------------------------------------------------------------------- /LearnJWT/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=jwt 2 | 3 | spring.datasource.url=jdbc:mysql://localhost:3306/tutorial?createDatabaseIfNotExist=true 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect 8 | spring.jpa.hibernate.ddl-auto=update -------------------------------------------------------------------------------- /LearnJWT/src/test/java/com/genuinecoder/jwt/JwtApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class JwtApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnJWT/src/test/java/com/genuinecoder/jwt/JwtSecretMakerTest.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.jwt; 2 | 3 | import io.jsonwebtoken.Jwts; 4 | import jakarta.xml.bind.DatatypeConverter; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import javax.crypto.SecretKey; 8 | 9 | public class JwtSecretMakerTest { 10 | 11 | @Test 12 | public void generateSecretKey() { 13 | SecretKey key = Jwts.SIG.HS512.key().build(); 14 | String encodedKey = DatatypeConverter.printHexBinary(key.getEncoded()); 15 | System.out.printf("\nKey = [%s]\n", encodedKey); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LearnJavaMailSender/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnJavaMailSender/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.3.2' 4 | id 'io.spring.dependency-management' version '1.1.6' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | toolchain { 12 | languageVersion = JavaLanguageVersion.of(21) 13 | } 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | implementation 'org.springframework.boot:spring-boot-starter-mail' 22 | implementation 'org.springframework.boot:spring-boot-starter-web' 23 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 24 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 25 | } 26 | 27 | tasks.named('test') { 28 | useJUnitPlatform() 29 | } 30 | -------------------------------------------------------------------------------- /LearnJavaMailSender/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnJavaMailSender/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnJavaMailSender/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnJavaMailSender/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnJavaMailSender/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'java-mail' 2 | -------------------------------------------------------------------------------- /LearnJavaMailSender/src/main/java/com/genuinecoder/java_mail/SpringBootEmailTutorialApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.java_mail; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootEmailTutorialApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootEmailTutorialApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnJavaMailSender/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=SpringBootEmailTutorial 2 | 3 | spring.mail.host=smtp.gmail.com 4 | spring.mail.port=587 5 | spring.mail.username= 6 | spring.mail.password= 7 | spring.mail.properties.mail.smtp.auth=true 8 | spring.mail.properties.mail.smtp.starttls.enable=true 9 | -------------------------------------------------------------------------------- /LearnJavaMailSender/src/main/resources/templates/email-content.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Spring Boot Tutorials Collection 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 |
16 | Your Channel Logo 17 |
18 | 19 | 20 |

Spring Boot Tutorials Collection

21 |

Dear Subscriber,

22 |

We're excited to share with you our curated playlist of Spring Boot tutorials. This playlist covers everything from the basics to advanced topics, designed to help you master Spring Boot.

23 | 24 |

Featured Tutorials:

25 |
    26 |
  • Introduction to Spring Boot - Watch Now
  • 27 |
  • Spring Security Tutorial - Watch Now
  • 28 |
  • JWT Authentication and Authorization using Spring Boot - Watch Now
  • 29 |
  • Spring Boot Microservices Full Course - Watch Now
  • 30 |
  • Java Reactive Programming using Spring WebFlux - Watch Now
  • 31 |
32 | 33 |

Explore the full playlist on our YouTube channel and enhance your Spring Boot skills!

34 | 35 | 36 | 37 |
38 |

You are receiving this email because you subscribed to our newsletter.

39 |
40 | 41 |
42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /LearnJavaMailSender/src/test/java/com/genuinecoder/java_mail/SpringBootEmailTutorialApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.java_mail; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootEmailTutorialApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.4' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | ext { 19 | set('springCloudVersion', "2023.0.1") 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.cloud:spring-cloud-starter-gateway-mvc' 24 | implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' 25 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 26 | } 27 | 28 | dependencyManagement { 29 | imports { 30 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 31 | } 32 | } 33 | 34 | tasks.named('test') { 35 | useJUnitPlatform() 36 | } 37 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnMicroservices/api-gateway/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'api-gateway' 2 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/src/main/java/com/genuinecoder/apigateway/ApiGatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.apigateway; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.web.servlet.config.annotation.CorsRegistry; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 9 | 10 | @SpringBootApplication 11 | @EnableDiscoveryClient 12 | 13 | public class ApiGatewayApplication { 14 | 15 | public static void main(String[] args) { 16 | SpringApplication.run(ApiGatewayApplication.class, args); 17 | } 18 | 19 | @Bean 20 | public WebMvcConfigurer webMvcConfigurer() { 21 | return new WebMvcConfigurer() { 22 | @Override 23 | public void addCorsMappings(CorsRegistry registry) { 24 | registry.addMapping("/**") 25 | .allowedMethods("*") 26 | .allowedHeaders("*") 27 | .allowedOrigins("*"); 28 | } 29 | }; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=api-gateway 2 | 3 | server.port=8080 4 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 5 | 6 | spring.cloud.gateway.mvc.routes[0].id=movie-catalog-service 7 | spring.cloud.gateway.mvc.routes[0].uri=http://localhost:8090 8 | spring.cloud.gateway.mvc.routes[0].predicates[0]=Path=/movie-info/** 9 | 10 | spring.cloud.gateway.mvc.routes[1].id=movie-streaming-service 11 | spring.cloud.gateway.mvc.routes[1].uri=http://localhost:8091 12 | spring.cloud.gateway.mvc.routes[1].predicates[0]=Path=/stream/** -------------------------------------------------------------------------------- /LearnMicroservices/api-gateway/src/test/java/com/genuinecoder/apigateway/ApiGatewayApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.apigateway; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ApiGatewayApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.4' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | ext { 19 | set('springCloudVersion', "2023.0.1") 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.cloud:spring-cloud-config-server' 24 | implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' 25 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 26 | } 27 | 28 | dependencyManagement { 29 | imports { 30 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 31 | } 32 | } 33 | 34 | tasks.named('test') { 35 | useJUnitPlatform() 36 | } 37 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnMicroservices/config-server/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnMicroservices/config-server/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'config-server' 2 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/src/main/java/com/genuinecoder/configserver/ConfigServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.configserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.config.server.EnableConfigServer; 7 | 8 | @SpringBootApplication 9 | @EnableDiscoveryClient 10 | @EnableConfigServer 11 | public class ConfigServerApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(ConfigServerApplication.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /LearnMicroservices/config-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=config-server 2 | server.port=8888 3 | 4 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 5 | 6 | spring.cloud.config.server.native.search-locations=F:\\Config 7 | spring.profiles.active=native -------------------------------------------------------------------------------- /LearnMicroservices/config-server/src/test/java/com/genuinecoder/configserver/ConfigServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.configserver; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ConfigServerApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnMicroservices/config/movie-catalog-service.properties: -------------------------------------------------------------------------------- 1 | server.port=8090 2 | 3 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 4 | 5 | spring.datasource.url=jdbc:mysql://localhost:3306/micro?createDatabaseIfNotExist=true 6 | spring.jpa.hibernate.ddl-auto=update 7 | spring.datasource.username=root 8 | spring.datasource.password=root 9 | 10 | management.tracing.sampling.probability=1 -------------------------------------------------------------------------------- /LearnMicroservices/config/movie-streaming-service-dev.properties: -------------------------------------------------------------------------------- 1 | server.port=8100 2 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka -------------------------------------------------------------------------------- /LearnMicroservices/config/movie-streaming-service.properties: -------------------------------------------------------------------------------- 1 | server.port=8091 2 | eureka.client.service-url.defaultZone=http://localhost:8761/eureka 3 | 4 | management.tracing.sampling.probability=1 -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.4' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | ext { 19 | set('springCloudVersion', "2023.0.1") 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.boot:spring-boot-starter-web' 24 | implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' 25 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 26 | 27 | implementation 'org.springframework.cloud:spring-cloud-starter-config' 28 | 29 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 30 | implementation 'com.mysql:mysql-connector-j' 31 | 32 | implementation 'org.springframework.boot:spring-boot-starter-actuator' 33 | implementation 'io.micrometer:micrometer-tracing-bridge-brave' 34 | implementation 'io.zipkin.reporter2:zipkin-reporter-brave' 35 | } 36 | 37 | dependencyManagement { 38 | imports { 39 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 40 | } 41 | } 42 | 43 | tasks.named('test') { 44 | useJUnitPlatform() 45 | } 46 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnMicroservices/movie-catalog-service/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'movie-catalog-service' 2 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/main/java/com/genuinecoder/moviecatalogservice/MovieCatalogServiceApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviecatalogservice; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class MovieCatalogServiceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MovieCatalogServiceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/main/java/com/genuinecoder/moviecatalogservice/controller/MovieInfoController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviecatalogservice.controller; 2 | 3 | import com.genuinecoder.moviecatalogservice.model.MovieInfo; 4 | import com.genuinecoder.moviecatalogservice.model.MovieInfoRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import java.util.List; 9 | 10 | @RestController 11 | public class MovieInfoController { 12 | 13 | @Autowired 14 | private MovieInfoRepository repository; 15 | 16 | @PostMapping("/movie-info/save") 17 | public List saveAll(@RequestBody List movieInfoList) { 18 | return repository.saveAll(movieInfoList); 19 | } 20 | 21 | @GetMapping("/movie-info/list") 22 | public List getAll() { 23 | return repository.findAll(); 24 | } 25 | 26 | @GetMapping("/movie-info/find-path-by-id/{movieInfoId}") 27 | public String findPathById(@PathVariable Long movieInfoId) { 28 | var videoInfoOptional = repository.findById(movieInfoId); 29 | return videoInfoOptional.map(MovieInfo::getPath).orElse(null); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/main/java/com/genuinecoder/moviecatalogservice/model/MovieInfo.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviecatalogservice.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | 8 | @Entity 9 | public class MovieInfo { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.IDENTITY) 13 | private Long id; 14 | private String name; 15 | private String description; 16 | private String path; 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public String getDescription() { 35 | return description; 36 | } 37 | 38 | public void setDescription(String description) { 39 | this.description = description; 40 | } 41 | 42 | public String getPath() { 43 | return path; 44 | } 45 | 46 | public void setPath(String path) { 47 | this.path = path; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/main/java/com/genuinecoder/moviecatalogservice/model/MovieInfoRepository.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviecatalogservice.model; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | @Repository 7 | public interface MovieInfoRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=movie-catalog-service 2 | 3 | spring.config.import=optional:configserver:http://localhost:8888 -------------------------------------------------------------------------------- /LearnMicroservices/movie-catalog-service/src/test/java/com/genuinecoder/moviecatalogservice/MovieCatalogServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviecatalogservice; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MovieCatalogServiceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.4' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | ext { 19 | set('springCloudVersion', "2023.0.1") 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.boot:spring-boot-starter-web' 24 | implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' 25 | implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer' 26 | implementation 'org.springframework.cloud:spring-cloud-starter-config' 27 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 28 | 29 | implementation 'org.springframework.boot:spring-boot-starter-actuator' 30 | implementation 'io.micrometer:micrometer-tracing-bridge-brave' 31 | implementation 'io.zipkin.reporter2:zipkin-reporter-brave' 32 | } 33 | 34 | dependencyManagement { 35 | imports { 36 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 37 | } 38 | } 39 | 40 | tasks.named('test') { 41 | useJUnitPlatform() 42 | } 43 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnMicroservices/movie-streaming-service/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'movie-streaming-service' 2 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/main/java/com/genuinecoder/moviestreamingservice/MovieStreamingServiceApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviestreamingservice; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | 7 | @SpringBootApplication 8 | @EnableDiscoveryClient 9 | public class MovieStreamingServiceApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(MovieStreamingServiceApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/main/java/com/genuinecoder/moviestreamingservice/controller/MovieCatalogService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviestreamingservice.controller; 2 | 3 | import com.netflix.discovery.converters.Auto; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Component; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | @Component 9 | public class MovieCatalogService { 10 | 11 | public static final String CATALOG_SERVICE = "http://movie-catalog-service"; 12 | 13 | @Autowired 14 | private RestTemplate restTemplate; 15 | 16 | public String getMoviePath(Long movieInfoId) { 17 | var response = restTemplate.getForEntity(CATALOG_SERVICE + "/movie-info/find-path-by-id/{movieInfoId}", String.class, movieInfoId); 18 | return response.getBody(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/main/java/com/genuinecoder/moviestreamingservice/controller/MovieStreamController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviestreamingservice.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.core.io.InputStreamResource; 5 | import org.springframework.http.MediaType; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import java.io.File; 12 | import java.io.FileInputStream; 13 | import java.io.FileNotFoundException; 14 | import java.util.logging.Level; 15 | import java.util.logging.Logger; 16 | 17 | @RestController 18 | public class MovieStreamController { 19 | 20 | public static final Logger log = Logger.getLogger(MovieStreamController.class.getName()); 21 | public static final String VIDEO_DIRECTORY = "F:\\Stream\\"; 22 | 23 | @Autowired 24 | private MovieCatalogService movieCatalogService; 25 | 26 | @GetMapping("/stream/{videoPath}") 27 | public ResponseEntity streamVideo(@PathVariable String videoPath) throws FileNotFoundException { 28 | File file = new File(VIDEO_DIRECTORY + videoPath); 29 | if (file.exists()) { 30 | InputStreamResource inputStreamResource = new InputStreamResource(new FileInputStream(file)); 31 | return ResponseEntity.ok() 32 | .contentType(MediaType.parseMediaType("video/mp4")) 33 | .body(inputStreamResource); 34 | } else { 35 | return ResponseEntity.notFound().build(); 36 | } 37 | } 38 | 39 | @GetMapping("/stream/with-id/{videoInfoId}") 40 | public ResponseEntity streamVideoById(@PathVariable Long videoInfoId) throws FileNotFoundException { 41 | String moviePath = movieCatalogService.getMoviePath(videoInfoId); 42 | log.log(Level.INFO, "Resolved movie path = {0}", moviePath); 43 | return streamVideo(moviePath); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/main/java/com/genuinecoder/moviestreamingservice/controller/RestConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviestreamingservice.controller; 2 | 3 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.client.RestTemplate; 7 | 8 | @Configuration 9 | public class RestConfiguration { 10 | 11 | @LoadBalanced 12 | @Bean 13 | public RestTemplate restTemplate() { 14 | return new RestTemplate(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=movie-streaming-service 2 | spring.config.import=optional:configserver:http://localhost:8888 3 | -------------------------------------------------------------------------------- /LearnMicroservices/movie-streaming-service/src/test/java/com/genuinecoder/moviestreamingservice/MovieStreamingServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.moviestreamingservice; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class MovieStreamingServiceApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.4' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | ext { 19 | set('springCloudVersion', "2023.0.1") 20 | } 21 | 22 | dependencies { 23 | implementation 'org.springframework.boot:spring-boot-starter-web' 24 | implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' 25 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 26 | } 27 | 28 | dependencyManagement { 29 | imports { 30 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 31 | } 32 | } 33 | 34 | tasks.named('test') { 35 | useJUnitPlatform() 36 | } 37 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnMicroservices/service-registry/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'service-registry' 2 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/src/main/java/com/genuinecoder/serviceregistry/ServiceRegistryApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.serviceregistry; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class ServiceRegistryApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ServiceRegistryApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=service-registry 2 | server.port=8761 3 | 4 | eureka.instance.hostname=localhost 5 | eureka.client.register-with-eureka=false 6 | eureka.client.fetch-registry=false -------------------------------------------------------------------------------- /LearnMicroservices/service-registry/src/test/java/com/genuinecoder/serviceregistry/ServiceRegistryApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.serviceregistry; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ServiceRegistryApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.2' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '17' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation 'org.springframework.boot:spring-boot-starter-webflux' 20 | implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '3.2.2' 21 | implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb-reactive', version: '3.2.2' 22 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 23 | testImplementation 'io.projectreactor:reactor-test' 24 | } 25 | 26 | tasks.named('test') { 27 | useJUnitPlatform() 28 | } 29 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnReactiveProgramming/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnReactiveProgramming/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'LearnReactiveProgramming' 2 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/BackpressureTutorial.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import reactor.core.publisher.BufferOverflowStrategy; 4 | import reactor.core.publisher.Flux; 5 | import reactor.core.publisher.Mono; 6 | 7 | import java.time.Duration; 8 | 9 | public class BackpressureTutorial { 10 | 11 | private Flux createNoOverflowFlux() { 12 | return Flux.range(1, Integer.MAX_VALUE) 13 | .log() 14 | .concatMap(x -> Mono.delay(Duration.ofMillis(100))); // simulate that processing takes time 15 | } 16 | 17 | private Flux createOverflowFlux() { 18 | return Flux.interval(Duration.ofMillis(1)) 19 | .log() 20 | .concatMap(x -> Mono.delay(Duration.ofMillis(100))); 21 | } 22 | 23 | private Flux createDropOnBackpressureFlux() { 24 | return Flux.interval(Duration.ofMillis(1)) 25 | .onBackpressureDrop() 26 | .concatMap(a -> Mono.delay(Duration.ofMillis(100)).thenReturn(a)) 27 | .doOnNext(a -> System.out.println("Element kept by consumer: " + a)); 28 | } 29 | 30 | private Flux createBufferOnBackpressureFlux() { 31 | return Flux.interval(Duration.ofMillis(1)) 32 | .onBackpressureBuffer(50, BufferOverflowStrategy.DROP_LATEST) 33 | .concatMap(a -> Mono.delay(Duration.ofMillis(100)).thenReturn(a)) 34 | .doOnNext(a -> System.out.println("Element kept by consumer: " + a)); 35 | } 36 | 37 | 38 | public static void main(String[] args) { 39 | BackpressureTutorial tutorial = new BackpressureTutorial(); 40 | tutorial.createBufferOnBackpressureFlux() 41 | .blockLast(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/DataController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import com.genuinecoder.learnreactiveprogramming.model.Customer; 4 | import com.genuinecoder.learnreactiveprogramming.model.Order; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate; 7 | import org.springframework.data.mongodb.core.query.Criteria; 8 | import org.springframework.data.mongodb.core.query.Query; 9 | import org.springframework.web.bind.annotation.*; 10 | import reactor.core.publisher.Mono; 11 | import reactor.util.function.Tuple2; 12 | 13 | import java.util.Map; 14 | 15 | @RestController 16 | public class DataController { 17 | 18 | @Autowired 19 | private ReactiveMongoTemplate reactiveMongoTemplate; 20 | 21 | @PostMapping("/customer/create") 22 | public Mono createCustomer(@RequestBody Customer customer) { 23 | return reactiveMongoTemplate.save(customer); 24 | } 25 | 26 | @GetMapping("/customer/find-by-id") 27 | public Mono findCustomerById(@RequestParam("customerId") String customerId) { 28 | return getCustomerById(customerId); 29 | } 30 | 31 | @PostMapping("/order/create") 32 | public Mono createOrder(@RequestBody Order order) { 33 | return reactiveMongoTemplate.save(order); 34 | } 35 | 36 | /** 37 | * Expected output 38 | * Genuine Coder : SUM(Order Total) 39 | * Anya : SUM(Order Total) 40 | * Bruce: 0 41 | */ 42 | @GetMapping("/sales/summary") 43 | public Mono> calculateSummary() { 44 | return reactiveMongoTemplate.findAll(Customer.class) 45 | .flatMap(customer -> Mono.zip(Mono.just(customer), calculateOrderSum(customer.getId()))) 46 | .collectMap(tuple2 -> tuple2.getT1().getName(), Tuple2::getT2); 47 | } 48 | 49 | private Mono calculateOrderSum(String customerId) { 50 | Criteria criteria = Criteria.where("customerId").is(customerId); 51 | return reactiveMongoTemplate.find(Query.query(criteria), Order.class) 52 | .map(Order::getTotal) 53 | .reduce(0d, Double::sum); 54 | } 55 | 56 | private Mono getCustomerById(String customerId) { 57 | Criteria criteria = Criteria.where("id").is(customerId); 58 | Query query = Query.query(criteria); 59 | return reactiveMongoTemplate.findOne(query, Customer.class) 60 | .log(); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/DatabaseConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import com.mongodb.reactivestreams.client.MongoClient; 4 | import com.mongodb.reactivestreams.client.MongoClients; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; 7 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate; 8 | import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; 9 | 10 | @EnableReactiveMongoRepositories 11 | public class DatabaseConfiguration extends AbstractReactiveMongoConfiguration { 12 | 13 | @Bean 14 | public MongoClient mongoClient() { 15 | return MongoClients.create(); 16 | } 17 | 18 | @Override 19 | protected String getDatabaseName() { 20 | return "genuine_coder"; 21 | } 22 | 23 | @Bean 24 | public ReactiveMongoTemplate reactiveMongoTemplate() { 25 | return new ReactiveMongoTemplate(mongoClient(), getDatabaseName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/LearnReactiveProgrammingApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class LearnReactiveProgrammingApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(LearnReactiveProgrammingApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/MainController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import reactor.core.publisher.Mono; 6 | 7 | @Controller 8 | public class MainController { 9 | 10 | @GetMapping("/") 11 | public Mono handleMain() { 12 | return Mono.just("home"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | import java.util.UUID; 7 | 8 | @Document 9 | public class Customer { 10 | 11 | @Id 12 | private String id; 13 | private String name; 14 | private String job; 15 | 16 | public Customer() { 17 | 18 | } 19 | 20 | public Customer(String name, String job) { 21 | this.id = UUID.randomUUID().toString(); 22 | this.name = name; 23 | this.job = job; 24 | } 25 | 26 | public String getId() { 27 | return id; 28 | } 29 | 30 | public void setId(String id) { 31 | this.id = id; 32 | } 33 | 34 | public String getName() { 35 | return name; 36 | } 37 | 38 | public void setName(String name) { 39 | this.name = name; 40 | } 41 | 42 | public String getJob() { 43 | return job; 44 | } 45 | 46 | public void setJob(String job) { 47 | this.job = job; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "Customer{" + 53 | "id='" + id + '\'' + 54 | ", name='" + name + '\'' + 55 | ", job='" + job + '\'' + 56 | '}'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/java/com/genuinecoder/learnreactiveprogramming/model/Order.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | import java.util.UUID; 7 | 8 | @Document 9 | public class Order { 10 | 11 | @Id 12 | private String id; 13 | private String customerId; 14 | private Double total; 15 | private Double discount; 16 | 17 | public Order(String customerId, Double total, Double discount) { 18 | this.id = UUID.randomUUID().toString(); 19 | this.customerId = customerId; 20 | this.total = total; 21 | this.discount = discount; 22 | } 23 | 24 | public Order() { 25 | 26 | } 27 | 28 | public String getId() { 29 | return id; 30 | } 31 | 32 | public void setId(String id) { 33 | this.id = id; 34 | } 35 | 36 | public String getCustomerId() { 37 | return customerId; 38 | } 39 | 40 | public void setCustomerId(String customerId) { 41 | this.customerId = customerId; 42 | } 43 | 44 | public Double getTotal() { 45 | return total; 46 | } 47 | 48 | public void setTotal(Double total) { 49 | this.total = total; 50 | } 51 | 52 | public Double getDiscount() { 53 | return discount; 54 | } 55 | 56 | public void setDiscount(Double discount) { 57 | this.discount = discount; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Order{" + 63 | "id='" + id + '\'' + 64 | ", customerId='" + customerId + '\'' + 65 | ", total=" + total + 66 | ", discount=" + discount + 67 | '}'; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring Reactive Tutorial 7 | 40 | 41 | 42 |
43 |

Spring Reactive Tutorial

44 |
45 | 46 |
47 |

Welcome to the Spring Reactive Tutorial!

48 |

This tutorial will guide you through the basics of building reactive applications with Spring.

49 |

Topics covered:

50 |
    51 |
  • Introduction to Reactive Programming
  • 52 |
  • Spring WebFlux
  • 53 |
  • Reactive Controllers
  • 54 |
  • Reactive Repositories
  • 55 | 56 |
57 |

Get ready to dive into the world of Spring Reactive programming!

58 |
59 | 60 |
61 |

© 2024 Your Name. All rights reserved.

62 |
63 | 64 | 65 | -------------------------------------------------------------------------------- /LearnReactiveProgramming/src/test/java/com/genuinecoder/learnreactiveprogramming/LearnReactiveProgrammingApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnreactiveprogramming; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LearnReactiveProgrammingApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringAI/.gitattributes: -------------------------------------------------------------------------------- 1 | /gradlew text eol=lf 2 | *.bat text eol=crlf 3 | *.jar binary 4 | -------------------------------------------------------------------------------- /LearnSpringAI/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnSpringAI/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.4.0' 4 | id 'io.spring.dependency-management' version '1.1.6' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | toolchain { 12 | languageVersion = JavaLanguageVersion.of(21) 13 | } 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | maven { url 'https://repo.spring.io/milestone' } 19 | } 20 | 21 | ext { 22 | set('springAiVersion', "1.0.0-M4") 23 | } 24 | 25 | dependencies { 26 | implementation 'org.springframework.boot:spring-boot-starter-web' 27 | implementation 'org.springframework.ai:spring-ai-ollama-spring-boot-starter' 28 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 29 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 30 | } 31 | 32 | dependencyManagement { 33 | imports { 34 | mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}" 35 | } 36 | } 37 | 38 | tasks.named('test') { 39 | useJUnitPlatform() 40 | } 41 | -------------------------------------------------------------------------------- /LearnSpringAI/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringAI/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnSpringAI/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnSpringAI/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spring-ai' 2 | -------------------------------------------------------------------------------- /LearnSpringAI/src/main/java/com/genuinecoder/spring_ai/LearnSpringAiApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.spring_ai; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class LearnSpringAiApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(LearnSpringAiApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringAI/src/main/java/com/genuinecoder/spring_ai/configuration/ChatClientConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.spring_ai.configuration; 2 | 3 | import org.springframework.ai.chat.client.ChatClient; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | @Configuration 8 | public class ChatClientConfiguration { 9 | 10 | @Bean 11 | public ChatClient chatClient(ChatClient.Builder builder) { 12 | return builder.build(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LearnSpringAI/src/main/java/com/genuinecoder/spring_ai/controller/ChatController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.spring_ai.controller; 2 | 3 | import org.springframework.ai.chat.client.ChatClient; 4 | import org.springframework.core.io.InputStreamResource; 5 | import org.springframework.util.MimeTypeUtils; 6 | import org.springframework.web.bind.annotation.*; 7 | import org.springframework.web.multipart.MultipartFile; 8 | import reactor.core.publisher.Flux; 9 | 10 | @RestController 11 | public class ChatController { 12 | 13 | private final ChatClient chatClient; 14 | 15 | public ChatController(ChatClient chatClient) { 16 | this.chatClient = chatClient; 17 | } 18 | 19 | @GetMapping("/chat") 20 | public String chat(@RequestParam("message") String message) { 21 | return chatClient.prompt() 22 | .user(message) 23 | .call() 24 | .content(); 25 | } 26 | 27 | @PostMapping("/chat-with-image") 28 | public String chat(@RequestPart("message") String message, @RequestPart("image") MultipartFile file) { 29 | return chatClient.prompt() 30 | .user(prompt -> prompt 31 | .text(message) 32 | .media(MimeTypeUtils.IMAGE_PNG, new InputStreamResource(file)) 33 | ) 34 | .call() 35 | .content(); 36 | } 37 | 38 | @GetMapping("/chat-with-stream-response") 39 | public Flux chatWithFlux(@RequestParam("message") String message) { 40 | return chatClient.prompt() 41 | .user(message) 42 | .stream() 43 | .content() 44 | .doOnNext(System.out::print); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /LearnSpringAI/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=LearnSpringAi 2 | 3 | spring.ai.ollama.chat.model=llama3.2-vision:11b -------------------------------------------------------------------------------- /LearnSpringAI/src/test/java/com/genuinecoder/spring_ai/LearnSpringAiApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.spring_ai; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LearnSpringAiApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringBoot/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnSpringBoot/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.3' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '21' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation 'org.springframework.boot:spring-boot-starter-web' 20 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 21 | implementation 'org.springframework.boot:spring-boot-starter-actuator' 22 | implementation 'com.mysql:mysql-connector-j' 23 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 24 | } 25 | 26 | tasks.named('test') { 27 | useJUnitPlatform() 28 | } 29 | -------------------------------------------------------------------------------- /LearnSpringBoot/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringBoot/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnSpringBoot/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnSpringBoot/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnSpringBoot/log/myLog.log.2024-03-03.0.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringBoot/log/myLog.log.2024-03-03.0.gz -------------------------------------------------------------------------------- /LearnSpringBoot/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'LearnSpringBoot' 2 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class Calculator { 7 | 8 | public int calculateSum(int a, int b) { 9 | return a + b; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/LearnSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class LearnSpringBootApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(LearnSpringBootApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/MySimpleController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.web.bind.annotation.*; 5 | 6 | @RestController 7 | @Profile("prod") 8 | public class MySimpleController { 9 | 10 | private final Calculator calculator; 11 | 12 | public MySimpleController(Calculator calculator) { 13 | this.calculator = calculator; 14 | } 15 | 16 | @RequestMapping(value = {"/home"}, method = {RequestMethod.GET, RequestMethod.POST}) 17 | public String home() { 18 | return "Hello World!"; 19 | } 20 | 21 | @GetMapping("/test") 22 | public String secondHome() { 23 | return "Hello World2!"; 24 | } 25 | 26 | //localhost:8500/sum?a=6&b=7 27 | @GetMapping("/sum") 28 | public int sum(@RequestParam("a") int a, @RequestParam("b") int b) { 29 | return calculator.calculateSum(a, b); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/MySimpleDevController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RequestMethod; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | @Profile("dev") 11 | public class MySimpleDevController { 12 | 13 | @RequestMapping(value = {"/home"}, method = {RequestMethod.GET, RequestMethod.POST}) 14 | public String home() { 15 | return "Hi Humann!"; 16 | } 17 | 18 | @GetMapping("/test") 19 | public String secondHome() { 20 | return "Prove me that you are not a robot!!"; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/StudentController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import com.genuinecoder.LearnSpringBoot.model.Student; 4 | import com.genuinecoder.LearnSpringBoot.model.StudentRepository; 5 | import org.springframework.web.bind.annotation.*; 6 | 7 | import java.util.List; 8 | import java.util.Optional; 9 | 10 | @RestController 11 | public class StudentController { 12 | 13 | private final StudentRepository repository; 14 | 15 | public StudentController(StudentRepository repository) { 16 | this.repository = repository; 17 | } 18 | 19 | @PostMapping("/student/save") 20 | public Student createStudent(@RequestBody Student student) { 21 | return repository.save(student); 22 | } 23 | 24 | @GetMapping("/student/findByEmail") 25 | public Student findStudent(@RequestParam("email") String email) { 26 | return repository.findByEmail(email); 27 | } 28 | 29 | @PatchMapping("/student/update") 30 | public Student findStudent(@RequestParam("student_id") Long id, @RequestParam("new_email") String email) { 31 | Optional student = repository.findById(id); 32 | if (student.isPresent()) { 33 | Student studentObj = student.get(); 34 | studentObj.setEmail(email); 35 | return repository.save(studentObj); 36 | } 37 | return null; 38 | } 39 | 40 | @GetMapping("/student/findByEmailDomain") 41 | public List findStudentByDomain(@RequestParam("domain") String domain) { 42 | return repository.findByDomain(domain); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/model/Student.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot.model; 2 | 3 | import jakarta.persistence.*; 4 | 5 | @Entity 6 | public class Student { 7 | 8 | @Id 9 | @GeneratedValue(strategy = GenerationType.IDENTITY) 10 | private Long id; 11 | private String name; 12 | @Column(unique = true, length = 1024) 13 | private String email; 14 | 15 | public Long getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Long id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getEmail() { 32 | return email; 33 | } 34 | 35 | public void setEmail(String email) { 36 | this.email = email; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/java/com/genuinecoder/LearnSpringBoot/model/StudentRepository.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot.model; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.jpa.repository.Query; 5 | import org.springframework.data.repository.query.Param; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | 10 | @Repository 11 | public interface StudentRepository extends JpaRepository { 12 | 13 | Student findByEmail(String email); 14 | 15 | @Query("SELECT s from Student s WHERE s.email LIKE %:domain") 16 | List findByDomain(@Param("domain") String domain); 17 | } 18 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | server.port=8500 2 | 3 | spring.datasource.username=root 4 | spring.datasource.password=root -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | server.port=9000 2 | 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | 6 | management.endpoints.web.exposure.include=health 7 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.file.name=log/myLog.log 2 | 3 | spring.datasource.url=jdbc:mysql://localhost:3306/tutorial?createDatabaseIfNotExist=true 4 | spring.jpa.hibernate.ddl-auto=update 5 | 6 | spring.profiles.active=prod 7 | 8 | management.endpoints.web.exposure.include=health,info,metrics 9 | management.endpoint.health.show-details=always -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/static/error/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 404 - Page Not Found 8 | 50 | 51 | 52 | 53 |
54 |

404

55 |

Page Not Found

56 | 404 Image 57 |

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

58 |

Go back to home page.

59 |
60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Spring Boot Learning Tutorial 8 | 61 | 62 | 63 | 64 |
65 |

Spring Boot Learning Tutorial

66 |
67 | 68 |
69 |

Welcome to our Spring Boot Tutorial!

70 |

71 | Whether you are a beginner or an experienced developer, our Spring Boot tutorial will guide you through 72 | the 73 | fundamentals and advanced concepts of building robust and scalable web applications with Spring Boot. 74 |

75 |

76 | Get started by exploring our comprehensive tutorials and hands-on examples. Master the key concepts 77 | and best 78 | practices to develop modern and efficient Spring Boot applications. 79 |

80 | Start Learning 81 |
82 | 83 |
84 |

Tutorial Highlights

85 | 86 |
87 | 88 |
89 |

© 2024 Your Company. All rights reserved.

90 |
91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/main/resources/static/test_me.html: -------------------------------------------------------------------------------- 1 | 2 |

Test me!!

3 | -------------------------------------------------------------------------------- /LearnSpringBoot/src/test/java/com/genuinecoder/LearnSpringBoot/LearnSpringBootApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.LearnSpringBoot; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LearnSpringBootApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/checksums/checksums.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/checksums/checksums.lock -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/checksums/md5-checksums.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/checksums/md5-checksums.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/checksums/sha1-checksums.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/checksums/sha1-checksums.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/dependencies-accessors/gc.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/dependencies-accessors/gc.properties -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/executionHistory/executionHistory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/executionHistory/executionHistory.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/executionHistory/executionHistory.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/executionHistory/executionHistory.lock -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/fileChanges/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/fileHashes/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/fileHashes/fileHashes.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/fileHashes/fileHashes.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/fileHashes/fileHashes.lock -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/fileHashes/resourceHashesCache.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/fileHashes/resourceHashesCache.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/8.8/gc.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/8.8/gc.properties -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/buildOutputCleanup/buildOutputCleanup.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/buildOutputCleanup/buildOutputCleanup.lock -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/buildOutputCleanup/cache.properties: -------------------------------------------------------------------------------- 1 | #Sat Aug 10 16:58:20 IST 2024 2 | gradle.version=8.8 3 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/buildOutputCleanup/outputFiles.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/buildOutputCleanup/outputFiles.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/file-system.probe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/file-system.probe -------------------------------------------------------------------------------- /LearnSpringOAuth/.gradle/vcs-1/gc.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/.gradle/vcs-1/gc.properties -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/.name: -------------------------------------------------------------------------------- 1 | oauth -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 16 | 17 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /LearnSpringOAuth/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LearnSpringOAuth/HELP.md: -------------------------------------------------------------------------------- 1 | # Getting Started 2 | 3 | ### Reference Documentation 4 | For further reference, please consider the following sections: 5 | 6 | * [Official Gradle documentation](https://docs.gradle.org) 7 | * [Spring Boot Gradle Plugin Reference Guide](https://docs.spring.io/spring-boot/3.3.2/gradle-plugin) 8 | * [Create an OCI image](https://docs.spring.io/spring-boot/3.3.2/gradle-plugin/packaging-oci-image.html) 9 | * [Spring Web](https://docs.spring.io/spring-boot/docs/3.3.2/reference/htmlsingle/index.html#web) 10 | * [OAuth2 Client](https://docs.spring.io/spring-boot/docs/3.3.2/reference/htmlsingle/index.html#web.security.oauth2.client) 11 | * [Thymeleaf](https://docs.spring.io/spring-boot/docs/3.3.2/reference/htmlsingle/index.html#web.servlet.spring-mvc.template-engines) 12 | 13 | ### Guides 14 | The following guides illustrate how to use some features concretely: 15 | 16 | * [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) 17 | * [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) 18 | * [Building REST services with Spring](https://spring.io/guides/tutorials/rest/) 19 | * [Handling Form Submission](https://spring.io/guides/gs/handling-form-submission/) 20 | 21 | ### Additional Links 22 | These additional references should also help you: 23 | 24 | * [Gradle Build Scans – insights for your project's build](https://scans.gradle.com#gradle) 25 | 26 | -------------------------------------------------------------------------------- /LearnSpringOAuth/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.3.2' 4 | id 'io.spring.dependency-management' version '1.1.6' 5 | } 6 | 7 | group = 'com.genuine' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | toolchain { 12 | languageVersion = JavaLanguageVersion.of(21) 13 | } 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' 22 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 23 | implementation 'org.springframework.boot:spring-boot-starter-web' 24 | implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' 25 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 26 | testImplementation 'org.springframework.security:spring-security-test' 27 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 28 | } 29 | 30 | tasks.named('test') { 31 | useJUnitPlatform() 32 | } 33 | -------------------------------------------------------------------------------- /LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/LearnSpringOAuthApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/LearnSpringOAuthApplication.class -------------------------------------------------------------------------------- /LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/MainController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/MainController.class -------------------------------------------------------------------------------- /LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/SpringConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/SpringConfig.class -------------------------------------------------------------------------------- /LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/ViewController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/classes/java/main/com/genuine/oauth/ViewController.class -------------------------------------------------------------------------------- /LearnSpringOAuth/build/resources/main/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=LearnSpringOAuth 2 | 3 | spring.security.oauth2.client.registration.google.client-id=221171710928-19ck8asng935hr92p45ovucaobe6aadl.apps.googleusercontent.com 4 | spring.security.oauth2.client.registration.google.client-secret=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 -------------------------------------------------------------------------------- /LearnSpringOAuth/build/resources/main/templates/custom_login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Sign in with google Button 8 | 9 | 57 | 58 | 59 |
60 | 63 |
64 | 65 | 66 | -------------------------------------------------------------------------------- /LearnSpringOAuth/build/resources/main/templates/user-profile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | User Profile 6 | 40 | 41 | 42 |
43 | User Photo 44 |

User Name

45 |

user@example.com

46 |
47 | 48 | 49 | -------------------------------------------------------------------------------- /LearnSpringOAuth/build/tmp/compileJava/compileTransaction/stash-dir/SpringConfig.class.uniqueId1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/tmp/compileJava/compileTransaction/stash-dir/SpringConfig.class.uniqueId1 -------------------------------------------------------------------------------- /LearnSpringOAuth/build/tmp/compileJava/compileTransaction/stash-dir/ViewController.class.uniqueId0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/tmp/compileJava/compileTransaction/stash-dir/ViewController.class.uniqueId0 -------------------------------------------------------------------------------- /LearnSpringOAuth/build/tmp/compileJava/previous-compilation-data.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/build/tmp/compileJava/previous-compilation-data.bin -------------------------------------------------------------------------------- /LearnSpringOAuth/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringOAuth/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnSpringOAuth/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnSpringOAuth/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnSpringOAuth/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'oauth' 2 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/java/com/genuine/oauth/LearnSpringOAuthApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuine.oauth; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 6 | 7 | @SpringBootApplication 8 | @EnableWebSecurity 9 | public class LearnSpringOAuthApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(LearnSpringOAuthApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/java/com/genuine/oauth/MainController.java: -------------------------------------------------------------------------------- 1 | package com.genuine.oauth; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import java.security.Principal; 7 | 8 | @RestController 9 | public class MainController { 10 | 11 | @RequestMapping("/") 12 | public String home() { 13 | return "Welcome!"; 14 | } 15 | 16 | @RequestMapping("/user") 17 | public Principal user(Principal user) { 18 | return user; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/java/com/genuine/oauth/SpringConfig.java: -------------------------------------------------------------------------------- 1 | package com.genuine.oauth; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6 | import org.springframework.security.web.SecurityFilterChain; 7 | 8 | @Configuration 9 | public class SpringConfig { 10 | 11 | @Bean 12 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { 13 | return http 14 | .authorizeHttpRequests(registry -> { 15 | registry.requestMatchers("/", "/login").permitAll(); 16 | registry.anyRequest().authenticated(); 17 | }) 18 | .oauth2Login(oauth2login -> { 19 | oauth2login 20 | .loginPage("/login") 21 | .successHandler((request, response, authentication) -> response.sendRedirect("/profile")); 22 | }) 23 | .build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/java/com/genuine/oauth/ViewController.java: -------------------------------------------------------------------------------- 1 | package com.genuine.oauth; 2 | 3 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | 8 | @Controller 9 | public class ViewController { 10 | 11 | @GetMapping("/profile") 12 | public String profile(OAuth2AuthenticationToken token, Model model) { 13 | model.addAttribute("name", token.getPrincipal().getAttribute("name")); 14 | model.addAttribute("email", token.getPrincipal().getAttribute("email")); 15 | model.addAttribute("photo", token.getPrincipal().getAttribute("picture")); 16 | return "user-profile"; 17 | } 18 | 19 | @GetMapping("/login") 20 | public String login() { 21 | return "custom_login"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=LearnSpringOAuth 2 | 3 | spring.security.oauth2.client.registration.google.client-id= 4 | spring.security.oauth2.client.registration.google.client-secret= -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/resources/templates/custom_login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Sign in with google Button 8 | 9 | 57 | 58 | 59 |
60 | 63 |
64 | 65 | 66 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/main/resources/templates/user-profile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | User Profile 6 | 40 | 41 | 42 |
43 | User Photo 44 |

User Name

45 |

user@example.com

46 |
47 | 48 | 49 | -------------------------------------------------------------------------------- /LearnSpringOAuth/src/test/java/com/genuine/oauth/LearnSpringOAuthApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuine.oauth; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LearnSpringOAuthApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringSecurity/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnSpringSecurity/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.2.2' 4 | id 'io.spring.dependency-management' version '1.1.4' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | sourceCompatibility = '17' 12 | } 13 | 14 | repositories { 15 | mavenCentral() 16 | } 17 | 18 | dependencies { 19 | implementation('org.springframework.boot:spring-boot-starter-security') 20 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 21 | implementation 'org.springframework.boot:spring-boot-starter-web' 22 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 23 | implementation 'com.mysql:mysql-connector-j' 24 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 25 | } 26 | 27 | tasks.named('test') { 28 | useJUnitPlatform() 29 | } 30 | -------------------------------------------------------------------------------- /LearnSpringSecurity/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnSpringSecurity/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnSpringSecurity/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnSpringSecurity/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /LearnSpringSecurity/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'LearnSpringSecurity' 2 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/AuthenticationSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity; 2 | 3 | import jakarta.servlet.ServletException; 4 | import jakarta.servlet.http.HttpServletRequest; 5 | import jakarta.servlet.http.HttpServletResponse; 6 | import org.springframework.security.core.Authentication; 7 | import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 8 | 9 | import java.io.IOException; 10 | 11 | public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { 12 | 13 | @Override 14 | public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { 15 | boolean isAdmin = authentication.getAuthorities().stream() 16 | .anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ROLE_ADMIN")); 17 | if (isAdmin) { 18 | setDefaultTargetUrl("/admin/home"); 19 | } else { 20 | setDefaultTargetUrl("/user/home"); 21 | } 22 | super.onAuthenticationSuccess(request, response, authentication); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/ContentController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | @Controller 7 | public class ContentController { 8 | 9 | @GetMapping("/home") 10 | public String handleWelcome() { 11 | return "home"; 12 | } 13 | 14 | @GetMapping("/admin/home") 15 | public String handleAdminHome() { 16 | return "home_admin"; 17 | } 18 | 19 | @GetMapping("/user/home") 20 | public String handleUserHome() { 21 | return "home_user"; 22 | } 23 | 24 | @GetMapping("/login") 25 | public String handleLogin() { 26 | return "custom_login"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/LearnSpringSecurityApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class LearnSpringSecurityApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(LearnSpringSecurityApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/RegistrationController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity; 2 | 3 | import com.genuinecoder.learnspringsecurity.model.MyUser; 4 | import com.genuinecoder.learnspringsecurity.model.MyUserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.security.crypto.password.PasswordEncoder; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | public class RegistrationController { 13 | 14 | @Autowired 15 | private MyUserRepository myUserRepository; 16 | @Autowired 17 | private PasswordEncoder passwordEncoder; 18 | 19 | @PostMapping("/register/user") 20 | public MyUser createUser(@RequestBody MyUser user) { 21 | user.setPassword(passwordEncoder.encode(user.getPassword())); 22 | return myUserRepository.save(user); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/model/MyUser.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | 8 | @Entity 9 | public class MyUser { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.AUTO) 13 | private Long id; 14 | private String username; 15 | private String password; 16 | private String role; //Eg: ADMIN,USER 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getUsername() { 27 | return username; 28 | } 29 | 30 | public void setUsername(String username) { 31 | this.username = username; 32 | } 33 | 34 | public String getPassword() { 35 | return password; 36 | } 37 | 38 | public void setPassword(String password) { 39 | this.password = password; 40 | } 41 | 42 | public String getRole() { 43 | return role; 44 | } 45 | 46 | public void setRole(String role) { 47 | this.role = role; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/model/MyUserDetailService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity.model; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.User; 5 | import org.springframework.security.core.userdetails.UserDetails; 6 | import org.springframework.security.core.userdetails.UserDetailsService; 7 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.Optional; 11 | 12 | @Service 13 | public class MyUserDetailService implements UserDetailsService { 14 | 15 | @Autowired 16 | private MyUserRepository repository; 17 | 18 | @Override 19 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 20 | Optional user = repository.findByUsername(username); 21 | if (user.isPresent()) { 22 | var userObj = user.get(); 23 | return User.builder() 24 | .username(userObj.getUsername()) 25 | .password(userObj.getPassword()) 26 | .roles(getRoles(userObj)) 27 | .build(); 28 | } else { 29 | throw new UsernameNotFoundException(username); 30 | } 31 | } 32 | 33 | private String[] getRoles(MyUser user) { 34 | if (user.getRole() == null) { 35 | return new String[]{"USER"}; 36 | } 37 | return user.getRole().split(","); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/java/com/genuinecoder/learnspringsecurity/model/MyUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity.model; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import java.util.Optional; 6 | 7 | public interface MyUserRepository extends JpaRepository { 8 | 9 | Optional findByUsername(String username); 10 | } 11 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/tutorial?createDatabaseIfNotExist=true 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | 5 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect 6 | spring.jpa.hibernate.ddl-auto=update 7 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/public/error/403.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 403 Forbidden 7 | 31 | 32 | 33 |
34 |

403 Forbidden

35 |

Sorry, you don't have permission to access this resource.

36 |
37 | 38 | 39 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/public/error/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 404 Not Found 7 | 31 | 32 | 33 |
34 |

404 Not Found

35 |

Sorry, the page you are looking for might be in another castle.

36 |
37 | 38 | 39 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/templates/custom_login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Login 7 | 58 | 59 | 60 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring Boot Security Tutorial 7 | 53 | 54 | 55 |
56 |

Spring Boot Security Tutorial

57 |

Welcome to the open section of the tutorial. Explore and learn about Spring Boot Security.

58 |
59 | 60 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/templates/home_admin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Admin Home - Spring Boot Security Tutorial 7 | 40 | 41 | 42 |
43 |

Welcome, Admin!

44 |

This is the admin home page. Explore and manage your admin functionalities.

45 |
46 | 47 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/main/resources/templates/home_user.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | User Home - Spring Boot Security Tutorial 7 | 40 | 41 | 42 |
43 |

Welcome, User!

44 |

This is the user home page. Explore and access your user-specific features.

45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /LearnSpringSecurity/src/test/java/com/genuinecoder/learnspringsecurity/LearnSpringSecurityApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.learnspringsecurity; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LearnSpringSecurityApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnUnitTest/.gitattributes: -------------------------------------------------------------------------------- 1 | /gradlew text eol=lf 2 | *.bat text eol=crlf 3 | *.jar binary 4 | -------------------------------------------------------------------------------- /LearnUnitTest/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /LearnUnitTest/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.springframework.boot' version '3.3.5' 4 | id 'io.spring.dependency-management' version '1.1.6' 5 | } 6 | 7 | group = 'com.genuinecoder' 8 | version = '0.0.1-SNAPSHOT' 9 | 10 | java { 11 | toolchain { 12 | languageVersion = JavaLanguageVersion.of(21) 13 | } 14 | } 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | 20 | dependencies { 21 | implementation 'org.springframework.boot:spring-boot-starter-security' 22 | implementation 'org.springframework.boot:spring-boot-starter-web' 23 | implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 24 | implementation 'org.springframework.boot:spring-boot-starter-data-jpa' 25 | implementation 'com.mysql:mysql-connector-j' 26 | testImplementation 'org.springframework.boot:spring-boot-starter-test' 27 | testImplementation 'org.springframework.security:spring-security-test' 28 | testImplementation 'com.h2database:h2' 29 | 30 | 31 | testRuntimeOnly 'org.junit.platform:junit-platform-launcher' 32 | } 33 | 34 | tasks.named('test') { 35 | useJUnitPlatform() 36 | } 37 | -------------------------------------------------------------------------------- /LearnUnitTest/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/afsalashyana/Spring-Boot-Tutorials/2998eb076c939a94558da8dd90564180d1adac72/LearnUnitTest/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /LearnUnitTest/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /LearnUnitTest/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'LearnSpringBootTest' 2 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/SpringBootTestApplication.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootTestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootTestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/config/AuthenticationSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.config; 2 | 3 | import jakarta.servlet.ServletException; 4 | import jakarta.servlet.http.HttpServletRequest; 5 | import jakarta.servlet.http.HttpServletResponse; 6 | import org.springframework.security.core.Authentication; 7 | import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 8 | 9 | import java.io.IOException; 10 | 11 | public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { 12 | 13 | @Override 14 | public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { 15 | boolean isAdmin = authentication.getAuthorities().stream() 16 | .anyMatch(grantedAuthority -> grantedAuthority.getAuthority().equals("ROLE_ADMIN")); 17 | if (isAdmin) { 18 | setDefaultTargetUrl("/admin/home"); 19 | } else { 20 | setDefaultTargetUrl("/user/home"); 21 | } 22 | super.onAuthenticationSuccess(request, response, authentication); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/config/SecurityConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.config; 2 | 3 | import com.genuinecoder.springboottest.model.MyUserDetailService; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.authentication.AuthenticationProvider; 7 | import org.springframework.security.authentication.dao.DaoAuthenticationProvider; 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; 11 | import org.springframework.security.core.userdetails.UserDetailsService; 12 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 13 | import org.springframework.security.crypto.password.PasswordEncoder; 14 | import org.springframework.security.web.SecurityFilterChain; 15 | 16 | @Configuration 17 | @EnableWebSecurity 18 | public class SecurityConfiguration { 19 | 20 | private final MyUserDetailService userDetailService; 21 | 22 | public SecurityConfiguration(MyUserDetailService userDetailService) { 23 | this.userDetailService = userDetailService; 24 | } 25 | 26 | @Bean 27 | public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { 28 | return httpSecurity 29 | .csrf(AbstractHttpConfigurer::disable) 30 | .authorizeHttpRequests(registry -> { 31 | registry.requestMatchers("/home", "/register/**").permitAll(); 32 | registry.requestMatchers("/admin/**").hasRole("ADMIN"); 33 | registry.requestMatchers("/user/**").hasRole("USER"); 34 | registry.anyRequest().authenticated(); 35 | }) 36 | .formLogin(httpSecurityFormLoginConfigurer -> { 37 | httpSecurityFormLoginConfigurer 38 | .loginPage("/login") 39 | .successHandler(new AuthenticationSuccessHandler()) 40 | .permitAll(); 41 | }) 42 | .build(); 43 | } 44 | 45 | @Bean 46 | public UserDetailsService userDetailsService() { 47 | return userDetailService; 48 | } 49 | 50 | @Bean 51 | public AuthenticationProvider authenticationProvider() { 52 | DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); 53 | provider.setUserDetailsService(userDetailService); 54 | provider.setPasswordEncoder(passwordEncoder()); 55 | return provider; 56 | } 57 | 58 | @Bean 59 | public PasswordEncoder passwordEncoder() { 60 | return new BCryptPasswordEncoder(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/controller/ContentController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | 6 | @Controller 7 | public class ContentController { 8 | 9 | @GetMapping("/home") 10 | public String handleWelcome() { 11 | return "home"; 12 | } 13 | 14 | @GetMapping("/admin/home") 15 | public String handleAdminHome() { 16 | return "home_admin"; 17 | } 18 | 19 | @GetMapping("/user/home") 20 | public String handleUserHome() { 21 | return "home_user"; 22 | } 23 | 24 | @GetMapping("/login") 25 | public String handleLogin() { 26 | return "custom_login"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/controller/RegistrationController.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.controller; 2 | 3 | import com.genuinecoder.springboottest.model.MyUser; 4 | import com.genuinecoder.springboottest.model.MyUserRepository; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.security.crypto.password.PasswordEncoder; 8 | import org.springframework.web.ErrorResponse; 9 | import org.springframework.web.bind.annotation.ExceptionHandler; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RestController; 13 | import org.springframework.web.server.ServerErrorException; 14 | 15 | import java.time.Year; 16 | 17 | @RestController 18 | public class RegistrationController { 19 | 20 | private final MyUserRepository myUserRepository; 21 | private final PasswordEncoder passwordEncoder; 22 | 23 | public RegistrationController(MyUserRepository myUserRepository, PasswordEncoder passwordEncoder) { 24 | this.myUserRepository = myUserRepository; 25 | this.passwordEncoder = passwordEncoder; 26 | } 27 | 28 | @PostMapping("/register/user") 29 | public MyUser createUser(@RequestBody MyUser user) { 30 | user.setPassword(passwordEncoder.encode(user.getPassword())); 31 | return myUserRepository.save(user); 32 | } 33 | 34 | @ExceptionHandler(IllegalArgumentException.class) 35 | public ResponseEntity handleException(IllegalArgumentException exp) { 36 | return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) 37 | .body(new ServerErrorException("NPE", exp)); 38 | } 39 | 40 | int getYear() { 41 | return Year.now().getValue(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/model/MyUser.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | 8 | @Entity 9 | public class MyUser { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.AUTO) 13 | private Long id; 14 | private String username; 15 | private String password; 16 | private String role; //Eg: ADMIN,USER 17 | 18 | public Long getId() { 19 | return id; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public String getUsername() { 27 | return username; 28 | } 29 | 30 | public void setUsername(String username) { 31 | this.username = username; 32 | } 33 | 34 | public String getPassword() { 35 | return password; 36 | } 37 | 38 | public void setPassword(String password) { 39 | this.password = password; 40 | } 41 | 42 | public String getRole() { 43 | return role; 44 | } 45 | 46 | public void setRole(String role) { 47 | this.role = role; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/model/MyUserDetailService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.model; 2 | 3 | import org.springframework.security.core.userdetails.User; 4 | import org.springframework.security.core.userdetails.UserDetails; 5 | import org.springframework.security.core.userdetails.UserDetailsService; 6 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.Optional; 10 | 11 | @Service 12 | public class MyUserDetailService implements UserDetailsService { 13 | 14 | private final MyUserRepository repository; 15 | 16 | public MyUserDetailService(MyUserRepository repository) { 17 | this.repository = repository; 18 | } 19 | 20 | @Override 21 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 22 | Optional user = repository.findByUsername(username); 23 | if (user.isPresent()) { 24 | var userObj = user.get(); 25 | return User.builder() 26 | .username(userObj.getUsername()) 27 | .password(userObj.getPassword()) 28 | .roles(getRoles(userObj)) 29 | .build(); 30 | } else { 31 | throw new UsernameNotFoundException(username); 32 | } 33 | } 34 | 35 | private String[] getRoles(MyUser user) { 36 | if (user.getRole() == null) { 37 | return new String[]{"USER"}; 38 | } 39 | return user.getRole().split(","); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/model/MyUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.model; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import java.util.Optional; 6 | 7 | public interface MyUserRepository extends JpaRepository { 8 | 9 | Optional findByUsername(String username); 10 | } 11 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/service/DiscountService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | import java.time.Year; 6 | 7 | @Service 8 | public class DiscountService { 9 | 10 | public float calculateDiscount(float amount, String promoCode) { 11 | if (promoCode == null) { 12 | return 0; 13 | } 14 | if (promoCode.equals("THANKSGIVING")) { 15 | return amount * 0.1f; 16 | } 17 | if (promoCode.equals("XMAS") && getCurrentYear().getValue() == 2025) { 18 | return amount * 0.25f; 19 | } 20 | return 0; 21 | } 22 | 23 | Year getCurrentYear() { 24 | return Year.now(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/java/com/genuinecoder/springboottest/service/StringProcessorService.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | @Service 6 | public class StringProcessorService { 7 | 8 | public boolean isPalindrome(String input) { 9 | String reversed = new StringBuilder(input).reverse().toString(); 10 | return reversed.equals(input); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/tutorial?createDatabaseIfNotExist=true 2 | spring.datasource.username=root 3 | spring.datasource.password=root 4 | 5 | spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect 6 | spring.jpa.hibernate.ddl-auto=update -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/error/403.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 403 Forbidden 7 | 31 | 32 | 33 |
34 |

403 Forbidden

35 |

Sorry, you don't have permission to access this resource.

36 |
37 | 38 | 39 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/error/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 404 Not Found 7 | 31 | 32 | 33 |
34 |

404 Not Found

35 |

Sorry, the page you are looking for might be in another castle.

36 |
37 | 38 | 39 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/templates/custom_login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Login 7 | 58 | 59 | 60 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring Boot Security Tutorial 7 | 53 | 54 | 55 |
56 |

Spring Boot Security Tutorial

57 |

Welcome to the open section of the tutorial. Explore and learn about Spring Boot Security.

58 |
59 | 60 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/templates/home_admin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Admin Home - Spring Boot Security Tutorial 7 | 40 | 41 | 42 |
43 |

Welcome, Admin!

44 |

This is the admin home page. Explore and manage your admin functionalities.

45 |
46 | 47 | -------------------------------------------------------------------------------- /LearnUnitTest/src/main/resources/templates/home_user.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | User Home - Spring Boot Security Tutorial 7 | 40 | 41 | 42 |
43 |

Welcome, User!

44 |

This is the user home page. Explore and access your user-specific features.

45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /LearnUnitTest/src/test/java/com/genuinecoder/springboottest/controller/ContentControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.controller; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.security.test.context.support.WithMockUser; 8 | import org.springframework.test.web.servlet.MockMvc; 9 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 10 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 11 | 12 | @SpringBootTest 13 | @AutoConfigureMockMvc 14 | public class ContentControllerTest { 15 | 16 | @Autowired 17 | private MockMvc mockMvc; 18 | 19 | @Test 20 | public void testHomeAccessible_WithoutLogin() throws Exception { 21 | mockMvc.perform(MockMvcRequestBuilders.get("/home")) 22 | .andExpect(MockMvcResultMatchers.status().is(200)); 23 | } 24 | 25 | @Test 26 | public void testUserHomeNotAccessible_WithoutLogin() throws Exception { 27 | mockMvc.perform(MockMvcRequestBuilders.get("/user/home")) 28 | .andExpect(MockMvcResultMatchers.status().is(302)); 29 | } 30 | 31 | @Test 32 | @WithMockUser(username = "tester", roles = "USER") 33 | public void testUserHomeAccessible_WithLogin() throws Exception { 34 | mockMvc.perform(MockMvcRequestBuilders.get("/user/home")) 35 | .andExpect(MockMvcResultMatchers.status().is(200)); 36 | } 37 | 38 | @Test 39 | public void testAdminHomeNotAccessible_WithoutLogin() throws Exception { 40 | mockMvc.perform(MockMvcRequestBuilders.get("/admin/home")) 41 | .andExpect(MockMvcResultMatchers.status().is(302)); 42 | } 43 | 44 | @Test 45 | @WithMockUser(username = "tester", roles = "ADMIN") 46 | public void testAdminHomeAccessible_WithAdminLogin() throws Exception { 47 | mockMvc.perform(MockMvcRequestBuilders.get("/admin/home")) 48 | .andExpect(MockMvcResultMatchers.status().is(200)); 49 | } 50 | 51 | @Test 52 | @WithMockUser(username = "tester", roles = "USER") 53 | public void testAdminHomeNotAccessible_WithUserLogin() throws Exception { 54 | mockMvc.perform(MockMvcRequestBuilders.get("/admin/home")) 55 | .andExpect(MockMvcResultMatchers.status().is(403)); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /LearnUnitTest/src/test/java/com/genuinecoder/springboottest/service/DiscountServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.service; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | import org.mockito.Mockito; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.boot.test.mock.mockito.SpyBean; 8 | 9 | import java.time.Year; 10 | 11 | @SpringBootTest 12 | public class DiscountServiceTest { 13 | 14 | @SpyBean 15 | private DiscountService discountService; 16 | 17 | @Test 18 | public void testCalculateDiscount_ValidPromoCode() { 19 | var discount = discountService.calculateDiscount(10, "THANKSGIVING"); 20 | Assertions.assertEquals(1f, discount); 21 | } 22 | 23 | @Test 24 | public void testCalculateDiscount_ValidPromoCodeForYear2025() { 25 | Mockito.when(discountService.getCurrentYear()).thenReturn(Year.of(2025)); 26 | var discount = discountService.calculateDiscount(20, "XMAS"); 27 | Assertions.assertEquals(5f, discount); 28 | } 29 | 30 | @Test 31 | public void testCalculateDiscount_ValidPromoCodeForYear2024() { 32 | Mockito.when(discountService.getCurrentYear()).thenReturn(Year.of(2024)); 33 | var discount = discountService.calculateDiscount(20, "XMAS"); 34 | Assertions.assertEquals(0f, discount); 35 | } 36 | 37 | @Test 38 | public void testCalculateDiscount_NullPromoCode() { 39 | var discount = discountService.calculateDiscount(20, null); 40 | Assertions.assertEquals(0f, discount); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LearnUnitTest/src/test/java/com/genuinecoder/springboottest/service/StringProcessorServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.genuinecoder.springboottest.service; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | @SpringBootTest 9 | public class StringProcessorServiceTest { 10 | 11 | @Autowired 12 | private StringProcessorService stringProcessorService; 13 | 14 | @Test 15 | public void testIsPalindrome_ValidPalindrome() { 16 | var result = stringProcessorService.isPalindrome("malayalam"); 17 | Assertions.assertTrue(result); 18 | } 19 | 20 | @Test 21 | public void testIsPalindrome_InvalidPalindrome() { 22 | var result = stringProcessorService.isPalindrome("genuine-coder"); 23 | Assertions.assertFalse(result); 24 | } 25 | 26 | @Test 27 | public void testIsPalindrome_NullInput() { 28 | Assertions.assertThrows(NullPointerException.class, () -> { 29 | stringProcessorService.isPalindrome(null); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LearnUnitTest/src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:tutorial 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password=password 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect --------------------------------------------------------------------------------