【问题标题】: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);
        }
    

    【讨论】:

    • 当你想用管理员用户登录时调用这个api
    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2018-12-08
    相关资源
    最近更新 更多