收到 HTTP.POST 发布的对象
Recieving a object posted by HTTP.POST
您好,我正在尝试为我的 spring 应用发送电子邮件确认 api
用户需要输入 his/her 姓名、备注、company/job、电子邮件。后
收到此对象我想发送确认 link 到电子邮件。
这是HTML代码:
<form method="POST" th:object="${Signature}">
<label>Name : </label>
<input id="inputName" type="text" th:field="*{name}">
<label>Note : </label>
<input id="inputNote" type="text" th:field="*{note}">
<br>
<label>Company : </label>
<input id="inputCompany" type="text" th:field="*{company}">
<label>Contact Info : </label>
<input id="inputContact" type="email" th:field="*{email}" placeholder="This info will not be shared">
<button>Submit</button>
<br>
</form>
这是控制器
@Controller
@RequestMapping("/")
public class RootController
{
@GetMapping
public String root(Model model)
{
model.addAttribute("Signature", new Signature());
return "Public/Home";
}
@PostMapping
public String signPosted(Signature s)
{
System.out.println("Post Received");
return "redirect:/thanks";
}
}
注意:即使应用程序位于根目录 ("/") 上,用户在发帖时也必须转到 /#contact
这是对象class
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class Signature
{
//@NotBlank(message="Name is required")
private String name;
private String note;
//@NotBlank(message="Name is required")
private String email;
//@NotBlank(message="Name is required")
private String company;
public Signature(){}
public Signature(String name,
String note,
String email,
String company)
{
this.name = name;
this.note = note;
this.email = email;
this.company = company;
}
}
当我点击提交按钮时,它会将您带到错误页面并且消息是
There was an unexpected error (type=Forbidden, status=403).
我的安全配置是
http
.authorizeRequests()
.antMatchers("/admin/**")
.authenticated()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
这是因为您没有在 Spring 安全配置中禁用 CSRF 保护(为除 GET 之外的每个 HTTP 动词启用),同时您还没有在 [=18] 中发送 CSRF 令牌=]表格.
如果要禁用 CSRF 保护,请尝试此 Spring 安全配置:
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/admin/**")
.authenticated()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
如果您想保留 CSRF 保护并在 HTML 表单中添加 CSRF 令牌,请尝试此操作:
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
您好,我正在尝试为我的 spring 应用发送电子邮件确认 api 用户需要输入 his/her 姓名、备注、company/job、电子邮件。后 收到此对象我想发送确认 link 到电子邮件。
这是HTML代码:
<form method="POST" th:object="${Signature}">
<label>Name : </label>
<input id="inputName" type="text" th:field="*{name}">
<label>Note : </label>
<input id="inputNote" type="text" th:field="*{note}">
<br>
<label>Company : </label>
<input id="inputCompany" type="text" th:field="*{company}">
<label>Contact Info : </label>
<input id="inputContact" type="email" th:field="*{email}" placeholder="This info will not be shared">
<button>Submit</button>
<br>
</form>
这是控制器
@Controller
@RequestMapping("/")
public class RootController
{
@GetMapping
public String root(Model model)
{
model.addAttribute("Signature", new Signature());
return "Public/Home";
}
@PostMapping
public String signPosted(Signature s)
{
System.out.println("Post Received");
return "redirect:/thanks";
}
}
注意:即使应用程序位于根目录 ("/") 上,用户在发帖时也必须转到 /#contact
这是对象class
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
public class Signature
{
//@NotBlank(message="Name is required")
private String name;
private String note;
//@NotBlank(message="Name is required")
private String email;
//@NotBlank(message="Name is required")
private String company;
public Signature(){}
public Signature(String name,
String note,
String email,
String company)
{
this.name = name;
this.note = note;
this.email = email;
this.company = company;
}
}
当我点击提交按钮时,它会将您带到错误页面并且消息是
There was an unexpected error (type=Forbidden, status=403).
我的安全配置是
http
.authorizeRequests()
.antMatchers("/admin/**")
.authenticated()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
这是因为您没有在 Spring 安全配置中禁用 CSRF 保护(为除 GET 之外的每个 HTTP 动词启用),同时您还没有在 [=18] 中发送 CSRF 令牌=]表格.
如果要禁用 CSRF 保护,请尝试此 Spring 安全配置:
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/admin/**")
.authenticated()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
如果您想保留 CSRF 保护并在 HTML 表单中添加 CSRF 令牌,请尝试此操作:
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>