瀏覽代碼

Authorization

dagarcos 2 年之前
父節點
當前提交
24df2b0d97
共有 24 個文件被更改,包括 775 次插入87 次删除
  1. 32 3
      DBCDS_S13_1_Auth/pom.xml
  2. 22 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/DbcdsS131AuthApplication.java
  3. 106 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/controllers/AuthorizationController.java
  4. 43 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/AuthenticationManager.java
  5. 68 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/CustomAuthorizationFilter.java
  6. 48 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/SecurityContextRepository.java
  7. 59 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/WebConfigSecurity.java
  8. 35 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/CustomUserDetailsService.java
  9. 90 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/JwtService.java
  10. 45 0
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/UserService.java
  11. 0 13
      DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/config/DbcdsS131AuthApplication.java
  12. 3 1
      DBCDS_S13_1_Auth/src/main/resources/application.properties
  13. 0 13
      DBCDS_S13_1_Auth/src/test/java/es/uv/garcosda/config/DbcdsS131AuthApplicationTests.java
  14. 13 3
      DBCDS_S13_1_Config/src/main/resources/config/api-gateway.properties
  15. 13 0
      DBCDS_S13_1_Config/src/main/resources/config/auth-service.properties
  16. 1 0
      DBCDS_S13_1_Config/src/main/resources/config/mail-service.properties
  17. 13 2
      DBCDS_S13_1_Data/src/main/java/es/uv/garcosda/data/domain/User.java
  18. 51 50
      DBCDS_S13_1_Data/src/main/resources/data.sql
  19. 9 0
      DBCDS_S13_1_Gateway/src/main/java/es/uv/garcosda/gateway/config/WebConfig.java
  20. 73 0
      DBCDS_S13_1_Gateway/src/main/java/es/uv/garcosda/gateway/security/AuthFilter.java
  21. 37 0
      DBCDS_S13_1_Shared/src/main/java/es/uv/garcosda/shared/domain/AuthenticationRequest.java
  22. 14 2
      DBCDS_S13_1_Shared/src/main/java/es/uv/garcosda/shared/domain/User.java
  23. 二進制
      DBCDS_S13_1_Shared/target/classes/es/uv/garcosda/shared/domain/AuthenticationRequest.class
  24. 二進制
      DBCDS_S13_1_Shared/target/classes/es/uv/garcosda/shared/domain/User.class

+ 32 - 3
DBCDS_S13_1_Auth/pom.xml

@@ -15,11 +15,12 @@
 	<description>SpringCloud - III</description>
 	<properties>
 		<java.version>17</java.version>
+		<spring-cloud.version>2022.0.2</spring-cloud.version>
 	</properties>
 	<dependencies>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter-web</artifactId>
+			<artifactId>spring-boot-starter-webflux</artifactId>
 		</dependency>
 
 		<dependency>
@@ -29,11 +30,39 @@
 			<optional>true</optional>
 		</dependency>
 		<dependency>
+		   <groupId>org.springframework.cloud</groupId>
+		   <artifactId>spring-cloud-starter-config</artifactId>
+		</dependency>
+		<dependency>
+		    <groupId>org.springframework.cloud</groupId>
+		    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
+		</dependency>
+		<dependency>
 			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter-test</artifactId>
-			<scope>test</scope>
+			<artifactId>spring-boot-starter-security</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.auth0</groupId>
+			<artifactId>java-jwt</artifactId>
+			<version>3.18.1</version>
+		</dependency>
+		<dependency>
+			<groupId>es.uv.garcosda.shared</groupId>
+		    <artifactId>DBCDS_S13_1_Shared</artifactId>
+		    <version>0.0.1-SNAPSHOT</version>
 		</dependency>
 	</dependencies>
+	<dependencyManagement>
+		<dependencies>
+			<dependency>
+				<groupId>org.springframework.cloud</groupId>
+				<artifactId>spring-cloud-dependencies</artifactId>
+				<version>${spring-cloud.version}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
 
 	<build>
 		<plugins>

+ 22 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/DbcdsS131AuthApplication.java

@@ -0,0 +1,22 @@
+package es.uv.garcosda.auth;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@SpringBootApplication
+public class DbcdsS131AuthApplication {
+	
+	@Bean
+	@LoadBalanced
+	public WebClient.Builder getWebClientBuilder() {
+		return WebClient.builder();
+	}
+
+	public static void main(String[] args) {
+		SpringApplication.run(DbcdsS131AuthApplication.class, args);
+	}
+
+}

+ 106 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/controllers/AuthorizationController.java

