在我的html中,我添加了以下指令
import org.keycloak.KeycloakPrincipal;
import org.keycloak.adapters.RefreshableKeycloakSecurityContext;
import org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
@Component
public class AnotherServiceClient {
public TypeOfObjectReturnedByAnotherService getFromAnotherService() {
RestTemplate restTemplate = new RestTemplate();
String endpoint = "http://localhost:40030/another/service/url";
String bearerToken = getAuthorizationToken();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "bearer " + bearerToken);
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<TypeOfObjectReturnedByAnotherService> response = restTemplate.exchange(endpoint, HttpMethod.GET, entity, TypeOfObjectReturnedByAnotherService.class);
return response.getBody();
}
private String getAuthorizationToken() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
SimpleKeycloakAccount details = (SimpleKeycloakAccount) authentication.getDetails();
KeycloakPrincipal<?> keycloakPrincipal = (KeycloakPrincipal<?>) details.getPrincipal();
RefreshableKeycloakSecurityContext context = (RefreshableKeycloakSecurityContext) getPrincipal().getKeycloakSecurityContext();
return context.getTokenString();
}
}
指令:
<html ng-app="xxxxx" bm-app="auth">
我只想验证用户是否已通过身份验证。如果不是,我需要使用自定义指令将用户重定向到登录页面。我是AngularJs的新手,请有人帮我解决这个问题。