我在春季阶段有一个服务,在那里我得到一个类似@ResquestBody的对象来执行注册,但是POST方法不起作用,在JSON步骤时返回错误。下面是错误和代码:
代码:
@RequestMapping(value = "/cliente/", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> createUser(@RequestBody Cliente user) {
logger.info("Creating User : {}", user);
if (clienteService.isUserExist(user)) {
logger.error("Unable to create. A User with name {} already exist", user.getNome());
return new ResponseEntity(new CustomErrorType("Unable to create. A User with name " +
user.getNome() + " already exist."),HttpStatus.CONFLICT);
}
clienteService.save(user);
return new ResponseEntity<String>(HttpStatus.CREATED);
}
班级:
@Entity
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Cliente implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String nome;
private String telefone;
private String endereco;
private String cidade;
private String estado;
private String cnpjCpf;
private String email;
private String senha;
@Lob
@Column(name = "imagem")
private byte[] imagem;
public Cliente(Long id, String nome, String telefone, String endereco, String cidade, String estado,
String cnpjCpf, String email, String senha, byte[] imagem) {
this.id = id;
this.nome = nome;
this.telefone = telefone;
this.endereco = endereco;
this.cidade = cidade;
this.estado = estado;
this.cnpjCpf = cnpjCpf;
this.email = email;
this.senha = senha;
this.imagem = imagem;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
public String getCnpjCpf() {
return cnpjCpf;
}
public void setCnpjCpf(String cnpjCpf) {
this.cnpjCpf = cnpjCpf;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public byte[] getImagem() {
return imagem;
}
public void setImagem(byte[] imagem) {
this.imagem = imagem;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cliente other = (Cliente) obj;
if (id != other.id)
return false;
return true;
}
}
图中显示,在使用邮递员应用程序执行POST时,我们会收到错误消息。
安全性:
@Configuration
@EnableWebSecurity
public class SecurityWebConfig extends WebSecurityConfigurerAdapter {
@Autowired
private ImplementsUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/resources/**").permitAll()
.antMatchers("/api/**").permitAll()
.antMatchers(HttpMethod.POST, "/fornecedor/lista").hasRole("ADMIN")
.antMatchers(HttpMethod.POST, "/fornecedor/cadastrarFornecedor").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
.and()
.rememberMe()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login")
.permitAll()
.and()
.rememberMe()
.userDetailsService(userDetailsService).and().csrf()
.and().exceptionHandling().accessDeniedPage("/Access_Denied");
http
.sessionManagement()
.sessionFixation().migrateSession()
.invalidSessionUrl("/login")
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.expiredUrl("/login")
.sessionRegistry(sessionRegistry());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/css/**", "/data-table/**", "/Doc/**", "/font-awesome/**", "/img/**", "/javascripts/**", "/js/**", "/stylesheets/**");
}
@Bean
SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
返回:
<!DOCTYPE html>
<html xmlns="http://www.thymeleaf.org/extras/dialect">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CompraFacil</title>
<!-- Bootstrap CSS-->
<link type="text/css" rel="stylesheet"
href="/css/bootstrap.min.css">
<!-- Google fonts - Roboto -->
<link type="text/css" rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
<!-- theme stylesheet-->
<link type="text/css" rel="stylesheet" href="/css/style.blue.css"
id="theme-stylesheet">
<!-- Custom stylesheet - for your changes-->
<link type="text/css" rel="stylesheet" href="/css/custom.css">
<!-- Favicon-->
<link rel="shortcut icon" href="img/favicon.ico">
<!-- you can replace it by local Font Awesome-->
<script type="application/javascript"
src="https://use.fontawesome.com/99347ac47f.js"></script>
<!-- Font Icons CSS-->
<link type="text/css" rel="stylesheet"
href="https://file.myfontastic.com/da58YPMQ7U5HY8Rb6UxkNf/icons.css">
<script type="text/javascript"
src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<form action="/login" method="post">
<input type="hidden" name="_csrf" value="c78362dd-e239-4b46-959b-0c3df8347abc"/>
<div class="page login-page">
<div class="container d-flex align-items-center">
<div class="form-holder has-shadow">
<div class="row">
<!-- Logo & Information Panel-->
<div class="col-lg-6">
<div class="info d-flex align-items-center">
<div class="content">
<div class="logo">
<h1>CompraFacil</h1>
</div>
<p>Uma plataforma de ecommerce B2C.</p>
</div>
</div>
</div>
<!-- Form Panel -->
<div class="col-lg-6 bg-white">
<div class="form d-flex align-items-center">
<div class="content">
<div class="form-group">
<input type="text" name="username" class="input-material">
<label for="login-username" placeholder="exemplo@dominio.com"
class="label-material">E-mail</label>
</div>
<div class="form-group">
<input type="password" name="password" class="input-material">
<label for="login-password" class="label-material">Senha</label>
</div>
<input type="submit" class="btn btn-primary btn-lg btn-block"
value="Acessar">
<br>
<div class="form-group">
<input type="checkbox" id="remember-me" name="remember-me" />
<label for="remember-me">Lembrar?</label>
</div>
<!-- This should be submit button but I replaced it with <a> for demo purposes-->
<a href="#" data-toggle="modal" data-target="#myModal"
data-placement="right" class="forgot-pass">Esqueceu a
senha?</a>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="copyrights text-center">
<p>
Design by
<a href="https://bootstrapious.com/admin-templates"
class="external">Bootstrapious</a>
</p>
<!-- Please do not remove the backlink to us unless you support further theme's development at https://bootstrapious.com/donate. It is part of the license conditions. Thank you for understanding :)-->
</div>
</div>
<input type="hidden" name="_csrf"
value="c78362dd-e239-4b46-959b-0c3df8347abc" />
</form>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Informe seu endereço
de e-mail</h4>
</div>
<div class="modal-body">
<form action="@{Logins.esqueciSenha}" method="POST">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Digite seu e-mail:</label>
<div class="input-group input-group-lg">
<input type="email" name="email" value="${flash['email']}"
class="form-control" title="Informe o seu endereço de email."
aria-describedby="sizing-addon7" required>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Enviar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
我的安全可能以某种方式阻止了我的职位,我无法确定,我是spring的新人。