@@ -0,0 +1,106 @@
+package es.uv.garcosda.auth.controllers;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.server.ServerWebExchange;
+
+import es.uv.garcosda.auth.services.JwtService;
+import es.uv.garcosda.auth.services.UserService;
+import es.uv.garcosda.shared.domain.AuthenticationRequest;
+import reactor.core.publisher.Mono;
+
+
+@RestController
+@RequestMapping("/auth")
+public class AuthorizationController {
+
+	@Autowired
+	private PasswordEncoder pe;
+	
+	@Autowired
+	private UserService us;
+	
+	@Autowired
+	private JwtService tp;
+	
+	@GetMapping("authorize")
+	public Mono<ResponseEntity<?>> validate(ServerWebExchange exchange){
+		String header = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
+		String token = tp.getTokenFromHeader(header);
+		try {
+			if(!tp.isTokenExpired(tp.getTokenFromHeader(header))) {
+				return Mono.just(ResponseEntity.status(HttpStatus.ACCEPTED).body(tp.getUsernameFromToken(token)));
+			}
+			else {
+				return Mono.just(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid token"));
+			}
+		}
+		catch(Exception e){
+			return Mono.just(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid token"));
+		}	
+	}
+	
+	@PostMapping("authenticate")
+    public Mono<ResponseEntity<?>> login(@RequestBody AuthenticationRequest auth) {
+		
+		return us.findByEmailMono(auth.getUsername())
+			     .map(user -> {
+							if (pe.matches(auth.getPassword(), user.getPassword())) {
+								Map<String, String> tokens = new HashMap<>();
+								String accessToken = this.tp.generateAccessToken(user.getEmail(), Arrays.asList("USER"));
+								String refreshToken = this.tp.generateRefreshToken(user.getEmail(), Arrays.asList("USER"));
+								tokens.put("access_token", accessToken);
+								tokens.put("refresh_token", refreshToken);
+								HttpHeaders headers = new HttpHeaders();
+							    headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
+							    headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
+								return ResponseEntity.ok()
+													 .headers(headers)
+													 .body(tokens);
+							} 
+							else {
+								return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
+							}
+				}).defaultIfEmpty(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
+	}
+	
+	@PostMapping("refresh")
+    public Mono<ResponseEntity<?>> refresh(ServerWebExchange exchange) {
+		String header = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
+		try {
+			String refreshToken = tp.getTokenFromHeader(header);
+			if(!tp.isTokenExpired(tp.getTokenFromHeader(header))) {
+				String accessToken = this.tp.generateAccessToken(tp.getUsernameFromToken(refreshToken), Arrays.asList(tp.getRolesFromToken(refreshToken)));
+				HttpHeaders headers = new HttpHeaders();
+			    headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
+			    headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
+			    Map<String, String> tokens = new HashMap<>();
+			    tokens.put("access_token", accessToken);
+				tokens.put("refresh_token", refreshToken);
+				return Mono.just(ResponseEntity.ok()
+						                       .headers(headers)
+						                       .body(tokens));
+			}
+			else {
+				return Mono.just(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid refresh token"));
+			}
+		}
+		catch(Exception e){
+			return Mono.just(ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid refresh token"));
+		}
+	}
+	
+}

+ 43 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/AuthenticationManager.java

@@ -0,0 +1,43 @@
+package es.uv.garcosda.auth.security;
+
+import java.util.Collection;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.ReactiveAuthenticationManager;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.stereotype.Component;
+
+import es.uv.garcosda.auth.services.JwtService;
+import reactor.core.publisher.Mono;
+
+@Component
+public class AuthenticationManager implements ReactiveAuthenticationManager {
+
+	@Autowired
+	private JwtService tp;
+
+	@Override
+	public Mono<Authentication> authenticate(Authentication authentication) {
+		String token = authentication.getCredentials().toString();
+		String username;
+		try {
+			username = tp.getUsernameFromToken(token);
+		} 
+		catch (Exception e) {
+			username = null;
+		}
+		if (username != null && !tp.isTokenExpired(token)) {
+			Collection<SimpleGrantedAuthority> authorities = tp.getAuthoritiesFromToken(token);
+			UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(username, username, authorities);
+			SecurityContextHolder.getContext().setAuthentication(auth);
+			return Mono.just(auth);
+		} 
+		else {
+			return Mono.empty();
+		}
+	}
+}
+

+ 68 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/CustomAuthorizationFilter.java

@@ -0,0 +1,68 @@
+package es.uv.garcosda.auth.security;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+
+import org.springframework.http.MediaType;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import es.uv.garcosda.auth.services.JwtService;
+
+import static org.springframework.http.HttpHeaders.AUTHORIZATION;
+
+public class CustomAuthorizationFilter extends OncePerRequestFilter {
+
+	private JwtService jwtService;
+	
+	public CustomAuthorizationFilter(JwtService jwtService) {
+		this.jwtService = jwtService;
+	}
+	
+	@Override
+	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+		if(request.getServletPath().equals("/api/v1/login")) {
+			filterChain.doFilter(request, response);
+		}
+		else {		
+			String authHeader = request.getHeader(AUTHORIZATION);
+			if(authHeader != null && authHeader.startsWith("Bearer ")) {
+				try {
+					String token = jwtService.getTokenFromHeader(authHeader);
+					String username = jwtService.getUsernameFromToken(token);		
+					
+					Collection<SimpleGrantedAuthority> authorities = jwtService.getAuthoritiesFromToken(token);
+					UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, authorities);
+					SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+					filterChain.doFilter(request, response);
+				}
+				catch(Exception exception) {
+					response.setHeader("error", exception.getMessage());
+					response.setStatus(403);
+					System.out.println(403);
+					Map<String, String> error = new HashMap<>();
+					error.put("error_msg", exception.getMessage());
+					response.setContentType(MediaType.APPLICATION_JSON_VALUE);
+					new ObjectMapper().writeValue(response.getOutputStream(), error);
+				}
+			}
+			else {
+				filterChain.doFilter(request, response);
+			}
+		}
+	}
+
+	
+	
+}

+ 48 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/SecurityContextRepository.java

@@ -0,0 +1,48 @@
+package es.uv.garcosda.auth.security;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextImpl;
+import org.springframework.security.web.server.context.ServerSecurityContextRepository;
+import org.springframework.stereotype.Component;
+import org.springframework.web.server.ServerWebExchange;
+
+import es.uv.garcosda.auth.services.JwtService;
+import reactor.core.publisher.Mono;
+
+@Component
+public class SecurityContextRepository implements ServerSecurityContextRepository {
+
+	@Autowired
+	private AuthenticationManager am;
+	@Autowired
+	private JwtService tp;
+
+	@Override
+	public Mono<Void> save(ServerWebExchange swe, SecurityContext sc) {
+		throw new UnsupportedOperationException("Not supported yet.");
+	}
+
+	@Override
+	public Mono<SecurityContext> load(ServerWebExchange swe) {
+		ServerHttpRequest request = swe.getRequest();
+		String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
+		String authToken = null;
+		if (header != null && header.startsWith(this.tp.getTokenHeaderPrefix())) {
+			authToken = this.tp.getTokenFromHeader(header);
+		}	
+		if (authToken != null) {
+			Authentication auth = new UsernamePasswordAuthenticationToken(authToken, authToken);
+			return this.am.authenticate(auth)
+					      .map((authentication) -> new SecurityContextImpl(authentication));
+		} 
+		else {
+			return Mono.empty();
+		}
+	}
+
+}

+ 59 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/security/WebConfigSecurity.java

@@ -0,0 +1,59 @@
+package es.uv.garcosda.auth.security;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpStatus;
+import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
+import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
+import org.springframework.security.config.web.server.ServerHttpSecurity;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.web.server.SecurityWebFilterChain;
+
+import reactor.core.publisher.Mono;
+
+@EnableReactiveMethodSecurity
+@EnableWebFluxSecurity
+@Configuration
+public class WebConfigSecurity {
+
+	@Autowired
+    private AuthenticationManager authenticationManager;
+
+    @Autowired
+    private SecurityContextRepository securityContextRepository;
+	
+	@Bean
+    public PasswordEncoder passwordEncoder() {
+        return new BCryptPasswordEncoder();
+    }
+			
+	@Bean
+	public SecurityWebFilterChain securityFilterChain(ServerHttpSecurity http) {
+		return http.csrf().disable()
+				   .formLogin().disable()
+				   .logout().disable()
+				   .authenticationManager(authenticationManager)
+	               .securityContextRepository(securityContextRepository)
+				   .authorizeExchange(exchanges -> exchanges
+					   .pathMatchers("/auth/authenticate").permitAll()
+					   .pathMatchers("/auth/authorize").permitAll()
+					   .pathMatchers("/auth/refresh").permitAll()
+					   .pathMatchers("/auth/test").permitAll()
+				       .anyExchange().authenticated()
+				   )
+				   .httpBasic()
+				   .and()
+               	   .exceptionHandling()
+               	       .authenticationEntryPoint((swe, e) -> Mono.fromRunnable(() -> {
+               			    swe.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
+               		    }))
+               	       .accessDeniedHandler((swe, e) -> Mono.fromRunnable(() -> {
+               			    swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
+               		    }))
+				   .and()
+				   .build();
+	}
+	
+}

+ 35 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/CustomUserDetailsService.java

@@ -0,0 +1,35 @@
+package es.uv.garcosda.auth.services;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.security.core.authority.AuthorityUtils;
+import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.stereotype.Service;
+
+import es.uv.garcosda.shared.domain.User;
+import reactor.core.publisher.Mono;
+
+@Service
+public class CustomUserDetailsService implements ReactiveUserDetailsService {
+	
+	@Autowired
+	UserService us;
+	
+	@Value("${mailer.data.url}")
+	private String data_api;
+
+    @Override
+    public Mono<UserDetails> findByUsername(String username) {
+    	
+    	User u = us.findByEmail(username);
+    	return Mono.just(this.toAuthUser(u));
+    	
+    }
+    
+    private org.springframework.security.core.userdetails.User toAuthUser(User u){
+    	return new org.springframework.security.core.userdetails.User(u.getEmail(), u.getPassword(), 
+				  AuthorityUtils.createAuthorityList("user"));
+    }
+    
+}

+ 90 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/JwtService.java

@@ -0,0 +1,90 @@
+package es.uv.garcosda.auth.services;
+
+import jakarta.annotation.PostConstruct;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
+import org.springframework.stereotype.Component;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.interfaces.DecodedJWT;
+
+@Component
+public class JwtService {
+
+	@Value("${sys.token.key}")
+	private String key;
+	
+	@Value("${sys.token.issuer}")
+	private String issuer;
+	
+	@Value("${sys.token.duration}")
+	private Integer duration;
+	
+	private Algorithm algorithm;
+	private JWTVerifier verifier;
+	
+	@PostConstruct
+	public void init(){
+		this.algorithm = Algorithm.HMAC256(this.key.getBytes());
+		this.verifier = JWT.require(this.algorithm).build();
+	}
+	
+	public String generateAccessToken(String username, List<String> claims) {
+		return JWT.create()
+				 .withSubject(username)
+				 .withExpiresAt(new Date(System.currentTimeMillis()+this.duration))
+				 .withIssuer(this.issuer)
+				 .withClaim("roles", claims)
+				 .sign(this.algorithm);
+	}
+	
+	public String generateRefreshToken(String username, List<String> claims) {
+		return JWT.create()
+				 .withSubject(username)
+				 .withExpiresAt(new Date(System.currentTimeMillis()+(this.duration*2)))
+				 .withIssuer(this.issuer)
+				 .withClaim("roles", claims)
+				 .sign(this.algorithm);
+	}
+	
+	public String getUsernameFromToken(String token) {
+		DecodedJWT decoded = this.verifier.verify(token);
+		return decoded.getSubject();
+	}
+	
+	public Collection<SimpleGrantedAuthority> getAuthoritiesFromToken(String token){
+		DecodedJWT decoded = this.verifier.verify(token);
+		String[] roles = decoded.getClaim("roles").asArray(String.class);
+		Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
+		for(String r : roles) { authorities.add(new SimpleGrantedAuthority(r)); }
+		return authorities;
+	}
+	
+	public String[] getRolesFromToken(String token){
+		DecodedJWT decoded = this.verifier.verify(token);
+		String[] roles = decoded.getClaim("roles").asArray(String.class);
+		return roles;
+	}
+	
+	public Boolean isTokenExpired(String token) {
+		DecodedJWT decoded = this.verifier.verify(token);
+        final Date expiration = decoded.getExpiresAt();
+        return expiration.before(new Date());
+    }
+	
+	public String getTokenFromHeader(String header) {
+		return header.substring(this.getTokenHeaderPrefix().length());
+	}
+	
+	public String getTokenHeaderPrefix() {
+		return "Bearer ";
+	}
+}

+ 45 - 0
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/auth/services/UserService.java

@@ -0,0 +1,45 @@
+package es.uv.garcosda.auth.services;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+import org.springframework.web.reactive.function.client.WebClient;
+
+import es.uv.garcosda.shared.domain.User;
+import reactor.core.publisher.Mono;
+
+@Service
+public class UserService {
+	
+	@Autowired
+	WebClient.Builder client;
+	
+	@Value("${mailer.data.url}")
+	private String data_api;
+
+	
+	public User findByEmail(String email) {
+		Mono<User> u = client.build().get()
+				 .uri(data_api+"/users/email/"+email)
+				 .retrieve()
+				 .bodyToMono(User.class)
+				 .switchIfEmpty(Mono.defer(() ->	
+					Mono.error(new UsernameNotFoundException("User Not Found"))));
+		
+		return u.block();
+	}
+	
+	public Mono<User> findByEmailMono(String email) {
+		Mono<User> u = client.build().get()
+				 .uri(data_api+"/users/email/"+email)
+				 .retrieve()
+				 .bodyToMono(User.class)
+				 .switchIfEmpty(Mono.defer(() ->	
+					Mono.error(new UsernameNotFoundException("User Not Found"))));
+		
+		return u;
+	}
+
+	
+}

+ 0 - 13
DBCDS_S13_1_Auth/src/main/java/es/uv/garcosda/config/DbcdsS131AuthApplication.java

@@ -1,13 +0,0 @@
-package es.uv.garcosda.config;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class DbcdsS131AuthApplication {
-
-	public static void main(String[] args) {
-		SpringApplication.run(DbcdsS131AuthApplication.class, args);
-	}
-
-}

+ 3 - 1
DBCDS_S13_1_Auth/src/main/resources/application.properties

@@ -1 +1,3 @@
-
+spring.application.name=auth-service
+spring.cloud.config.enabled=true
+spring.config.import=optional:configserver:http://127.0.0.1:8888

+ 0 - 13
DBCDS_S13_1_Auth/src/test/java/es/uv/garcosda/config/DbcdsS131AuthApplicationTests.java

@@ -1,13 +0,0 @@
-package es.uv.garcosda.config;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.boot.test.context.SpringBootTest;
-
-@SpringBootTest
-class DbcdsS131AuthApplicationTests {
-
-	@Test
-	void contextLoads() {
-	}
-
-}

+ 13 - 3
DBCDS_S13_1_Config/src/main/resources/config/api-gateway.properties

@@ -8,6 +8,11 @@ mailer.input.url=http://input-service/api/v1
 mailer.output.url=http://output-service/api/v1
 mailer.data.url=http://data-service/api/v1
 
+mailer.auth.name=auth-service
+mailer.auth.path=/auth
+mailer.mail.name=mail-service
+mailer.mail.path=/api/v1
+
 eureka.instance.hostname=localhost
 eureka.client.service-url.default-zone=http://127.0.0.1:8761/eureka
 
@@ -15,6 +20,11 @@ spring.cloud.gateway.discovery.locator.enabled=true
 eureka.instance.instance-id=${spring.application.name}:${random.uuid}
 spring.cloud.loadbalancer.ribbon.enabled=false
 
-spring.cloud.gateway.routes[0].id=mail-service
-spring.cloud.gateway.routes[0].uri=lb://mail-service
-spring.cloud.gateway.routes[0].predicates[0]=Path=/api/v1/**
+spring.cloud.gateway.routes[0].id=${mailer.mail.name}
+spring.cloud.gateway.routes[0].uri=lb://${mailer.mail.name}
+spring.cloud.gateway.routes[0].predicates[0]=Path=${mailer.mail.path}/**
+spring.cloud.gateway.routes[0].filters[0]=AuthFilter
+
+spring.cloud.gateway.routes[1].id=${mailer.auth.name}
+spring.cloud.gateway.routes[1].uri=lb://${mailer.auth.name}
+spring.cloud.gateway.routes[1].predicates[0]=Path=${mailer.auth.path}/**

+ 13 - 0
DBCDS_S13_1_Config/src/main/resources/config/auth-service.properties

@@ -0,0 +1,13 @@
+server.port=0
+
+mailer.input.url=http://input-service/api/v1
+mailer.output.url=http://output-service/api/v1
+mailer.data.url=http://data-service/api/v1
+
+eureka.instance.hostname=localhost
+eureka.client.service-url.default-zone=http://127.0.0.1:8761/eureka
+eureka.instance.instance-id=${spring.application.name}:${random.uuid}
+
+sys.token.issuer=News service
+sys.token.key=MySuperSecureEncriptedAndProtectedKey
+sys.token.duration=600000

+ 1 - 0
DBCDS_S13_1_Config/src/main/resources/config/mail-service.properties

@@ -7,6 +7,7 @@ server.port=0
 mailer.input.url=http://input-service/api/v1
 mailer.output.url=http://output-service/api/v1
 mailer.data.url=http://data-service/api/v1
+mailer.auth.url=http://auth-service/auth
 
 eureka.instance.hostname=localhost
 eureka.client.service-url.default-zone=http://127.0.0.1:8761/eureka

+ 13 - 2
DBCDS_S13_1_Data/src/main/java/es/uv/garcosda/data/domain/User.java

@@ -27,20 +27,23 @@ public class User  implements Serializable {
 	private String firstname;
 	private String lastname;
 	private String email;
+	private String password;
 	
 	public User() {}
 
-	public User(String email, String firstname, String lastname) {
+	public User(String email, String firstname, String lastname, String password) {
 		this.email = email;
 		this.firstname = firstname;
 		this.lastname = lastname;
+		this.password = password;
 	}
 	
-	public User(String id, String email, String firstname, String lastname) {
+	public User(String id, String email, String firstname, String lastname, String password) {
 		this.id = id;
 		this.email = email;
 		this.firstname = firstname;
 		this.lastname = lastname;
+		this.password = password;
 	}
 
 	public String getId() {
@@ -74,4 +77,12 @@ public class User  implements Serializable {
 	public void setLastname(String lastname) {
 		this.lastname = lastname;
 	}	
+	
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}	
 }

+ 51 - 50
DBCDS_S13_1_Data/src/main/resources/data.sql

@@ -1,53 +1,54 @@
-INSERT INTO users (id, firstname, lastname, email) VALUES ('f184847a-68af-4362-9ee7-35fc6e6eed11','Emmey','Orlene','emmey.orlene@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('b3dcd61e-d400-47e6-8c0d-ee7a7c439646','Carly','Skurnik','carly.skurnik@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('da382572-9999-4cab-a227-2ea1961732cf','Charlena','Harned','charlena.harned@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('3910b6f7-f11e-424e-8380-694b3293ec6a','Anthia','Harday','anthia.harday@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('93440957-e78e-42ce-8c14-7a5438c89949','Lita','Philoo','lita.philoo@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('adf5600c-fc2d-4c47-ba2a-f52826dc60ba','Lolita','Mozelle','lolita.mozelle@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('94e1bb86-07f7-4f10-a6e9-34c133b9be3f','Edith','Neils','edith.neils@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c9c998c2-b5a3-4052-ae9f-1afe2eb5f8af','Fanchon','Gualtiero','fanchon.gualtiero@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c30a9cd7-30af-4b73-9f4f-84f012ace4ea','Harrietta','Bettine','harrietta.bettine@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('37c0a502-28bf-48bf-9afb-792b181e28eb','Mamie','Milde','mamie.milde@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('4762caa9-657d-4183-a5b9-4b97cea2a5d4','Cassandra','Kussell','cassandra.kussell@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('3de2b9e1-6bd7-42d1-ab03-4eaa9874259b','Britte','Danby','britte.danby@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('77044f3f-20a0-441d-b025-acb04685c950','Quintina','Wilona','quintina.wilona@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('5d661895-038e-49b3-a7f5-ad56b8c3655a','Desirae','Ferino','desirae.ferino@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('dad62c3c-6150-44ec-84a0-525955a686e5','Adriana','Germann','adriana.germann@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('8cd4cdc5-1f0e-4509-91da-d1d1a6dd087c','Ivett','Whittaker','ivett.whittaker@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('a79c97c9-9297-4a3f-843e-c4dc6a22fb0b','Justinn','Mendez','justinn.mendez@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('f8abf69b-598d-4d79-ae7a-207150efbd8a','Pamella','Tayib','pamella.tayib@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('45644b92-3458-4b1e-a4bd-7b2f0742cdd0','Augustine','Ventre','augustine.ventre@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('db99e2df-1029-48d9-a208-25fdaf44bde5','Nerta','Burnside','nerta.burnside@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('ded607da-926f-475e-b2e7-71531dc0d451','Aigneis','Fairweather','aigneis.fairweather@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('cd3314cf-2efe-4892-a11d-d21c28946675','Hannis','Timon','hannis.timon@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c91133d5-60e3-452c-bc1d-7fe42e7504a6','Bertine','An','bertine.an@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('04d3a3f1-c146-4951-a734-8d195d4e4f7a','Kerrin','Tatianas','kerrin.tatianas@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('6b3c116f-68e5-4428-8d0b-65b932621515','Calla','Gaynor','calla.gaynor@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('416a3fc9-57a2-4de2-9e1c-26937b1c2506','Ofilia','Raychel','ofilia.raychel@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('70a9f479-9c6f-431a-8c07-d8de6e39ec26','Hope','Zitvaa','hope.zitvaa@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('d46f298f-e944-4438-8a63-913c68fef34c','Roseline','Chem','roseline.chem@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('a7125920-7ea1-47a9-9d22-f9a441a4b6af','Juliane','Lia','juliane.lia@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('262c513c-90d9-4663-ab46-f9587a2dedae','Phedra','Tengdin','phedra.tengdin@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('bce62edb-69ef-42da-b488-6904f914b34c','Nadine','Johanna','nadine.johanna@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('02751288-ab8f-4f25-9841-fa32bfda3f56','Asia','Charity','asia.charity@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('ad1266fe-c6a4-4001-8863-1c7df3478668','Loree','Terencio','loree.terencio@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('073bc4b3-398b-41b9-a948-c63727a76261','Jordan','Jobi','jordan.jobi@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('e385545f-b715-413e-b569-a59729d5b57a','Norine','Koehler','norine.koehler@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('cfa1c54d-b8a5-4d00-ba13-712107c0c129','Jacquetta','Carmena','jacquetta.carmena@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('24603a73-67f8-41c8-a107-660c170b3fc8','Hettie','Reinke','hettie.reinke@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('903e408d-6758-4a4e-8595-82f4365b61c8','Kial','Roarke','kial.roarke@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('fb380197-d655-49c9-a7b7-b84064c79f1b','Liana','Torray','liana.torray@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c31b89c6-76f7-4576-b1f0-397e65b86e26','Marika','Ahab','marika.ahab@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('913881a9-01b2-4b9b-8c50-ba401f02c226','Berta','Hanshaw','berta.hanshaw@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c4b3190c-b523-4de7-8caf-5d482eb954a2','Cherilyn','Marcellus','cherilyn.marcellus@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('c4340ed5-925f-4980-ba65-57bdb602b004','Marleah','Stacy','marleah.stacy@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('43834fb0-01f3-49b1-9967-4a0047a52469','Asia','Heisel','asia.heisel@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('41729310-7490-42d2-89a4-7b248d9c38b9','Kerrin','Infield','kerrin.infield@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('0bc6e9e7-d971-40e2-8b40-0d5a8dcc1fa9','Cristine','Rillings','cristine.rillings@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('f2be8755-127d-49df-8c39-d9627612e5b7','Jennica','Daegal','jennica.daegal@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('39f0d17f-dd1d-4a46-beb5-038b348d59fe','Alyssa','Chick','alyssa.chick@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('4c7c0bc7-31a4-4d5b-ab8b-5ae02894788d','Melodie','Granoff','melodie.granoff@yopmail.com');
-INSERT INTO users (id, firstname, lastname, email) VALUES ('fafd24e4-cce7-4ddd-9a8c-e2ee9b1db82e','Georgetta','Afton','georgetta.afton@yopmail.com');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('f184847a-68af-4362-9ee7-35fc6e6eed11','Emmey','Orlene','emmey.orlene@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('b3dcd61e-d400-47e6-8c0d-ee7a7c439646','Carly','Skurnik','carly.skurnik@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('da382572-9999-4cab-a227-2ea1961732cf','Charlena','Harned','charlena.harned@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('3910b6f7-f11e-424e-8380-694b3293ec6a','Anthia','Harday','anthia.harday@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('93440957-e78e-42ce-8c14-7a5438c89949','Lita','Philoo','lita.philoo@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('adf5600c-fc2d-4c47-ba2a-f52826dc60ba','Lolita','Mozelle','lolita.mozelle@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('94e1bb86-07f7-4f10-a6e9-34c133b9be3f','Edith','Neils','edith.neils@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c9c998c2-b5a3-4052-ae9f-1afe2eb5f8af','Fanchon','Gualtiero','fanchon.gualtiero@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c30a9cd7-30af-4b73-9f4f-84f012ace4ea','Harrietta','Bettine','harrietta.bettine@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('37c0a502-28bf-48bf-9afb-792b181e28eb','Mamie','Milde','mamie.milde@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('4762caa9-657d-4183-a5b9-4b97cea2a5d4','Cassandra','Kussell','cassandra.kussell@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('3de2b9e1-6bd7-42d1-ab03-4eaa9874259b','Britte','Danby','britte.danby@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('77044f3f-20a0-441d-b025-acb04685c950','Quintina','Wilona','quintina.wilona@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('5d661895-038e-49b3-a7f5-ad56b8c3655a','Desirae','Ferino','desirae.ferino@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('dad62c3c-6150-44ec-84a0-525955a686e5','Adriana','Germann','adriana.germann@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('8cd4cdc5-1f0e-4509-91da-d1d1a6dd087c','Ivett','Whittaker','ivett.whittaker@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('a79c97c9-9297-4a3f-843e-c4dc6a22fb0b','Justinn','Mendez','justinn.mendez@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('f8abf69b-598d-4d79-ae7a-207150efbd8a','Pamella','Tayib','pamella.tayib@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('45644b92-3458-4b1e-a4bd-7b2f0742cdd0','Augustine','Ventre','augustine.ventre@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('db99e2df-1029-48d9-a208-25fdaf44bde5','Nerta','Burnside','nerta.burnside@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('ded607da-926f-475e-b2e7-71531dc0d451','Aigneis','Fairweather','aigneis.fairweather@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('cd3314cf-2efe-4892-a11d-d21c28946675','Hannis','Timon','hannis.timon@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c91133d5-60e3-452c-bc1d-7fe42e7504a6','Bertine','An','bertine.an@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('04d3a3f1-c146-4951-a734-8d195d4e4f7a','Kerrin','Tatianas','kerrin.tatianas@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('6b3c116f-68e5-4428-8d0b-65b932621515','Calla','Gaynor','calla.gaynor@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('416a3fc9-57a2-4de2-9e1c-26937b1c2506','Ofilia','Raychel','ofilia.raychel@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('70a9f479-9c6f-431a-8c07-d8de6e39ec26','Hope','Zitvaa','hope.zitvaa@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('d46f298f-e944-4438-8a63-913c68fef34c','Roseline','Chem','roseline.chem@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('a7125920-7ea1-47a9-9d22-f9a441a4b6af','Juliane','Lia','juliane.lia@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('262c513c-90d9-4663-ab46-f9587a2dedae','Phedra','Tengdin','phedra.tengdin@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('bce62edb-69ef-42da-b488-6904f914b34c','Nadine','Johanna','nadine.johanna@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('02751288-ab8f-4f25-9841-fa32bfda3f56','Asia','Charity','asia.charity@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('ad1266fe-c6a4-4001-8863-1c7df3478668','Loree','Terencio','loree.terencio@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('073bc4b3-398b-41b9-a948-c63727a76261','Jordan','Jobi','jordan.jobi@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('e385545f-b715-413e-b569-a59729d5b57a','Norine','Koehler','norine.koehler@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('cfa1c54d-b8a5-4d00-ba13-712107c0c129','Jacquetta','Carmena','jacquetta.carmena@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('24603a73-67f8-41c8-a107-660c170b3fc8','Hettie','Reinke','hettie.reinke@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('903e408d-6758-4a4e-8595-82f4365b61c8','Kial','Roarke','kial.roarke@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('fb380197-d655-49c9-a7b7-b84064c79f1b','Liana','Torray','liana.torray@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c31b89c6-76f7-4576-b1f0-397e65b86e26','Marika','Ahab','marika.ahab@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('913881a9-01b2-4b9b-8c50-ba401f02c226','Berta','Hanshaw','berta.hanshaw@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c4b3190c-b523-4de7-8caf-5d482eb954a2','Cherilyn','Marcellus','cherilyn.marcellus@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('c4340ed5-925f-4980-ba65-57bdb602b004','Marleah','Stacy','marleah.stacy@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('43834fb0-01f3-49b1-9967-4a0047a52469','Asia','Heisel','asia.heisel@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('41729310-7490-42d2-89a4-7b248d9c38b9','Kerrin','Infield','kerrin.infield@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('0bc6e9e7-d971-40e2-8b40-0d5a8dcc1fa9','Cristine','Rillings','cristine.rillings@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('f2be8755-127d-49df-8c39-d9627612e5b7','Jennica','Daegal','jennica.daegal@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('39f0d17f-dd1d-4a46-beb5-038b348d59fe','Alyssa','Chick','alyssa.chick@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('4c7c0bc7-31a4-4d5b-ab8b-5ae02894788d','Melodie','Granoff','melodie.granoff@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+INSERT INTO users (id, firstname, lastname, email, password) VALUES ('fafd24e4-cce7-4ddd-9a8c-e2ee9b1db82e','Georgetta','Afton','georgetta.afton@yopmail.com', '$2a$10$sGqkHp68dtM7I8DtRn/urOM7vD8acl/PEaPyrU1.RFWfBeqDQdMFO');
+
 
 INSERT INTO mails (id, _from, _to, subject, body, _date) VALUES ('1991b879-a9a5-44c2-a135-867ccf738a11','f184847a-68af-4362-9ee7-35fc6e6eed11','416a3fc9-57a2-4de2-9e1c-26937b1c2506','Sed ante.','Integer aliquet, massa id lobortis convallis, tortor risus dapibus augue, vel accumsan tellus nisi eu orci. Mauris lacinia sapien quis libero. Nullam sit amet turpis elementum ligula vehicula consequat. Morbi a ipsum. Integer a nibh. In quis justo. Maecenas rhoncus aliquam lacus. Morbi quis tortor id nulla ultrices aliquet. Maecenas leo odio, condimentum id, luctus nec, molestie sed, justo. Pellentesque viverra pede ac diam. Cras pellentesque volutpat dui. Maecenas tristique, est et tempus semper, est quam pharetra magna, ac consequat metus sapien ut nunc. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris viverra diam vitae quam. Suspendisse potenti. Nullam porttitor lacus at turpis. Donec posuere metus vitae ipsum. Aliquam non mauris. Morbi non lectus.','2023-04-29');
 INSERT INTO mails (id, _from, _to, subject, body, _date) VALUES ('77e9ba8a-566d-4b4e-b977-599877d364db','b3dcd61e-d400-47e6-8c0d-ee7a7c439646','913881a9-01b2-4b9b-8c50-ba401f02c226','Integer a nibh.','Aliquam erat volutpat. In congue. Etiam justo. Etiam pretium iaculis justo. In hac habitasse platea dictumst. Etiam faucibus cursus urna. Ut tellus. Nulla ut erat id mauris vulputate elementum. Nullam varius. Nulla facilisi. Cras non velit nec nisi vulputate nonummy. Maecenas tincidunt lacus at velit. Vivamus vel nulla eget eros elementum pellentesque. Quisque porta volutpat erat. Quisque erat eros, viverra eget, congue eget, semper rutrum, nulla. Nunc purus. Phasellus in felis.','2023-04-29');

+ 9 - 0
DBCDS_S13_1_Gateway/src/main/java/es/uv/garcosda/gateway/config/WebConfig.java

@@ -1,15 +1,24 @@
 package es.uv.garcosda.gateway.config;
 
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.cors.CorsConfiguration;
 import org.springframework.web.cors.reactive.CorsWebFilter;
 import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
+import org.springframework.web.reactive.function.client.WebClient;
 
 import java.util.List;
 
 @Configuration
 public class WebConfig extends CorsConfiguration {
+	
+	@Bean
+	@LoadBalanced
+	public WebClient.Builder getWebClientBuilder() {
+		return WebClient.builder();
+	}
+	
     @Bean
     public CorsWebFilter corsFilter() {
         CorsConfiguration config = new CorsConfiguration();

+ 73 - 0
DBCDS_S13_1_Gateway/src/main/java/es/uv/garcosda/gateway/security/AuthFilter.java

@@ -0,0 +1,73 @@
+package es.uv.garcosda.gateway.security;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.gateway.filter.GatewayFilter;
+import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.server.reactive.ServerHttpResponse;
+import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
+
+import com.google.common.net.HttpHeaders;
+
+import reactor.core.publisher.Mono;
+
+@Component
+public class AuthFilter extends AbstractGatewayFilterFactory<AuthFilter.Config> {
+	
+	@Autowired
+	private WebClient.Builder client;
+	
+	public AuthFilter() {
+        super(Config.class);
+    }
+	
+	@Override
+	public GatewayFilter apply(Config config) {
+		
+		return (exchange, chain) -> {
+			
+			ServerHttpResponse response = exchange.getResponse();
+			
+			if(!exchange.getRequest().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
+				response.setStatusCode(HttpStatus.UNAUTHORIZED);
+				return response.setComplete();
+			}
+			
+			String authHeader = exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION).get(0);
+			if(!authHeader.contains("Bearer")) {
+				response.setStatusCode(HttpStatus.UNAUTHORIZED);
+				return response.setComplete();
+			}
+						
+			         
+			try {
+				return client.build()
+					         .get()
+					         .uri("http://auth-service/auth/authorize")
+					         .header(HttpHeaders.AUTHORIZATION, authHeader)
+					         .retrieve()
+					         .onStatus(httpStatus -> httpStatus.value() != HttpStatus.ACCEPTED.value(),
+					        	 error -> { return Mono.error(new Throwable("UNAUTHORIZED")); })
+					         .toEntity(String.class)
+					         .flatMap(entity -> {	    	 
+					       	     if(entity.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
+					    	 	     response.setStatusCode(HttpStatus.UNAUTHORIZED);
+					    	 	     return response.setComplete();
+					    	     }	 
+					    	     return chain.filter(exchange);
+					         });
+			}
+			catch (Throwable e){
+				response.setStatusCode(HttpStatus.UNAUTHORIZED);
+				return response.setComplete();
+			}
+		};
+
+	}
+	
+	public static class Config{ }
+	
+}

+ 37 - 0
DBCDS_S13_1_Shared/src/main/java/es/uv/garcosda/shared/domain/AuthenticationRequest.java

@@ -0,0 +1,37 @@
+package es.uv.garcosda.shared.domain;
+
+public class AuthenticationRequest {
+	private String username;
+    private String password;
+    
+    public AuthenticationRequest() {}
+    
+	public AuthenticationRequest(String username, String password) {
+		this.username = username;
+		this.password = password;
+	}
+
+	public String getUsername() {
+		return username;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+    
+    
+    
+    
+    
+    
+}
+
+

+ 14 - 2
DBCDS_S13_1_Shared/src/main/java/es/uv/garcosda/shared/domain/User.java

@@ -11,20 +11,23 @@ public class User  implements Serializable {
 	private String email;
 	private String firstname;
 	private String lastname;
+	private String password;
 	
 	public User() {}
 
-	public User(String email, String firstname, String lastname) {
+	public User(String email, String firstname, String lastname, String password) {
 		this.email = email;
 		this.firstname = firstname;
 		this.lastname = lastname;
+		this.password = password;
 	}
 	
-	public User(String id, String email, String firstname, String lastname) {
+	public User(String id, String email, String firstname, String lastname, String password) {
 		this.id = id;
 		this.email = email;
 		this.firstname = firstname;
 		this.lastname = lastname;
+		this.password = password;
 	}
 
 	public String getId() {
@@ -58,4 +61,13 @@ public class User  implements Serializable {
 	public void setLastname(String lastname) {
 		this.lastname = lastname;
 	}	
+	
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}	
+	
 }

二進制
DBCDS_S13_1_Shared/target/classes/es/uv/garcosda/shared/domain/AuthenticationRequest.class


二進制
DBCDS_S13_1_Shared/target/classes/es/uv/garcosda/shared/domain/User.class