扩展 Jhipster JWT (Spring) 单体应用程序以支持模拟

Extending a Jhipster JWT (Spring) monolith application to support impersonation

我生成了一个使用 JWT 身份验证的 jhipster angular/java 应用程序。

我现在想扩展应用程序以支持模拟。

我有兴趣实现以下目标:

我看到 Spring 支持模拟,但我不清楚如何在使用 JWT 的情况下在我的 Jhipster 应用程序中正确实施它。我不确定 Spring 路由是否适合 JHipster-JWT-Monolith 应用程序 - 我认为这不是正确的方法。

虽然有一些关于其他各种 post 的不完整信息,但经过广泛搜索后,我无法找到 post 可以提供清晰的分步指南。如果有人能为我做到这一点,我将不胜感激。我希望其他人也会发现这样的答案非常有用。

提前致谢。 弗加尔

您只需要在 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);
    }