Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Postman Collections/Recipe Sharing.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"info": {
"_postman_id": "60fbf79d-b642-4fda-ade9-6b05f031ffde",
"name": "Recipe Sharing",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "21114780"
},
"item": [
{
"name": "Recipe File by Recipe name",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/v1/recipe/banana",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"recipe",
"banana"
]
}
},
"response": []
},
{
"name": "Recipe names by tag",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/v1/request-recipe/tag/apple",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"request-recipe",
"tag",
"apple"
]
}
},
"response": []
},
{
"name": "All Recipe names",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/v1/available-recipes",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"available-recipes"
]
}
},
"response": []
},
{
"name": "Upload recipe",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "recipeName",
"value": "Pasta Salad",
"type": "text"
},
{
"key": "recipeTags",
"value": "Pasta, Salad, Peas, Healthy, Olive Oil",
"type": "text"
},
{
"key": "recipe",
"type": "file",
"src": "/Users/vibhuti03/Desktop/Pasta Salad.pdf"
}
]
},
"url": {
"raw": "http://localhost:8080/api/v1/recipe-intake",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"v1",
"recipe-intake"
]
}
},
"response": []
}
]
}
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -41,6 +45,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/vibhuti/recipeSharing/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.vibhuti.recipeSharing.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
public class SecurityConfig {

@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}

@Bean
public UserDetailsService userDetailsService(){
UserDetails adminUser = User.withUsername("adminUser").password(passwordEncoder().encode("password"))
.roles("ADMIN","AUTHORIZED").build();
UserDetails authorizedUser = User.withUsername("authUser").password(passwordEncoder().encode("password"))
.roles("AUTHORIZED").build();

return new InMemoryUserDetailsManager(adminUser,authorizedUser);
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf((csrf) -> csrf.disable())
.authorizeHttpRequests((authorizeHttpRequests) -> authorizeHttpRequests
.requestMatchers("/api/v1/recipe-intake").hasRole("AUTHORIZED")
.requestMatchers("/api/v1/recipe/**").permitAll()
.requestMatchers("/api/v1/available-recipes").permitAll()
.requestMatchers("/api/v1/request-recipe/**").permitAll()
.anyRequest().authenticated())
.formLogin(withDefaults());

return httpSecurity.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RecipeController {

@Autowired
private final FetchRecipe fetchRecipe;
@PostMapping("recipe-intake")
@PostMapping("/recipe-intake")
public ResponseEntity<Object> recipeIntake(@RequestParam("recipeName") String recipeName,
@RequestParam("recipeTags") List<String> recipeTags,
@RequestParam("recipe") MultipartFile recipeFile) throws IOException {
Expand All @@ -38,13 +38,13 @@ public ResponseEntity<Object> recipeIntake(@RequestParam("recipeName") String re

}

@GetMapping("available-recipes")
@GetMapping("/available-recipes")
public ResponseEntity<List<String>> availableRecipes(){
List<String> recipeNames = fetchRecipe.fetchAllRecipe();
return ResponseEntity.ok().body(recipeNames);
}

@GetMapping("request-recipe/tag/{recipeTag}")
@GetMapping("/request-recipe/tag/{recipeTag}")
public ResponseEntity<List<String>> recipesByTag(@PathVariable String recipeTag){
List<String> recipeNames = fetchRecipe.fetchRecipesByTag(recipeTag);
return ResponseEntity.ok().body(recipeNames);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ spring.datasource.password=lilly123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=update