Blazej 4 лет назад
Родитель
Сommit
c93a3f52c7

+ 6 - 8
pom.xml

@@ -13,9 +13,6 @@
     <version>0.0.1-SNAPSHOT</version>
     <version>0.0.1-SNAPSHOT</version>
     <name>PlantsForYou</name>
     <name>PlantsForYou</name>
     <description>PlantsForYou</description>
     <description>PlantsForYou</description>
-    <properties>
-        <java.version>11</java.version>
-    </properties>
     <dependencies>
     <dependencies>
         <dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <groupId>org.springframework.boot</groupId>
@@ -39,6 +36,12 @@
             <artifactId>postgresql</artifactId>
             <artifactId>postgresql</artifactId>
             <scope>runtime</scope>
             <scope>runtime</scope>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <version>3.8.1</version>
+            <type>maven-plugin</type>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <artifactId>lombok</artifactId>
@@ -64,11 +67,6 @@
             <artifactId>java-jwt</artifactId>
             <artifactId>java-jwt</artifactId>
             <version>3.19.1</version>
             <version>3.19.1</version>
         </dependency>
         </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
     </dependencies>
     </dependencies>
 
 
     <build>
     <build>

+ 4 - 0
src/main/java/com/example/plantsforyou/plant/PlantController.java

@@ -22,4 +22,8 @@ public class PlantController {
         return plantService.getAllPlants();
         return plantService.getAllPlants();
     }
     }
 
 
+    @GetMapping(path = "no-auth") //ONLY FOR TESTING
+    public List<Plant> getPlantsNoAuth(){
+        return plantService.getAllPlants();
+    }
 }
 }

+ 0 - 4
src/main/java/com/example/plantsforyou/plant/PlantManagementController.java

@@ -24,16 +24,12 @@ public class PlantManagementController {
 
 
     @PostMapping
     @PostMapping
     public ResponseEntity addNewPlant(@RequestBody Plant plant){
     public ResponseEntity addNewPlant(@RequestBody Plant plant){
-        if(plantService.getAllPlants().contains(plant))
-            return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
         plantService.addPlant(plant);
         plantService.addPlant(plant);
         return new ResponseEntity(HttpStatus.OK);
         return new ResponseEntity(HttpStatus.OK);
     }
     }
 
 
     @DeleteMapping(path = "{plantID}")
     @DeleteMapping(path = "{plantID}")
     public ResponseEntity deletePlant(@PathVariable("plantID") Long plantID){
     public ResponseEntity deletePlant(@PathVariable("plantID") Long plantID){
-        if(plantService.findPlantById(plantID).isEmpty())
-            return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
         plantService.delete(plantID);
         plantService.delete(plantID);
         return new ResponseEntity(HttpStatus.OK);
         return new ResponseEntity(HttpStatus.OK);
     }
     }

+ 20 - 2
src/main/java/com/example/plantsforyou/security/config/WebSecurityConfig.java

@@ -15,6 +15,13 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.CorsConfigurationSource;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+
+
+import java.util.Arrays;
+import java.util.Collections;
 
 
 import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
 import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
 
 
@@ -28,14 +35,25 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
 
     @Override
     @Override
     protected void configure(HttpSecurity http) throws Exception {
     protected void configure(HttpSecurity http) throws Exception {
+        http.cors();
         http.csrf().disable();
         http.csrf().disable();
         http.sessionManagement().sessionCreationPolicy(STATELESS);
         http.sessionManagement().sessionCreationPolicy(STATELESS);
-        http.authorizeRequests().antMatchers("/api/v*/registration/**", "/api/v*/users/token/refresh/**").permitAll();
+        http.authorizeRequests().antMatchers("/api/v*/registration/**", "/api/v*/users/token/refresh/**", "/api/v*/plants/no-auth").permitAll();
         http.authorizeRequests().antMatchers("/api/v*/**").hasAnyAuthority("USER");
         http.authorizeRequests().antMatchers("/api/v*/**").hasAnyAuthority("USER");
         http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
         http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
         http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
         http.addFilterBefore(new CustomAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
     }
     }
 
 
+    @Bean
+    CorsConfigurationSource corsConfigurationSource() {
+        CorsConfiguration configuration = new CorsConfiguration();
+        configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
+        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", configuration);
+        return source;
+    }
+
     @Bean
     @Bean
     @Override
     @Override
     public AuthenticationManager authenticationManagerBean() throws Exception {
     public AuthenticationManager authenticationManagerBean() throws Exception {
@@ -43,7 +61,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
     }
     }
 
 
     @Override
     @Override
-    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+    protected void configure(AuthenticationManagerBuilder auth){
         auth.authenticationProvider(daoAuthenticationProvider());
         auth.authenticationProvider(daoAuthenticationProvider());
     }
     }
     @Bean
     @Bean