簡體   English   中英

在ResourceSupport中設置實體中不存在的表單字段的正確位置

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

我有一個用於在我的REST API中登錄用戶的模型,對應於User表(電子郵件和密碼作為表列)

@Entity
public class User {

@Id
@GeneratedValues
private Long id;

private String email;
private String password;

+GET , +SET

}

然后有@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:-

        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:-

    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;
        }

    }

有2個問題-

  • 我需要發送一個成功標志為布爾值作為響應,為此我向AuthLoginFormResource添加了布爾值成功。 但是,只能通過AuthLoginFormResourceAsm.toResource方法設置AuthLoginFormResource,而AuthLoginFormResource方法則是通過實體User進行設置的。 由於用戶實體模型數據庫沒有成功列,因此我無法在此設置成功。

    因此,我應該將虛擬成功字段添加到用戶實體並從服務方法中進行設置,盡管數據庫中沒有這樣的字段,還是在此處創建一個代表登錄表單的新實體並返回?

  • 與另一個字段相同的問題是身份驗證令牌,該字段在數據庫中不存在,但是是響應的一部分。

在ResourceSupport對象中設置此類字段的正確位置是什么-在數據庫實體內部並從Service返回/在Domain Model頂部創建另一個Form Model實體並從Service返回。

這是我在許多數據模型和表單不一一對應的地方面臨的基本問題。

我強烈建議以下內容:

  1. 修改UserService.login方法以基於成功身份驗證而不是從數據庫中檢索到的用戶對象返回true或false。
  2. 作為響應的一部分,僅返回狀態為OK和FAIL的true或false,而不是整個AuthLoginFormResource 這是一種不好的做法,因為您是在往返中來回發送usernamepassword作為請求和響應的一部分。 如果有人在竊聽,他們可以輕松找出哪些用戶名密碼有效,哪些無效。

要么

如果您喜歡此自定義身份驗證實現,請考慮使用基本授權,摘要授權或OAuth。 使用Spring Security,您可以真正輕松地實現上述任何目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM