传递 POST 请求。春季安全。休息模板。杰克逊转换器

Posted

技术标签:

【中文标题】传递 POST 请求。春季安全。休息模板。杰克逊转换器【英文标题】:Passing POST request. Spting security. Rest template. Jackson converter 【发布时间】:2017-08-19 03:07:18 【问题描述】:

我是 Spring 框架的初学者,我想问你一个问题。 我如何正确传递 POST 请求以在数据库中注册我的用户。

我有一个 android 客户端,我找到了使用网络服务的唯一方法。 我正在使用 AsyncTask 来管理请求。

但我不知道如何使用我的User 类发送 POST 请求以将他注册到数据库中。 数据库的逻辑已经完成。

我请你帮我如何发布请求以及如何在服务器上管理他。

这是我的代码:

服务器端,用户控制器:

@RestController
@RequestMapping("/user")
public class UserController 

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;


    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    @ResponseBody
    public User registration(@RequestBody User user, BindingResult bindingResult, Model model) 
        userValidator.validate(user, bindingResult);// here is validate logic
        if (bindingResult.hasErrors()) 
            //What should i return here to my Android client ?
        
        userService.save(user);
        securityService.autoLogin(user.getUserName(), user.getConfirmPassword());
        return user;
    

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String error, String logout) 
        if (error != null) 
            model.addAttribute("error", "Username or password is incorrect.");
        

        if (logout != null) 
            model.addAttribute("message", "Logged out successfully.");
        

        return "login";// what should I return to the client ???
        //How my android client will understand that user is loged in ?
    

接下来我有客户端(这里有一些 MVP 逻辑,但 nvm):

public class CreateAccountPresenter implements CreateAccountContract.Presenter 
    private CreateAccountContract.View view;
    private String userName;
    private String userPassword;
    private String confirmUserPassword;


    public CreateAccountPresenter(CreateAccountContract.View view) 
        this.view = view;
    

    @Override
    public void onCreateAccountClick() 
        userName = view.getUserName();
        userPassword = view.getPassword();
        confirmUserPassword = view.getPasswordConfirmation();
        new CreateAccountTask().execute();

    

    private class CreateAccountTask extends AsyncTask<Void, Void, User>
        // How do I properly pass the post request with my User object ?
        @Override
        protected User doInBackground(Void... voids) 
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            User user = new User(userName, userPassword, confirmUserPassword);
            return restTemplate.postForObject(URL.getUserRegistration(), user, User.class);
        

        @Override
        protected void onPostExecute(User user) 
            view.makeToast("User registration complited " + user.getUserName());
        
    


我需要在客户端初始化转换器吗? 寻求帮助,如果您在 cmets 中发布一些代码也将很棒。

更新 我更改了一点客户端代码,现在我在客户端收到下一个错误:

W/RestTemplate: POST request for "http://192.168.0.80:8080/user/registration" resulted in 400 (Bad Request); invoking error handler
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                  Process: com.example.user.userauthorisation, PID: 4676
                  java.lang.RuntimeException: An error occured while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:300)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                      at java.lang.Thread.run(Thread.java:818)
                   Caused by: org.springframework.web.client.HttpClientErrorException: 400 Bad Request
                      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
                      at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:585)
                      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541)
                      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499)
                      at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:348)
                      at com.example.user.userauthorisation.createaccount.CreateAccountPresenter$CreateAccountTask.doInBackground(CreateAccountPresenter.java:42)
                      at com.example.user.userauthorisation.createaccount.CreateAccountPresenter$CreateAccountTask.doInBackground(CreateAccountPresenter.java:35)
                      at android.os.AsyncTask$2.call(AsyncTask.java:288)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                      at java.lang.Thread.run(Thread.java:818) 
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeb89dde0, error=EGL_SUCCESS

在服务器端:

2254021 [http-apr-8080-exec-2] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized field "confirmationPassword" (class com.webserverconfig.user.entity.User), not marked as ignorable (4 known properties: "id", "confirmPassword", "userName", "userPassword"])
 at [Source: java.io.PushbackInputStream@2cbfcb7c; line: 1, column: 26] (through reference chain: com.webserverconfig.user.entity.User["confirmationPassword"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "confirmationPassword" (class com.webserverconfig.user.entity.User), not marked as ignorable (4 known properties: "id", "confirmPassword", "userName", "userPassword"])
 at [Source: java.io.PushbackInputStream@2cbfcb7c; line: 1, column: 26] (through reference chain: com.webserverconfig.user.entity.User["confirmationPassword"])

有什么帮助吗?

【问题讨论】:

【参考方案1】:

您没有共享您的User bean,但错误日志显示您正在尝试访问字段confirmationPassword,而只有confirmPassword。检查 bean 的 getter 名称。

【讨论】:

是的,我已经解决了。现在注册方法工作正常。我一个,只有登录方法有问题。

以上是关于传递 POST 请求。春季安全。休息模板。杰克逊转换器的主要内容,如果未能解决你的问题,请参考以下文章

休息模板 - 如何在发送之前检索原始 POST 请求字符串 [重复]

春季安全+休息不起作用

如何使用jackson映射动态json属性名称以进行spring rest模板请求

春季文件上传 - '所需的请求部分不存在'

Gatling 2.0.3 - 将值映射到模板

春季靴子泽西杰克逊