From 164fcefcadd30bc04128320345629a503f88e8ff Mon Sep 17 00:00:00 2001 From: Sergio Holovati <58234823+SergioHolovati@users.noreply.github.com> Date: Mon, 15 Jun 2020 15:57:21 -0300 Subject: [PATCH] Teste Upload Teste pela empresa Altran --- .gitattributes | 2 + DemoApplication.java | 19 ++++++++ config/SecurityConfig.java | 34 +++++++++++++ controller/ControllerUser.java | 54 +++++++++++++++++++++ controller/GastosController.java | 62 ++++++++++++++++++++++++ gastos/GastoByUser.java | 63 ++++++++++++++++++++++++ repository/GastosRepository.java | 14 ++++++ repository/UsuarioRepository.java | 15 ++++++ repository/ValidationRepository.java | 8 ++++ service/CustomUserDetailService.java | 31 ++++++++++++ service/UsuarioService.java | 21 ++++++++ usuario/UserValid.java | 71 ++++++++++++++++++++++++++++ usuario/UsuarioUser.java | 48 +++++++++++++++++++ 13 files changed, 442 insertions(+) create mode 100644 .gitattributes create mode 100644 DemoApplication.java create mode 100644 config/SecurityConfig.java create mode 100644 controller/ControllerUser.java create mode 100644 controller/GastosController.java create mode 100644 gastos/GastoByUser.java create mode 100644 repository/GastosRepository.java create mode 100644 repository/UsuarioRepository.java create mode 100644 repository/ValidationRepository.java create mode 100644 service/CustomUserDetailService.java create mode 100644 service/UsuarioService.java create mode 100644 usuario/UserValid.java create mode 100644 usuario/UsuarioUser.java diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..dfe07704 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/DemoApplication.java b/DemoApplication.java new file mode 100644 index 00000000..41d602f7 --- /dev/null +++ b/DemoApplication.java @@ -0,0 +1,19 @@ +package br.com.altrantest.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +@SpringBootApplication +@EnableJpaRepositories + +public class DemoApplication extends SpringBootServletInitializer { + + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + +} + diff --git a/config/SecurityConfig.java b/config/SecurityConfig.java new file mode 100644 index 00000000..1e00397e --- /dev/null +++ b/config/SecurityConfig.java @@ -0,0 +1,34 @@ +package br.com.altrantest.demo.config; + +import br.com.altrantest.demo.service.CustomUserDetailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + private CustomUserDetailService customUserDetailService; + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/*/user/**") .hasRole("USER") + .antMatchers("/*/admin/**") .hasRole("ADMIN") + .and() + .httpBasic() + .and() + .csrf().disable(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(customUserDetailService).passwordEncoder(new BCryptPasswordEncoder()); + } + +} diff --git a/controller/ControllerUser.java b/controller/ControllerUser.java new file mode 100644 index 00000000..cc4bdb7d --- /dev/null +++ b/controller/ControllerUser.java @@ -0,0 +1,54 @@ +package br.com.altrantest.demo.controller; + +import br.com.altrantest.demo.repository.UsuarioRepository; +import br.com.altrantest.demo.usuario.UsuarioUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/v1") +public class ControllerUser { + @Autowired + private UsuarioRepository repository; + private UsuarioUser usuario; + + + //Retorna tudo + @GetMapping(path = "/user") + public ResponseEntityfindAll(){ + + return new ResponseEntity<>(repository.findAll(), HttpStatus.OK); + } +//controle de usuario + @GetMapping(path = "/user/{id}") + public ResponseEntity getUserById(@PathVariable("id")Long id, + @AuthenticationPrincipal UserDetails userDetails){ + System.out.println(userDetails); + UsuarioUser usuario = repository.findById(id).get(); + return new ResponseEntity<>(usuario, HttpStatus.OK); + } + @PostMapping(path = "/admin") + public ResponseEntity adcionarUsuario(@RequestBody UsuarioUser usuario){ + usuario = repository.save(usuario); + return ResponseEntity.ok().body(usuario); + } + @DeleteMapping(path = "/admin/{id}") + public ResponseEntitydelete(@PathVariable Long id){ + repository.deleteById(id); + return new ResponseEntity<>(HttpStatus.OK); + } + @PutMapping(path = "/admin/{id}") + public ResponseEntity update(@RequestBody UsuarioUser usuario){ + repository.save(usuario); + return new ResponseEntity<>(HttpStatus.OK); + } + + + + + +} diff --git a/controller/GastosController.java b/controller/GastosController.java new file mode 100644 index 00000000..55e46a09 --- /dev/null +++ b/controller/GastosController.java @@ -0,0 +1,62 @@ +package br.com.altrantest.demo.controller; + +import br.com.altrantest.demo.gastos.GastoByUser; +import br.com.altrantest.demo.repository.GastosRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + + + +@RequestMapping("/v1/gastos") +@RestController +public class GastosController { + + @Autowired + private GastosRepository gastosRepository; + + private GastoByUser gastosByUser; + + //Controle de gastos + + + @GetMapping(path = "/user/all") + public ResponseEntity findAllGastos() { + + return new ResponseEntity<>(gastosRepository.findAll(), HttpStatus.OK); + } + + @GetMapping(path = "/user/{id}") + public ResponseEntity getGastosById(@PathVariable("id") Long id) { + GastoByUser gastos = gastosRepository.findById(id).get(); + return new ResponseEntity<>(gastos, HttpStatus.OK); + } + + @PostMapping(path = "/user") + public ResponseEntity adcionarGastos(@RequestBody GastoByUser gastosByUser) { + gastosByUser = gastosRepository.save(gastosByUser); + return ResponseEntity.ok().body(gastosByUser); + } + + @DeleteMapping(path = "/admin/{id}") + public ResponseEntity deleteGastos(@PathVariable Long id) { + gastosRepository.deleteById(id); + return new ResponseEntity<>(HttpStatus.OK); + } + + @PutMapping(path = "/{id}") + public ResponseEntity update(@RequestBody GastoByUser gastoByUser) { + gastosRepository.save(gastoByUser); + return new ResponseEntity<>(HttpStatus.OK); + } + + @GetMapping(path = "/user/filtro/{descricao}") + @ResponseBody + public ResponseEntity getByDesc(@PathVariable String descricao) { + + return new ResponseEntity<>(gastosRepository.findByDescricao(descricao), HttpStatus.OK); + } + + +} diff --git a/gastos/GastoByUser.java b/gastos/GastoByUser.java new file mode 100644 index 00000000..d83351a4 --- /dev/null +++ b/gastos/GastoByUser.java @@ -0,0 +1,63 @@ +package br.com.altrantest.demo.gastos; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import java.io.Serializable; +import java.util.Date; + +@Entity +public class GastoByUser implements Serializable { + @Id + @GeneratedValue + private Long id; + private String descricao; + private Date dataCompra ; + private Long codigoUsuario; + private Double valor; + + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + + public String getDescricao() { + return descricao; + } + + public void setDescricao(String descricao) { + this.descricao = descricao; + } + + public Date getDataCompra() { + return dataCompra; + } + + public void setDataCompra(Date dataCompra) { +this.dataCompra = dataCompra; + } + + public Long getCodigoUsuario() { + return codigoUsuario; + } + + public void setCodigoUsuario(Long codigoUsuario) { + this.codigoUsuario = codigoUsuario; + } + + public Double getValor() { + return valor; + } + + public void setValor(Double valor) { + this.valor = valor; + } + + +} diff --git a/repository/GastosRepository.java b/repository/GastosRepository.java new file mode 100644 index 00000000..91140ac8 --- /dev/null +++ b/repository/GastosRepository.java @@ -0,0 +1,14 @@ +package br.com.altrantest.demo.repository; + +import br.com.altrantest.demo.gastos.GastoByUser; +import org.springframework.data.repository.PagingAndSortingRepository; + +import java.util.List; +import java.util.Optional; + +public interface GastosRepository extends PagingAndSortingRepository { + public Optional findById(Long id); + + + List findByDescricao(String desc); +} \ No newline at end of file diff --git a/repository/UsuarioRepository.java b/repository/UsuarioRepository.java new file mode 100644 index 00000000..fe6771bf --- /dev/null +++ b/repository/UsuarioRepository.java @@ -0,0 +1,15 @@ +package br.com.altrantest.demo.repository; + +import br.com.altrantest.demo.usuario.UsuarioUser; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface UsuarioRepository extends PagingAndSortingRepository { + public Optional findById(Long id); + + + +} diff --git a/repository/ValidationRepository.java b/repository/ValidationRepository.java new file mode 100644 index 00000000..ace1a4bc --- /dev/null +++ b/repository/ValidationRepository.java @@ -0,0 +1,8 @@ +package br.com.altrantest.demo.repository; + +import br.com.altrantest.demo.usuario.UserValid; +import org.springframework.data.repository.PagingAndSortingRepository; + +public interface ValidationRepository extends PagingAndSortingRepository { + UserValid findByUsername(String username); +} diff --git a/service/CustomUserDetailService.java b/service/CustomUserDetailService.java new file mode 100644 index 00000000..cdf8a2e9 --- /dev/null +++ b/service/CustomUserDetailService.java @@ -0,0 +1,31 @@ +package br.com.altrantest.demo.service; + +import br.com.altrantest.demo.repository.ValidationRepository; +import br.com.altrantest.demo.usuario.UserValid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; +@Component +public class CustomUserDetailService implements UserDetailsService { + private ValidationRepository validationRepository; + @Autowired + public CustomUserDetailService(ValidationRepository validationRepository) { + this.validationRepository = validationRepository; + } + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{ + UserValid user = Optional.ofNullable(validationRepository.findByUsername(username)).orElseThrow(()->new UsernameNotFoundException("User not Found")); + List authorityListAdmin = AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"); + List authorityListUser = AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"); + return new User(user.getUsername(),user.getPassword(),user.isAdmin()? authorityListAdmin : authorityListUser); + } +} diff --git a/service/UsuarioService.java b/service/UsuarioService.java new file mode 100644 index 00000000..56f39392 --- /dev/null +++ b/service/UsuarioService.java @@ -0,0 +1,21 @@ +package br.com.altrantest.demo.service; + + +import br.com.altrantest.demo.repository.UsuarioRepository; +import br.com.altrantest.demo.usuario.UsuarioUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UsuarioService { + + @Autowired + UsuarioRepository repository; + + public UsuarioUser consultar(Long id){ + UsuarioUser result = repository.findById(id).get(); + return result; + } + + +} diff --git a/usuario/UserValid.java b/usuario/UserValid.java new file mode 100644 index 00000000..70d25388 --- /dev/null +++ b/usuario/UserValid.java @@ -0,0 +1,71 @@ +package br.com.altrantest.demo.usuario; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.validation.constraints.NotEmpty; +import java.io.Serializable; + + +@Entity +public class UserValid implements Serializable { + @Id + @GeneratedValue + private Long id; + @NotEmpty + @Column(unique = true) + private String username; + @NotEmpty + @JsonIgnore + private String password; + @NotEmpty + private String name; + @NotEmpty + private boolean admin; + + public UserValid() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isAdmin() { + return admin; + } + + public void setAdmin(boolean admin) { + this.admin = admin; + } +} diff --git a/usuario/UsuarioUser.java b/usuario/UsuarioUser.java new file mode 100644 index 00000000..585b81d9 --- /dev/null +++ b/usuario/UsuarioUser.java @@ -0,0 +1,48 @@ +package br.com.altrantest.demo.usuario; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import java.io.Serializable; + +@Entity +public class UsuarioUser implements Serializable { + @Id + @GeneratedValue + private Long id; + private String token; + private String name; + + public UsuarioUser() { + } + + public UsuarioUser(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}