创建/注册具有 spring 安全性的新用户?
Creation / registration new user with spring security?
我正在学习和开发 Restful api 作为我未来 android 应用程序的后端。我使用 spring 框架。我使用 spring 安全来限制对某些资源的访问。
我有 UserController 方法 create(params..) 应该创建新用户。
@RestController
public class UserController {
private UserService userService;
private CreateFormValidator createFormValidator;
@Autowired
public UserController(UserService userService, CreateFormValidator createFormValidator) {
this.userService = userService;
this.createFormValidator = createFormValidator;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.addValidators(createFormValidator);
}
@RequestMapping(value = "/user/create", method = RequestMethod.POST)
String create(@RequestBody @Valid CreateForm createForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "user_create";
}
try {
userService.create(createForm);
} catch (DataIntegrityViolationException e) {
bindingResult.reject("username.exists", "Username already exists");
return "user_create";
}
return "redirect:/login";
}
}
和安全配置,现在我在这里只指定我希望 "user/create/" 对所有人都是允许的。
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("user/create").permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
}
但是当我测试它并在 "user/create" 上使用 json body
执行 POST
{
"username" : "name",
"password" : "1234",
"repeatedPassword" : "1234"
}
我得到:
"error": "Forbidden" "message": "Invalid CSRF Token 'null' was found
on the request parameter '_csrf' or header 'X-CSRF-TOKEN'."
你能解释一下为什么在配置 class 中禁止此 url 的 permitAll 访问吗?以及如何解决?提前致谢!
如果您使用的是 rest 客户端,请包含 X-CSRF-TOKEN 以请求 header。您将从您对服务的获取请求之一获取 csrf 令牌值。
或者尝试以下操作:
{
"username" : "name",
"password" : "1234",
"repeatedPassword" : "1234",
"_csrf" : "token value"
}
我正在学习和开发 Restful api 作为我未来 android 应用程序的后端。我使用 spring 框架。我使用 spring 安全来限制对某些资源的访问。 我有 UserController 方法 create(params..) 应该创建新用户。
@RestController
public class UserController {
private UserService userService;
private CreateFormValidator createFormValidator;
@Autowired
public UserController(UserService userService, CreateFormValidator createFormValidator) {
this.userService = userService;
this.createFormValidator = createFormValidator;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.addValidators(createFormValidator);
}
@RequestMapping(value = "/user/create", method = RequestMethod.POST)
String create(@RequestBody @Valid CreateForm createForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "user_create";
}
try {
userService.create(createForm);
} catch (DataIntegrityViolationException e) {
bindingResult.reject("username.exists", "Username already exists");
return "user_create";
}
return "redirect:/login";
}
}
和安全配置,现在我在这里只指定我希望 "user/create/" 对所有人都是允许的。
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("user/create").permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
}
但是当我测试它并在 "user/create" 上使用 json body
执行 POST{
"username" : "name",
"password" : "1234",
"repeatedPassword" : "1234"
}
我得到:
"error": "Forbidden" "message": "Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'."
你能解释一下为什么在配置 class 中禁止此 url 的 permitAll 访问吗?以及如何解决?提前致谢!
如果您使用的是 rest 客户端,请包含 X-CSRF-TOKEN 以请求 header。您将从您对服务的获取请求之一获取 csrf 令牌值。
或者尝试以下操作:
{
"username" : "name",
"password" : "1234",
"repeatedPassword" : "1234",
"_csrf" : "token value"
}