@Test public void login() throws Exception { GroupDO group = GroupDO.builder().name("少林足球").info("致敬周星星").build(); groupMapper.insert(group); RegisterDTO dto = new RegisterDTO(); dto.setGroupIds(Arrays.asList(group.getId())); dto.setEmail(email); dto.setConfirmPassword(password); dto.setPassword(password); dto.setUsername(username); userService.createUser(dto); LoginDTO dto1 = new LoginDTO(); dto1.setUsername(username); dto1.setPassword(password); ObjectMapper mapper = new ObjectMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); String content = mapper.writeValueAsString(dto1); mvc.perform(post("/cms/user/login") .contentType(MediaType.APPLICATION_JSON).content(content)) .andDo(print()) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.access_token").isNotEmpty()) .andExpect(MockMvcResultMatchers.jsonPath("$.refresh_token").isNotEmpty()); }
@PostMapping("/login") public Tokens login(@RequestBody @Validated LoginDTO validator) { UserDO user = userService.getUserByUsername(validator.getUsername()); if (user == null) { throw new NotFoundException("user not found", 10021); } boolean valid = userIdentityService.verifyUsernamePassword( user.getId(), user.getUsername(), validator.getPassword()); if (!valid) throw new ParameterException("username or password is fault", 10031); return jwt.generateTokens(user.getId()); }
UserController { @PostMapping("/login") public Tokens login(@RequestBody @Validated LoginDTO validator) { UserDO user = userService.getUserByUsername(validator.getUsername()); if (user == null) { throw new NotFoundException("user not found", 10021); } boolean valid = userIdentityService.verifyUsernamePassword( user.getId(), user.getUsername(), validator.getPassword()); if (!valid) throw new ParameterException("username or password is fault", 10031); return jwt.generateTokens(user.getId()); } }
UserController { @PostMapping("/login") public Tokens login(@RequestBody @Validated LoginDTO validator) { UserDO user = userService.getUserByUsername(validator.getUsername()); if (user == null) { throw new NotFoundException("user not found", 10021); } boolean valid = userIdentityService.verifyUsernamePassword( user.getId(), user.getUsername(), validator.getPassword()); if (!valid) throw new ParameterException("username or password is fault", 10031); return jwt.generateTokens(user.getId()); }  }
UserController { @PostMapping("/login") public Tokens login(@RequestBody @Validated LoginDTO validator) { UserDO user = userService.getUserByUsername(validator.getUsername()); if (user == null) { throw new NotFoundException("user not found", 10021); } boolean valid = userIdentityService.verifyUsernamePassword( user.getId(), user.getUsername(), validator.getPassword()); if (!valid) throw new ParameterException("username or password is fault", 10031); return jwt.generateTokens(user.getId()); }  @PostMapping("/register") @AdminRequired UnifyResponseVO<String> register(@RequestBody @Validated RegisterDTO validator); @PostMapping("/login") Tokens login(@RequestBody @Validated LoginDTO validator); @PutMapping @LoginRequired UnifyResponseVO update(@RequestBody @Validated UpdateInfoDTO validator); @PutMapping("/change_password") @LoginRequired UnifyResponseVO updatePassword(@RequestBody @Validated ChangePasswordDTO validator); @GetMapping("/refresh") @RefreshRequired Tokens refreshToken(); @GetMapping("/permissions") @LoginRequired @RouteMeta(permission = "查询自己拥有的权限", module = "用户", mount = true) UserPermissionsVO getPermissions(); @LoginRequired @RouteMeta(permission = "查询自己信息", module = "用户", mount = true) @GetMapping("/information") UserInfoVO getInformation(); }
UserController { @PostMapping("/login") public Tokens login(@RequestBody @Validated LoginDTO validator) { UserDO user = userService.getUserByUsername(validator.getUsername()); if (user == null) { throw new NotFoundException("user not found", 10021); } boolean valid = userIdentityService.verifyUsernamePassword( user.getId(), user.getUsername(), validator.getPassword()); if (!valid) throw new ParameterException("username or password is fault", 10031); return jwt.generateTokens(user.getId()); }  @PostMapping("/register") @AdminRequired UnifyResponseVO<String> register(@RequestBody @Validated RegisterDTO validator); @PostMapping("/login") Tokens login(@RequestBody @Validated LoginDTO validator); @PutMapping @LoginRequired UnifyResponseVO update(@RequestBody @Validated UpdateInfoDTO validator); @PutMapping("/change_password") @LoginRequired UnifyResponseVO updatePassword(@RequestBody @Validated ChangePasswordDTO validator); @GetMapping("/refresh") @RefreshRequired Tokens refreshToken(); @GetMapping("/permissions") @LoginRequired @RouteMeta(permission = "查询自己拥有的权限", module = "用户", mount = true) UserPermissionsVO getPermissions(); @LoginRequired @RouteMeta(permission = "查询自己信息", module = "用户", mount = true) @GetMapping("/information") UserInfoVO getInformation();  }