From ebc045a52e7b29b95ba160de677a4b3fd4340c75 Mon Sep 17 00:00:00 2001 From: Jorge Amaro Date: Sat, 8 Jul 2023 21:45:02 -0300 Subject: [PATCH] Final --- .../devsuperior/bds03/config/AppConfig.java | 33 ++++ .../config/AuthorizationServerConfig.java | 67 +++++++ .../bds03/config/ResourceServerConfig.java | 53 ++++++ .../bds03/config/SecurityConfig.java | 16 -- .../bds03/config/WebSecurityConfig.java | 50 +++++ .../bds03/controllers/EmployeeController.java | 2 +- .../controllers/exceptions/FieldMessage.java | 35 ++++ .../exceptions/ResourceExceptionHandler.java | 39 ++++ .../controllers/exceptions/StandardError.java | 60 ++++++ .../exceptions/ValidationError.java | 19 ++ .../devsuperior/bds03/dto/EmployeeDTO.java | 10 + .../com/devsuperior/bds03/entities/Role.java | 74 ++++++++ .../com/devsuperior/bds03/entities/User.java | 171 ++++++++++++++++++ .../bds03/repositories/UserRepository.java | 10 + .../bds03/services/UserService.java | 34 ++++ src/main/resources/application.properties | 8 +- src/main/resources/data.sql | 12 +- 17 files changed, 674 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/devsuperior/bds03/config/AppConfig.java create mode 100644 src/main/java/com/devsuperior/bds03/config/AuthorizationServerConfig.java create mode 100644 src/main/java/com/devsuperior/bds03/config/ResourceServerConfig.java delete mode 100644 src/main/java/com/devsuperior/bds03/config/SecurityConfig.java create mode 100644 src/main/java/com/devsuperior/bds03/config/WebSecurityConfig.java create mode 100644 src/main/java/com/devsuperior/bds03/controllers/exceptions/FieldMessage.java create mode 100644 src/main/java/com/devsuperior/bds03/controllers/exceptions/ResourceExceptionHandler.java create mode 100644 src/main/java/com/devsuperior/bds03/controllers/exceptions/StandardError.java create mode 100644 src/main/java/com/devsuperior/bds03/controllers/exceptions/ValidationError.java create mode 100644 src/main/java/com/devsuperior/bds03/entities/Role.java create mode 100644 src/main/java/com/devsuperior/bds03/entities/User.java create mode 100644 src/main/java/com/devsuperior/bds03/repositories/UserRepository.java create mode 100644 src/main/java/com/devsuperior/bds03/services/UserService.java diff --git a/src/main/java/com/devsuperior/bds03/config/AppConfig.java b/src/main/java/com/devsuperior/bds03/config/AppConfig.java new file mode 100644 index 00000000..2170b2fd --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/config/AppConfig.java @@ -0,0 +1,33 @@ +package com.devsuperior.bds03.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; + +@Configuration +public class AppConfig { + + @Value("${jwt.secret}") + private String jwtSecret; + + @Bean + BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Bean + public JwtAccessTokenConverter accessTokenConverter() { + JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter(); + tokenConverter.setSigningKey(jwtSecret); + return tokenConverter; + } + + @Bean + public JwtTokenStore tokenStore() { + return new JwtTokenStore(accessTokenConverter()); + } + +} diff --git a/src/main/java/com/devsuperior/bds03/config/AuthorizationServerConfig.java b/src/main/java/com/devsuperior/bds03/config/AuthorizationServerConfig.java new file mode 100644 index 00000000..a8791509 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/config/AuthorizationServerConfig.java @@ -0,0 +1,67 @@ +package com.devsuperior.bds03.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; +import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; + +@Configuration +@EnableAuthorizationServer +public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{ + + @Value("${security.oauth2.client.client-id}") + private String clientId; + + @Value("${security.oauth2.client.client-secret}") + private String clientSecret; + + @Value("${jwt.duration}") + private Integer jwtDuration; + + @Autowired + private BCryptPasswordEncoder passwordEncoder; + + @Autowired + private JwtAccessTokenConverter accessTokenConverter; + + @Autowired + private JwtTokenStore tokenStore; + + @Autowired + private AuthenticationManager authenticationManager; + + @Override + public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { + security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); + } + + + @Override + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { + clients.inMemory() + .withClient(clientId) + .secret(passwordEncoder.encode(clientSecret)) + .scopes("read,write") + .authorizedGrantTypes("password") + .accessTokenValiditySeconds(jwtDuration); + + } + + + @Override + public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + endpoints.authenticationManager(authenticationManager) + .tokenStore(tokenStore) + .accessTokenConverter(accessTokenConverter); + } + + +} diff --git a/src/main/java/com/devsuperior/bds03/config/ResourceServerConfig.java b/src/main/java/com/devsuperior/bds03/config/ResourceServerConfig.java new file mode 100644 index 00000000..362aa508 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/config/ResourceServerConfig.java @@ -0,0 +1,53 @@ +package com.devsuperior.bds03.config; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; +import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; +import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; +import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; + + +@Configuration +@EnableResourceServer +public class ResourceServerConfig extends ResourceServerConfigurerAdapter{ + + + @Autowired + private Environment environment; + + @Autowired + private JwtTokenStore tokenStore; + + private static final String[] PUBLIC = {"/oauth/token", "/h2-console/**"}; + + private static final String[] OPERATOR_GET = {"/departments/**", "/employees/**"}; + + + @Override + public void configure(ResourceServerSecurityConfigurer resources) throws Exception { + resources.tokenStore(tokenStore); + } + + + @Override + public void configure(HttpSecurity http) throws Exception { + + + if(Arrays.asList(environment.getActiveProfiles()).contains("test")) { + http.headers().frameOptions().disable(); + } + + http.authorizeRequests() + .antMatchers(PUBLIC).permitAll() + .antMatchers(HttpMethod.GET, OPERATOR_GET).hasAnyRole("OPERATOR","ADMIN") + .anyRequest().hasAnyRole("ADMIN"); + } + + +} diff --git a/src/main/java/com/devsuperior/bds03/config/SecurityConfig.java b/src/main/java/com/devsuperior/bds03/config/SecurityConfig.java deleted file mode 100644 index 09a75cde..00000000 --- a/src/main/java/com/devsuperior/bds03/config/SecurityConfig.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.devsuperior.bds03.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.web.builders.WebSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; - -@Configuration -@EnableWebSecurity -public class SecurityConfig extends WebSecurityConfigurerAdapter { - - @Override - public void configure(WebSecurity web) throws Exception { - web.ignoring().antMatchers("/**"); - } -} diff --git a/src/main/java/com/devsuperior/bds03/config/WebSecurityConfig.java b/src/main/java/com/devsuperior/bds03/config/WebSecurityConfig.java new file mode 100644 index 00000000..f2f11c60 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/config/WebSecurityConfig.java @@ -0,0 +1,50 @@ +package com.devsuperior.bds03.config; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.WebSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private Environment env; + + @Autowired + private BCryptPasswordEncoder passwordEncoder; + + @Autowired + private UserDetailsService userDetailsService; + + @Override + public void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); + } + + @Override + public void configure(WebSecurity web) throws Exception { + if(Arrays.asList(env.getActiveProfiles()).contains("test")) { + web.ignoring().antMatchers("/actuator/**"); + } + } + + @Override + @Bean + protected AuthenticationManager authenticationManager() throws Exception { + return super.authenticationManager(); + } + + + +} \ No newline at end of file diff --git a/src/main/java/com/devsuperior/bds03/controllers/EmployeeController.java b/src/main/java/com/devsuperior/bds03/controllers/EmployeeController.java index 4969d1c4..a583411d 100644 --- a/src/main/java/com/devsuperior/bds03/controllers/EmployeeController.java +++ b/src/main/java/com/devsuperior/bds03/controllers/EmployeeController.java @@ -35,7 +35,7 @@ public ResponseEntity> findAll(Pageable pageable) { } @PostMapping - public ResponseEntity insert(@RequestBody EmployeeDTO dto) { + public ResponseEntity insert(@RequestBody @Valid EmployeeDTO dto) { dto = service.insert(dto); URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}") .buildAndExpand(dto.getId()).toUri(); diff --git a/src/main/java/com/devsuperior/bds03/controllers/exceptions/FieldMessage.java b/src/main/java/com/devsuperior/bds03/controllers/exceptions/FieldMessage.java new file mode 100644 index 00000000..8561f0b1 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/controllers/exceptions/FieldMessage.java @@ -0,0 +1,35 @@ +package com.devsuperior.bds03.controllers.exceptions; + +import java.io.Serializable; + +public class FieldMessage implements Serializable{ + + private static final long serialVersionUID = 1L; + + private String fieldName; + private String message; + + public FieldMessage() { + } + + public FieldMessage(String fieldName, String message) { + super(); + this.fieldName = fieldName; + this.message = message; + } + + public String getFieldName() { + return fieldName; + } + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + + +} diff --git a/src/main/java/com/devsuperior/bds03/controllers/exceptions/ResourceExceptionHandler.java b/src/main/java/com/devsuperior/bds03/controllers/exceptions/ResourceExceptionHandler.java new file mode 100644 index 00000000..175e0ad5 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/controllers/exceptions/ResourceExceptionHandler.java @@ -0,0 +1,39 @@ +package com.devsuperior.bds03.controllers.exceptions; + +import java.time.Instant; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + + +@ControllerAdvice +public class ResourceExceptionHandler { + + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity validation( + MethodArgumentNotValidException ex, HttpServletRequest request){ + + HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY; + ValidationError error = new ValidationError(); + error.setTimestamp(Instant.now()); + error.setStatus(status.value()); + error.setError("Validation exception"); + error.setMessage(ex.getMessage()); + error.setPath(request.getRequestURI()); + + for(FieldError field : ex.getBindingResult().getFieldErrors()) { + error.addError(field.getField(), field.getDefaultMessage()); + } + return ResponseEntity.status(status).body(error); + + + } + +} diff --git a/src/main/java/com/devsuperior/bds03/controllers/exceptions/StandardError.java b/src/main/java/com/devsuperior/bds03/controllers/exceptions/StandardError.java new file mode 100644 index 00000000..9c3628e3 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/controllers/exceptions/StandardError.java @@ -0,0 +1,60 @@ +package com.devsuperior.bds03.controllers.exceptions; + +import java.io.Serializable; +import java.time.Instant; + +public class StandardError implements Serializable { + + private static final long serialVersionUID = 1L; + + private Instant timestamp; + private Integer status; + private String error; + private String message; + private String path; + + public StandardError() { + } + + public Instant getTimestamp() { + return timestamp; + } + + public void setTimestamp(Instant timestamp) { + this.timestamp = timestamp; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + +} diff --git a/src/main/java/com/devsuperior/bds03/controllers/exceptions/ValidationError.java b/src/main/java/com/devsuperior/bds03/controllers/exceptions/ValidationError.java new file mode 100644 index 00000000..9d140f2c --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/controllers/exceptions/ValidationError.java @@ -0,0 +1,19 @@ +package com.devsuperior.bds03.controllers.exceptions; + +import java.util.ArrayList; +import java.util.List; + +public class ValidationError extends StandardError { + + private static final long serialVersionUID = 1L; + private List errors = new ArrayList<>(); + + public void addError(String fieldName, String message) { + errors.add(new FieldMessage(fieldName, message)); + } + + public List getErrors() { + return errors; + } + +} diff --git a/src/main/java/com/devsuperior/bds03/dto/EmployeeDTO.java b/src/main/java/com/devsuperior/bds03/dto/EmployeeDTO.java index 2402cfe3..a5c81d58 100644 --- a/src/main/java/com/devsuperior/bds03/dto/EmployeeDTO.java +++ b/src/main/java/com/devsuperior/bds03/dto/EmployeeDTO.java @@ -2,14 +2,24 @@ import java.io.Serializable; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + import com.devsuperior.bds03.entities.Employee; public class EmployeeDTO implements Serializable { private static final long serialVersionUID = 1L; private Long id; + + @NotBlank(message = "Campo requerido") private String name; + + @Email (message = "Email inválido") private String email; + + @NotNull (message = "Campo requerido") private Long departmentId; public EmployeeDTO() { diff --git a/src/main/java/com/devsuperior/bds03/entities/Role.java b/src/main/java/com/devsuperior/bds03/entities/Role.java new file mode 100644 index 00000000..18eaedf9 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/entities/Role.java @@ -0,0 +1,74 @@ +package com.devsuperior.bds03.entities; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "tb_role") +public class Role implements Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String authority; + + + public Role() { + } + + public Role(Long id, String authority) { + super(); + this.id = id; + this.authority = authority; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Role other = (Role) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAuthority() { + return authority; + } + + public void setAuthority(String authority) { + this.authority = authority; + } + + +} diff --git a/src/main/java/com/devsuperior/bds03/entities/User.java b/src/main/java/com/devsuperior/bds03/entities/User.java new file mode 100644 index 00000000..e654f065 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/entities/User.java @@ -0,0 +1,171 @@ +package com.devsuperior.bds03.entities; + +import java.io.Serializable; +import java.time.Instant; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +@Entity +@Table(name = "tb_user") +public class User implements UserDetails, Serializable{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + + @Column(unique = true) + private String email; + + private String password; + + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "tb_user_role", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "role_id")) + private Set roles = new HashSet<>(); + + @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") + private Instant createdAt; + + @Column(columnDefinition = "TIMESTAMP WITHOUT TIME ZONE") + private Instant updateAt; + + public User() { + } + + + public User(Long id, String email, String password) { + super(); + this.id = id; + this.email = email; + this.password = password; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + User other = (User) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + + public Set getRoles() { + return roles; + } + + + @PrePersist + public void preCreatedPersist() { + createdAt = Instant.now(); + } + + @PreUpdate + public void preUpdatePersist() { + updateAt = Instant.now(); + } + + + @Override + public Collection getAuthorities() { + return roles.stream() + .map(role -> new SimpleGrantedAuthority(role.getAuthority())) + .collect(Collectors.toList()); + } + + + @Override + public String getUsername() { + return email; + } + + + @Override + public boolean isAccountNonExpired() { + return true; + } + + + @Override + public boolean isAccountNonLocked() { + return true; + } + + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + + @Override + public boolean isEnabled() { + return true; + } +} diff --git a/src/main/java/com/devsuperior/bds03/repositories/UserRepository.java b/src/main/java/com/devsuperior/bds03/repositories/UserRepository.java new file mode 100644 index 00000000..2230e8d2 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/repositories/UserRepository.java @@ -0,0 +1,10 @@ +package com.devsuperior.bds03.repositories; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.devsuperior.bds03.entities.User; + +public interface UserRepository extends JpaRepository { + + User findByEmail(String email); +} diff --git a/src/main/java/com/devsuperior/bds03/services/UserService.java b/src/main/java/com/devsuperior/bds03/services/UserService.java new file mode 100644 index 00000000..44fa8311 --- /dev/null +++ b/src/main/java/com/devsuperior/bds03/services/UserService.java @@ -0,0 +1,34 @@ +package com.devsuperior.bds03.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +import com.devsuperior.bds03.entities.User; +import com.devsuperior.bds03.repositories.UserRepository; + +@Service +public class UserService implements UserDetailsService{ + + private static Logger logger = LoggerFactory.getLogger(UserService.class); + + @Autowired + private UserRepository userRepository; + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + User user = userRepository.findByEmail(username); + + if(user == null) { + logger.error("User not found: " + username); + throw new UsernameNotFoundException("Email not found"); + } + + logger.info("User found: " + username); + return user; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a7dcd8af..de5668d0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,9 @@ -spring.profiles.active=test +spring.profiles.active=${APP_PROFILE:test} spring.jpa.open-in-view=false + +security.oauth2.client.client-id=${CLIENT_ID:myclientid} +security.oauth2.client.client-secret=${CLIENT_SECRET:myclientsecret} + +jwt.secret=${JWT_SECRET:MY-JWT-SECRET} +jwt.duration=${JWT_DURATION:86400} \ No newline at end of file diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 08576c63..4120310a 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,3 +1,13 @@ +INSERT INTO tb_user (email, password, created_At) VALUES ('ana@gmail.com', '$2a$10$eACCYoNOHEqXve8aIWT8Nu3PkMXWBaOxJ9aORUYzfMQCbVBIhZ8tG', TIMESTAMP WITH TIME ZONE '2023-05-30T18:17:07.12345Z'); +INSERT INTO tb_user (email, password, created_At) VALUES ('bob@gmail.com', '$2a$10$eACCYoNOHEqXve8aIWT8Nu3PkMXWBaOxJ9aORUYzfMQCbVBIhZ8tG', TIMESTAMP WITH TIME ZONE '2023-05-30T18:17:07.12345Z'); + +INSERT INTO tb_role (authority) VALUES ('ROLE_OPERATOR'); +INSERT INTO tb_role (authority) VALUES ('ROLE_ADMIN'); + +INSERT INTO tb_user_role (user_id, role_id) VALUES (1, 1); +INSERT INTO tb_user_role (user_id, role_id) VALUES (2, 1); +INSERT INTO tb_user_role (user_id, role_id) VALUES (2, 2); + INSERT INTO tb_department(name) VALUES ('Sales'); INSERT INTO tb_department(name) VALUES ('Management'); INSERT INTO tb_department(name) VALUES ('Training'); @@ -15,4 +25,4 @@ INSERT INTO tb_employee(name, email, department_id) VALUES ('Rafael', 'rafael@gm INSERT INTO tb_employee(name, email, department_id) VALUES ('Tiago', 'tiago@gmail.com', 1); INSERT INTO tb_employee(name, email, department_id) VALUES ('Andressa', 'andressa@gmail.com', 2); INSERT INTO tb_employee(name, email, department_id) VALUES ('Meire', 'meire@gmail.com', 3); -INSERT INTO tb_employee(name, email, department_id) VALUES ('Carol', 'carol@gmail.com', 1); +INSERT INTO tb_employee(name, email, department_id) VALUES ('Carol', 'carol@gmail.com', 1); \ No newline at end of file