package br.com.alura.mvc.mudi.api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import br.com.alura.mvc.mudi.model.Pedido; import br.com.alura.mvc.mudi.model.StatusPedido; import br.com.alura.mvc.mudi.repository.PedidoRepository;
@RestController @RequestMapping("/api/pedidos") public class PedidosRest {
@Autowired
private PedidoRepository pedidoRepository;
@GetMapping("aguardando")
public List<Pedido> getPedidosAguardandoOfertas() {
Sort sort = Sort.by("id").descending();
PageRequest paginacao = PageRequest.of(0, 10, sort);
return pedidoRepository.findByStatus(StatusPedido.AGUARDANDO, paginacao);
}
}
package br.com.alura.mvc.mudi;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/usuario/pedido", true)
.permitAll()
)
.logout(logout -> {
logout.logoutUrl("/logout")
.logoutSuccessUrl("/home");
})
.csrf().disable();//spring habilita uma forma de segurança automaticamente e precisa desabilitar o csrf()
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
BCryptPasswordEncoder enconder = new BCryptPasswordEncoder();
// UserDetails user = User.builder()
// .username("maria")
// .password(enconder.encode("maria"))
// .roles("ADM")
// .build();
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(enconder);
//.withUser(user);
}
// @Bean
// @Override
// public UserDetailsService userDetailsService() {
// UserDetails user =
// User.withDefaultPasswordEncoder()
// .username("joao")
// .password("joao")
// .roles("ADM")
// .build();
//
// return new InMemoryUserDetailsManager(user);
// }
}
package br.com.alura.mvc.mudi.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
@Controller @RequestMapping(value = "/oferta", method = RequestMethod.GET) public class OfertaController {
@GetMapping
public String getFormularioParaOfertas(){
return "/oferta/home";//pasta fisica
}
}
package br.com.alura.mvc.mudi.model;
import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table;
@Entity @Table(name="users") public class User {
@Id
private String username;
private String password;
private Boolean enable;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private List<Pedido> pedidos;
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 Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
}
insira seu código aqui
`