Skip to content

Commit 9d3a36f

Browse files
committed
begin separate users
1 parent da75341 commit 9d3a36f

29 files changed

+768
-2
lines changed
File renamed without changes.

backend/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
<groupId>org.projectx</groupId>
1010
<artifactId>projectx</artifactId>
11-
<version>1.0.1</version>
11+
<version>2.0.0</version>
1212
<packaging>pom</packaging>
1313

1414
<modules>
1515
<module>api-server</module>
16-
<module>auth-server</module>
16+
<module>auth-service</module>
17+
<module>user-service</module>
1718
<module>discovery-service</module>
1819
</modules>
1920
</project>

backend/user-service/pom.xml

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
7+
<modelVersion>4.0.0</modelVersion>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>3.1.3</version>
13+
<relativePath />
14+
</parent>
15+
16+
<properties>
17+
<java.version>17</java.version>
18+
<maven.compiler.source>${java.version}</maven.compiler.source>
19+
<maven.compiler.target>${java.version}</maven.compiler.target>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
</properties>
22+
23+
<groupId>org.projectx</groupId>
24+
<artifactId>api-server</artifactId>
25+
<version>2.0.0</version>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-security</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-docker-compose</artifactId>
47+
<scope>runtime</scope>
48+
<optional>true</optional>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
<scope>provided</scope>
54+
<optional>true</optional>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-test</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.security</groupId>
63+
<artifactId>spring-security-test</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.cloud</groupId>
69+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
70+
</dependency>
71+
</dependencies>
72+
73+
<dependencyManagement>
74+
<dependencies>
75+
<dependency>
76+
<groupId>org.springframework.cloud</groupId>
77+
<artifactId>spring-cloud-dependencies</artifactId>
78+
<version>2022.0.4</version>
79+
<type>pom</type>
80+
<scope>import</scope>
81+
</dependency>
82+
</dependencies>
83+
</dependencyManagement>
84+
85+
<build>
86+
<plugins>
87+
<plugin>
88+
<groupId>org.springframework.boot</groupId>
89+
<artifactId>spring-boot-maven-plugin</artifactId>
90+
<configuration>
91+
<excludes>
92+
<exclude>
93+
<groupId>org.projectlombok</groupId>
94+
<artifactId>lombok</artifactId>
95+
</exclude>
96+
</excludes>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.projectx.api;
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 ApiApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(ApiApplication.class, args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.projectx.api.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import org.springframework.security.config.Customizer;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10+
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.security.web.SecurityFilterChain;
12+
import org.springframework.web.cors.CorsConfiguration;
13+
import org.springframework.web.cors.CorsConfigurationSource;
14+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
15+
16+
import java.util.List;
17+
18+
@Configuration
19+
@EnableWebSecurity
20+
public class SecurityConfig {
21+
@Bean
22+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
23+
return http
24+
.cors(c -> this.corsConfigurationSource())
25+
.csrf(c -> c.disable())
26+
.authorizeHttpRequests(c -> c.requestMatchers("/user/**", "/home/**", "/measurements/**").permitAll().anyRequest().authenticated())
27+
.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()))
28+
.build();
29+
}
30+
31+
@Bean
32+
public PasswordEncoder passwordEncoder() {
33+
return new BCryptPasswordEncoder();
34+
}
35+
36+
@Bean
37+
public CorsConfigurationSource corsConfigurationSource() {
38+
CorsConfiguration corsConfig = new CorsConfiguration();
39+
40+
corsConfig.setAllowedOrigins(List.of("*"));
41+
corsConfig.setAllowedMethods(List.of("*"));
42+
corsConfig.setAllowedHeaders(List.of("*"));
43+
44+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
45+
46+
source.registerCorsConfiguration("/**", corsConfig);
47+
48+
return source;
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.projectx.api.controller;
2+
3+
import org.projectx.api.model.Device;
4+
import org.projectx.api.request.DeviceRequest;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
import org.projectx.api.service.DeviceService;
8+
9+
import java.security.Principal;
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("/devices")
14+
public class DeviceController {
15+
16+
private final DeviceService deviceService;
17+
18+
public DeviceController(DeviceService deviceService) {
19+
this.deviceService = deviceService;
20+
}
21+
22+
@GetMapping
23+
public List<Device> allDevices(Principal principal) {
24+
return this.deviceService.getAllByUser(principal);
25+
}
26+
27+
@GetMapping("/{id}")
28+
public Device getDevice(@PathVariable String id) {
29+
return this.deviceService.getById(id);
30+
}
31+
32+
@PostMapping("/create")
33+
public Device createDevice(@RequestBody DeviceRequest request) {
34+
return this.deviceService.create(request);
35+
}
36+
37+
@PostMapping("/activate/{id}")
38+
public Device activateDevice(@PathVariable String id) {
39+
return this.deviceService.activate(id);
40+
}
41+
42+
@PostMapping("/update/{id}")
43+
public Device updateDevice(@PathVariable String id, @RequestBody DeviceRequest request) {
44+
return this.deviceService.update(id, request);
45+
}
46+
47+
@DeleteMapping("/delete/{id}")
48+
public ResponseEntity deleteDevice(@PathVariable String id) {
49+
return this.deviceService.delete(id);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.projectx.api.controller;
2+
3+
import org.projectx.api.model.Device;
4+
import org.projectx.api.service.DeviceService;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("/home")
14+
public class HomeController {
15+
16+
private final DeviceService deviceService;
17+
18+
public HomeController(DeviceService deviceService) {
19+
this.deviceService = deviceService;
20+
}
21+
22+
@GetMapping
23+
public List<Device> index() {
24+
return this.deviceService.getAllActivated();
25+
}
26+
27+
@GetMapping("/stats")
28+
public ResponseEntity stats() {
29+
return ResponseEntity.ok(this.deviceService.getAllActivated());
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.projectx.api.controller;
2+
3+
import org.projectx.api.repository.DeviceRepository;
4+
import org.projectx.api.repository.MeasurementRepository;
5+
import org.projectx.api.model.Measurement;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.time.LocalDateTime;
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("/measurements")
13+
public class MeasurementController {
14+
private final MeasurementRepository measurementRepository;
15+
16+
private final DeviceRepository deviceRepository;
17+
18+
public MeasurementController(MeasurementRepository measurementRepository, DeviceRepository deviceRepository) {
19+
this.measurementRepository = measurementRepository;
20+
this.deviceRepository = deviceRepository;
21+
}
22+
23+
@GetMapping("/{deviceId}")
24+
public List<Measurement> getMeasurements(@PathVariable String deviceId) {
25+
return this.measurementRepository.findAllByDevice(deviceId);
26+
}
27+
28+
@PutMapping("/{deviceId}")
29+
public Measurement putMeasurement(@PathVariable String deviceId, @RequestBody Measurement measurement, @RequestHeader("X-API-KEY") String apiToken) {
30+
if (!this.deviceRepository.existsById(deviceId)){
31+
throw new RuntimeException("Device with id=%s dont exist".formatted(deviceId));
32+
}
33+
34+
if (apiToken != "fake-api-key") {
35+
throw new RuntimeException("API key is not valid");
36+
}
37+
38+
measurement.setDevice(deviceId);
39+
measurement.setTimestamp(LocalDateTime.now());
40+
41+
return this.measurementRepository.save(measurement);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.projectx.api.controller;
2+
3+
import lombok.Data;
4+
import org.projectx.api.model.User;
5+
import org.projectx.api.repository.UserRepository;
6+
import org.projectx.api.request.CreateUserRequest;
7+
import org.springframework.http.HttpStatusCode;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
10+
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.web.bind.annotation.*;
12+
13+
import java.time.LocalDateTime;
14+
import java.util.List;
15+
16+
@RestController
17+
public class UserController {
18+
private final PasswordEncoder passwordEncoder;
19+
20+
private final UserRepository userRepository;
21+
22+
public UserController(PasswordEncoder passwordEncoder, UserRepository userRepository) {
23+
this.passwordEncoder = passwordEncoder;
24+
this.userRepository = userRepository;
25+
}
26+
27+
@GetMapping("/user/{username}")
28+
public UserResponse user(@PathVariable String username) {
29+
User user = this.userRepository
30+
.findByUsername(username)
31+
.orElseThrow(() -> new UsernameNotFoundException("User with %s username is not found".formatted(username)));
32+
33+
UserResponse response = new UserResponse();
34+
35+
response.setEmail(user.getEmail());
36+
response.setUsername(user.getUsername());
37+
response.setPassword(user.getPassword());
38+
response.setRoles(user.getAuthorities().stream().map(role -> role.getAuthority()).toList());
39+
40+
return response;
41+
}
42+
43+
@PostMapping("/user/create")
44+
public ResponseEntity create(@RequestBody CreateUserRequest request) {
45+
User user = new User();
46+
47+
user.setEmail(request.getEmail());
48+
user.setUsername(request.getUsername());
49+
user.setPassword(this.passwordEncoder.encode(request.getPassword()));
50+
user.setCreated(LocalDateTime.now());
51+
52+
return ResponseEntity.status(HttpStatusCode.valueOf(201)).body(this.userRepository.save(user));
53+
}
54+
55+
@Data
56+
public class UserResponse {
57+
private String username;
58+
59+
private String password;
60+
61+
private String email;
62+
63+
private List<String> roles;
64+
}
65+
}

0 commit comments

Comments
 (0)