【问题标题】:Extending a Jhipster JWT (Spring) monolith application to support impersonation扩展 Jhipster JWT (Spring) 单体应用程序以支持模拟
【发布时间】:2020-03-05 21:47:41
【问题描述】:
我生成了一个使用 JWT 身份验证的 jhipster angular/java 应用程序。
我现在想扩展应用程序以支持模拟。
我有兴趣实现以下目标:
我看到 Spring 支持模拟,但鉴于使用了 JWT,我不清楚如何在我的 Jhipster 应用程序中正确实现它。我不确定 Spring 路线是否适合 JHipster-JWT-Monolith 应用程序 - 我认为这不是正确的方法。
虽然关于其他各种帖子的信息不完整,但经过广泛搜索后,我无法找到可以提供明确的分步指南的帖子。如果有人能为我做到这一点,将不胜感激。我希望其他人也会发现这样的答案非常有用。
提前致谢。
费格尔
【问题讨论】:
标签:
spring
jwt
jhipster
impersonation
【解决方案1】:
您只需要在 UserJwtController.java 中添加以下方法
@PostMapping("/authenticate-externalnodes")
public ResponseEntity<JWTToken> authenticateExternalnodes(@Valid @RequestBody LoginVM loginVM) {
// Get Roles for user via username
Set<Authority> authorities = userService.getUserWithAuthoritiesByLogin(loginVM.getUsername()).get()
.getAuthorities();
// Create Granted Authority Rules
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
for (Authority authority : authorities) {
grantedAuthorities.add(new SimpleGrantedAuthority(authority.getName()));
}
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
loginVM.getUsername(), "", grantedAuthorities);
Authentication authentication = 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(JWTFilter.AUTHORIZATION_HEADER, "Bearer " + jwt);
return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);
}