문제는 새 대상 사용자 정보가 들어있는 새 토큰을 만들어야한다는 것입니다. 이 새 토큰은 클라이언트에 다시 보내 져야하므로 이후 요청시 새 대상 사용자 토큰이 사용됩니다. 이 경우 토큰은 (JDBCTokenStore를 사용하여) 서버 측에서 유지되지만 서버 측의 완전 무 상태 환경 (JWT-Token)에서도 작동합니다.
우리의 환경은 1.2 버전의 클라이언트가있는 spring-boot/jhipster 응용 프로그램입니다. 이 새로운 토큰은 로컬 스토리지에있는 토큰을 저장 (우리의 경우 각도 1.2 응용 프로그램에서) 클라이언트에 반환됩니다
@Inject
private UserDetailsService userDetailsService;
@Inject
private AuthorizationServerTokenServices tokenService;
@Inject
private ClientDetailsService clientDetailsService;
public OAuth2AccessToken createImpersonationAccessToken(String login) {
UserDetails userDetails = userDetailsService.loadUserByUsername(login);
log.info("Switching current user to {}", login);
Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
List<GrantedAuthority> impersonationAuthorities = new ArrayList<>(authorities);
Authentication source = SecurityContextHolder.getContext().getAuthentication();
// add current user authentication (to switch back from impersonation):
SwitchUserGrantedAuthority switchUserAuthority =
new SwitchUserGrantedAuthority(AuthoritiesConstants.IMPERSONATION, source);
impersonationAuthorities.add(switchUserAuthority);
UserDetails newUserDetails =
org.springframework.security.core.userdetails.User
.withUsername(login)
.authorities(impersonationAuthorities)
.password("justinventedhere")
.build();
Authentication userPasswordAuthentiation =
new UsernamePasswordAuthenticationToken(newUserDetails, null, impersonationAuthorities);
Map<String, String> parameters = new HashMap<>();
ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
OAuth2Request oauthRequest = new OAuth2Request(parameters, client.getClientId(), client.getAuthorities(), true,
client.getScope(), client.getResourceIds(), null, null, null);
OAuth2Authentication authentication = new OAuth2Authentication(oauthRequest, userPasswordAuthentiation);
OAuth2AccessToken createAccessToken = tokenService.createAccessToken(authentication);
return createAccessToken;
}
(다음 요청에 사용되는) :
새로운 토큰을 생성. 그런 다음 응용 프로그램을 다시로드해야합니다 (대상 사용자를 업데이트하는 가장 간단한 방법).
vm.switchToClient = function (client) {
vm.switchingUser = true;
UserService.switchToClient(client, function(response) {
var expiredAt = new Date();
$localStorage.authenticationToken = response;
window.location.href='#/';
window.location.reload()
});
}