【发布时间】:2019-11-03 01:00:37
【问题描述】:
由于 Jhipster(Angular + Springboot Scaffolding) 使用 JWT 进行身份验证,我们有一个自定义登录模型来检查用户身份验证。现在代码变成了这样(UserJWTController.java):
@PostMapping("/authenticate")
@Timed
public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginVM loginVM) {
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(loginVM.getUsername(), loginVM.getPassword());
Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
boolean rememberMe = (loginVM.isRememberMe() == null) ? false : loginVM.isRememberMe();
String jwt = tokenProvider.createToken(authentication, rememberMe);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);
}
据观察,当用户输入无效凭据时,会引发错误。前端(角度)通过在模态本身中显示文本来处理错误。
现在,假设我还需要添加自定义验证。例如,我需要检查该用户的姓氏是否为“Mike”,然后抛出一个自定义错误,提示“Mike 无法登录”。
问题是 authenticate() (org.springframework.security.authentication.AuthenticationManager) 方法默认抛出 DisabledException,LockedException 和 BadCredentialsException。
我对 Spring Security 没有经验,所以我真的不明白我们如何覆盖类并添加所需的功能。
此外,Angular 捕获了一个通用异常,所以我的猜测是我们需要添加另一个约束:
.catch(() => {
this.authenticationError = true;
});
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
标签: angular spring spring-boot spring-security jhipster