package br.com.gpansolucoes.sistemas.config.security;
import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; 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;
@EnableWebSecurity @Configuration public class SecurityConfigurations extends WebSecurityConfigurerAdapter {
//Configurações de Autenticação
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
//Configurações de Autorização
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/autenticacao/login").permitAll()
.anyRequest().authenticated();
}
//Configurações de recursos estaticos(js,css, images, etc...)
@Override
public void configure(WebSecurity web) throws Exception {
}
} package br.com.gpansolucoes.sistemas.controllers;
import br.com.gpansolucoes.sistemas.dtos.request.AutenticacaoRequestDTO; import br.com.gpansolucoes.sistemas.dtos.request.LoginRequestDTO; import br.com.gpansolucoes.sistemas.dtos.response.AutenticacaoResponseDTO; import br.com.gpansolucoes.sistemas.response.Response; import br.com.gpansolucoes.sistemas.services.AutenticacaoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;
@RestController @RequestMapping("/api/v1/pos-venda/autenticacao") public class AutenticacaoController {
@Autowired
AutenticacaoService autenticacaoService;
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Response<AutenticacaoResponseDTO>> create(@RequestBody AutenticacaoRequestDTO autenticacaoRequestDTO) throws Exception {
Response<AutenticacaoResponseDTO> response = new Response<AutenticacaoResponseDTO>();
AutenticacaoResponseDTO retorno = autenticacaoService.criarAutenticacao(autenticacaoRequestDTO);
response.setData(retorno);
return ResponseEntity.ok().body(response);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Response<AutenticacaoResponseDTO>> findById(@PathVariable("id") Integer id) throws Exception {
Response<AutenticacaoResponseDTO> response = new Response<AutenticacaoResponseDTO>();
AutenticacaoResponseDTO retorno = autenticacaoService.consultarAutenticacao(id);
if(retorno == null){
return ResponseEntity.noContent().build();
}
response.setData(retorno);
return ResponseEntity.ok().body(response);
}
@PostMapping(value = "/login")
public ResponseEntity login(@RequestBody LoginRequestDTO loginRequestDTO) throws Exception {
autenticacaoService.consultarAutenticacaoPorEmaileSenha(loginRequestDTO);
return ResponseEntity.ok().body("");
}
@RequestMapping(value = "/{idAutenticacao}", method = RequestMethod.PUT)
public ResponseEntity update(@PathVariable("idAutenticacao") Integer idAutenticacao, @RequestBody AutenticacaoRequestDTO autenticacaoRequestDTO) throws Exception {
autenticacaoService.atualizarAutenticacao(autenticacaoRequestDTO, idAutenticacao);
return ResponseEntity.ok().body("");
}
@RequestMapping(value = "/{idAutenticacao}", method = RequestMethod.DELETE)
public ResponseEntity delete (@PathVariable("idAutenticacao") Integer idAutenticacao) throws Exception {
autenticacaoService.deletarAutenticacao(idAutenticacao);
return ResponseEntity.ok().body("");
}
}