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

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

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

我对实现以下目标感兴趣:

  • 由管理员进行的模拟:允许管理员用户以任何其他用户身份登录

  • 授予用户的模仿::允许已被授予模仿用户(由用户本身授予的权限)的另一用户以该其他用户身份登录。

  • 审核-记录更改(审核功能)-审核记录必须能够区分实际用户和模拟用户,并将其记录在审核记录中。

我看到Spring支持模拟,但是对于使用JWT的人,我不清楚如何在Jhipster应用程序中正确实现模拟。我不确定Spring路线是否适合JHipster-JWT-Monolith应用程序-我认为这不是正确的方法。

尽管其他各种帖子上的信息不完整,但经过大量搜索之后,我一直找不到能提供清晰逐步指导的帖子。如果有人能为我做到这一点,将不胜感激。我希望其他人也会发现这样的答案非常有用。

先谢谢了。 弗加尔(Fergal)

smartbeth1 回答:扩展Jhipster JWT(Spring)整体应用程序以支持模拟

您只需要在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);
    }
本文链接:https://www.f2er.com/3132457.html

大家都在问