简体   繁体   English

在ResourceSupport中设置实体中不存在的表单字段的正确位置

[英]Proper place for setting form fields in ResourceSupport which don't exist in Entity

I have a model for logging in user in my REST API, corresponds to User table (email and password as table columns) 我有一个用于在我的REST API中登录用户的模型,对应于User表(电子邮件和密码作为表列)

@Entity
public class User {

@Id
@GeneratedValues
private Long id;

private String email;
private String password;

+GET , +SET

}

Then there is @Controller which is making call to above User Entity using JPAService 然后有@Controller正在使用JPAService调用上述用户实体

    @Controller
    @RequestMapping("/rest/auths")
    public class AuthController {

        @Autowired    
        private UserService authService;

        @RequestMapping(value = "/login", method = RequestMethod.POST)
            public @ResponseBody ResponseEntity<AuthLoginFormResource> login(@RequestBody AuthLoginFormResource sentAuth) {

            User user = authService.login(sentAuth.toUser());
            AuthLoginFormResource res = new AuthLoginFormResourceAsm().toResource(user); 

            HttpHeaders headers = new HttpHeaders();
                headers.setLocation(URI.create(res.getLink("self").getHref()));

                return new ResponseEntity<AuthLoginFormResource>(res, HttpStatus.OK);
            }
       }

AuthLoginFormResource : - AuthLoginFormResource:-

        public class AuthLoginFormResource extends ResourceSupport {

            private String email;
            private String password;
            private boolean success;

        public User toUser() { 

            User user = new User(); 
            user.setEmail(email);
            user.setPassword(password);
            //user.setSuccess(false);
            return user;

        }
        +GET, +SET
    }

AuthLoginFormResourceAsm : - AuthLoginFormResourceAsm:-

    public class AuthLoginFormResourceAsm extends ResourceAssemblerSupport<User, AuthLoginFormResource> {

        public AuthLoginFormResourceAsm() {
            super(User.class, AuthLoginFormResource.class);
        }

        @Override
        public AuthLoginFormResource toResource(User user) {

            AuthLoginFormResource res = new AuthLoginFormResource();
            res.setEmail(user.getEmail());
            res.setPassword(user.getPassword());
            //res.setSuccess(user.isSuccess()); // Success is not existing in USER

            res.add(linkTo(AuthController.class).withSelfRel());

            return res;
        }

    }

There are 2 issues - 有2个问题-

  • I need to send a success flag as boolean in response for which i have added a boolean success to AuthLoginFormResource. 我需要发送一个成功标志为布尔值作为响应,为此我向AuthLoginFormResource添加了布尔值成功。 But, AuthLoginFormResource gets set only from AuthLoginFormResourceAsm.toResource method , which in turn does it from entity User. 但是,只能通过AuthLoginFormResourceAsm.toResource方法设置AuthLoginFormResource,而AuthLoginFormResource方法则是通过实体User进行设置的。 As User entity models database where there is no success column, I am not able to set success at this place. 由于用户实体模型数据库没有成功列,因此我无法在此设置成功。

    So, should I add dummy success field to User Entity and set that from service method , though there is no such field in database or create a new Entity representing Login Form here and return that ? 因此,我应该将虚拟成功字段添加到用户实体并从服务方法中进行设置,尽管数据库中没有这样的字段,还是在此处创建一个代表登录表单的新实体并返回?

  • Same problem with another field that is a token for authentication which does not exist in database but is part of response. 与另一个字段相同的问题是身份验证令牌,该字段在数据库中不存在,但是是响应的一部分。

What is correct place for setting such fields in ResourceSupport object - inside database Entity and return from Service / creating another Form Model entity on top of Domain Model and return from service. 在ResourceSupport对象中设置此类字段的正确位置是什么-在数据库实体内部并从Service返回/在Domain Model顶部创建另一个Form Model实体并从Service返回。

This is basic question I am facing in many places where data model and forms don't match one to one. 这是我在许多数据模型和表单不一一对应的地方面临的基本问题。

I strongly recommend the following; 我强烈建议以下内容:

  1. Modify UserService.login method to return true or false based on successfull authentication instead of retrieved user object from database. 修改UserService.login方法以基于成功身份验证而不是从数据库中检索到的用户对象返回true或false。
  2. Return only true or false with status OK and FAIL, as part of the response not the entire AuthLoginFormResource . 作为响应的一部分,仅返回状态为OK和FAIL的true或false,而不是整个AuthLoginFormResource This is a bad practice because you are sending out the username and password as part of the request and response, back and forth in a roundtrip. 这是一种不好的做法,因为您是在往返中来回发送usernamepassword作为请求和响应的一部分。 If someone is evesdropping they can easily figure out what username passwords work and what don't. 如果有人在窃听,他们可以轻松找出哪些用户名密码有效,哪些无效。

Or 要么

Consider using Basic Authorization, Digest Authorization or OAuth if you fancy than this custom Authentication Implementation. 如果您喜欢此自定义身份验证实现,请考虑使用基本授权,摘要授权或OAuth。 Using Spring Security you can achieve any of the aforementioned really easily. 使用Spring Security,您可以真正轻松地实现上述任何目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM