【发布时间】:2018-05-25 00:47:03
【问题描述】:
我正在开发一个处理登录的示例 spring-boot API,该 API 工作正常,但是在错误处理期间我遇到了问题。我在教程中看到的所有错误处理都是在控制器中完成的,但是我的逻辑在服务中,这就是我想做错误处理的地方。
如何实现对服务中不同类型错误的错误处理,或者这样做被认为是不好的做法。我目前的问题是我的错误消息如下所示:
{
"message": "Please Activate account before attempting to Sign in",
"token": " ",
"accountLocked": false,
"accountEnabled": false,
"status": false,
"balance": " ",
"profileImage": null,
"roles": null,
"baraAuthorities": null,
"customerNumber": 0,
"userEmail": "",
"name": ""
}
但是我希望只显示消息和状态并在响应中隐藏其余部分,我该如何实现:
{
"message": "Please Activate account before attempting to Sign in",
"status": false
}
这是我的 LoginService 代码:
@Service
public class LoginService implements UserDetailsService {
@Autowired
private UserLoginRepository loginRepository;
@Autowired
private LoginInformationRepository logininfoRepository;
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String balance, message, pin, token, userEmail, name, profileImage = "";
Boolean status, successStatus, accounState = false;
int customerNumber, attempts;
List<Roles> userRoles = new ArrayList();
private boolean userNameExist(String username) {
UserLogin user = loginRepository.findByUsername(username);
if (user != null) {
return true;
}
return false;
}
public LoginResponse verifyLoginDetails(LoginObject user) throws LoginException {
AuthoritiesKeys userAuth = new AuthoritiesKeys();
Merchant searchedMerchant = merchantRepository.findByUserlogin_LoginCredentialsID(userdetails.getLoginCredentialsID());
DateUtility currentDate = new DateUtility();
Boolean status = false;
if (userdetails == null) {
return new LoginResponse("Unable to Login. Please check login details and try again",status);
} else {
pin = user.getPassword();
attempts = logininfoRepository.countByTodayDateAndUserLoginLoginCredentialsIDAndLoginSuccessStatusFalse(currentDate.getCurrentDate(), userdetails.getLoginCredentialsID());
if (attempts < 3) {
if (bCryptPasswordEncoder.matches(pin, userdetails.getPassword())) {
if (userdetails.isEnabled() == true) {
if (searchedMerchant != null) {
message = "Logged in Successfully";
status = userdetails.isAccountNonLocked();
accounState = userdetails.isEnabled();
userRoles = (List<Roles>) userdetails.getAuthorities();
balance = searchedMerchant.getAccount().getBalance().toString();
successStatus = true;
customerNumber = searchedMerchant.getMerchantNumber();
userEmail = searchedMerchant.getEmail();
name = searchedMerchant.getBusinessName();
return new LoginResponse(message, token, accounState, status, userRoles, successStatus, balance, userAuth, profileImage, customerNumber, userEmail, name);
} else {
return new LoginResponse("Error Merchant Information Error . Please contact the team",status);
}
} else {
return new LoginResponse("Please Activate account before attempting to Sign in",status);
}
} else {
return new LoginResponse("Wrong Username or Password",status);
}
} else {
userdetails.setAccountNonLocked(false);
userdetails.setEnabled(false);
loginRepository.save(userdetails);
return new LoginResponse("Too many attempts account has been locked",status);
}
}
}
这是我的 LoginResponse 代码:
public class LoginResponse {
private String message;
private String token;
private Boolean accountLocked;
private Boolean accountEnabled;
private Boolean status;
private String balance;
private String profileImage;
List<Roles> roles = new ArrayList<>();
AuthoritiesKeys baraAuthorities = new AuthoritiesKeys();
private int customerNumber;
private String userEmail;
private String name;
public LoginResponse() {
}
public LoginResponse(String message, Boolean status) {
this.message = message;
this.status = status;
}
public LoginResponse(String message, String token, Boolean accountLocked, Boolean accountEnabled, List<Roles> myroles, Boolean status, String balance, AuthoritiesKeys userBaraAuthorities, String profileImage, int customerNumber, String userEmail, String name) {
this.message = message;
this.token = token;
this.accountLocked = accountLocked;
this.accountEnabled = accountEnabled;
this.status = status;
this.balance = balance;
this.roles = myroles;
this.baraAuthorities = userBaraAuthorities;
this.profileImage = profileImage;
this.customerNumber = customerNumber;
this.userEmail = userEmail;
this.name = name;
}
… // left getter and setters for brevity
}
这是我的 LoginController 代码:
@RestController
@Api(value = "Login API", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserLoginController {
@Autowired
private LoginService loginService;
@RequestMapping(method = RequestMethod.POST, value = "/api/usermanagement/user/login")
@ApiOperation("login registered user")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = ResponseMessage.class)})
public LoginResponse Login(@Valid @RequestBody LoginObject credentails) throws LoginException {
return loginService.verifyLoginDetails(credentails);
}
}
【问题讨论】:
-
错误处理应该在大多数级别进行,而且如果有错误,那么它是否与用户相关,如果是,您建议如何让他们知道吗?
-
我更新了问题
标签: java spring-boot error-handling