org.springframework.security.oauth2.client.OAuth2RestTemplate.getForObject()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中org.springframework.security.oauth2.client.OAuth2RestTemplate.getForObject()方法的一些代码示例,展示了OAuth2RestTemplate.getForObject()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OAuth2RestTemplate.getForObject()方法的具体详情如下:
包路径:org.springframework.security.oauth2.client.OAuth2RestTemplate
类名称:OAuth2RestTemplate
方法名:getForObject

OAuth2RestTemplate.getForObject介绍

暂无

代码示例

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

/**
 * Get the authenticated user from the user service
 *
 * @return the currently authenticated user
 */
public User getAuthenticatedUser() {
  return oAuth2RestTemplate.getForObject("http://user-service/uaa/v1/me", User.class);
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

public boolean validateAccountNumber(String accountNumber) throws Exception {
    Account[] accounts = oAuth2RestTemplate.getForObject("http://account-service/v1/accounts", Account[].class);

    // Ensure account number is owned by the authenticated user
    if (accounts != null &&
        !Arrays.asList(accounts).stream().anyMatch(acct ->
            Objects.equals(acct.getAccountNumber(), accountNumber))) {
      throw new Exception("Account number invalid");
    }

    return true;
  }
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

/**
 * Get the shopping cart for the currently authenticated user
 *
 * @return an aggregate object derived from events performed by the user
 * @throws Exception
 */
public ShoppingCart getShoppingCart() throws Exception {
  User user = oAuth2RestTemplate.getForObject("http://user-service/uaa/v1/me", User.class);
  ShoppingCart shoppingCart = null;
  if (user != null) {
    Catalog catalog = restTemplate.getForObject("http://catalog-service/v1/catalog", Catalog.class);
    shoppingCart = aggregateCartEvents(user, catalog);
  }
  return shoppingCart;
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

public Order createOrder(List<LineItem> lineItems) {
  Account[] accounts = oAuth2RestTemplate.getForObject("http://account-service/v1/accounts", Account[].class);
  Account defaultAccount = Arrays.asList(accounts).stream()
      .filter(Account::getDefaultAccount)
      .findFirst().orElse(null);
  if (defaultAccount == null) {
    return null;
  }
  Order newOrder = new Order(defaultAccount.getAccountNumber(), defaultAccount.getAddresses().stream()
      .filter(address -> address.getAddressType() == AddressType.SHIPPING)
      .findFirst()
      .orElseThrow(() -> new RuntimeException("Default account does not have a shipping address")));
  newOrder.setLineItems(lineItems);
  newOrder = orderRepository.save(newOrder);
  return newOrder;
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

public List<Account> getUserAccounts() {
    List<Account> account = null;
    User user = oAuth2RestTemplate.getForObject("http://user-service/uaa/v1/me", User.class);
    if (user != null) {
      account = accountRepository.findAccountsByUserId(user.getUsername());
    }

    // Mask credit card numbers
    if (account != null) {
      account.forEach(acct -> acct.getCreditCards()
          .forEach(card ->
              card.setNumber(card.getNumber()
                  .replaceAll("([\\d]{4})(?!$)", "****-"))));
    }

    return account;
  }
}

代码示例来源:origin: kbastani/spring-cloud-event-sourcing-example

oAuth2RestTemplate.getForObject(String.format("http://inventory-service/v1/inventory?productIds=%s", currentCart.getLineItems()
    .stream()
    .map(LineItem::getProductId)
User user = oAuth2RestTemplate.getForObject("http://user-service/uaa/v1/me", User.class);

代码示例来源:origin: chaokunyang/microservices-event-sourcing

/**
 * 从user-service得到已验证的用户
 * @return 当前验证的用户
 */
public User getAuthenticatedUser() {
  //System.out.println(oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class));
  return oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class);
}

代码示例来源:origin: longfeizheng/sso-merryyou

@RequestMapping("/api")
String home(Model model) {
  String result = oAuth2RestTemplate.getForObject(messagesUrl + "/2", String.class);
  return result;
}

代码示例来源:origin: PacktPublishing/OAuth-2.0-Cookbook

private void tryToGetUserProfile(ModelAndView mv) {
  String endpoint = "http://localhost:8080/api/profile";
  try {
    UserProfile userProfile = restTemplate.getForObject(endpoint, UserProfile.class);
    mv.addObject("profile", userProfile);
  } catch (HttpClientErrorException e) {
    throw new RuntimeException("it was not possible to retrieve user profile");
  }
}

代码示例来源:origin: PacktPublishing/OAuth-2.0-Cookbook

private void tryToGetUserProfile(ModelAndView mv) {
  String endpoint = "http://localhost:8080/api/profile";
  try {
    UserProfile userProfile = restTemplate.getForObject(endpoint, UserProfile.class);
    mv.addObject("profile", userProfile);
  } catch (HttpClientErrorException e) {
    throw new RuntimeException("it was not possible to retrieve user profile");
  }
}

代码示例来源:origin: PacktPublishing/OAuth-2.0-Cookbook

@GetMapping("/dashboard")
public ModelAndView dashboard() {
  ModelAndView mv = new ModelAndView("dashboard");
  String endpoint = "http://localhost:8080/api/users";
  try {
    UserProfile[] users = restTemplate.getForObject(endpoint, UserProfile[].class);
    mv.addObject("users", users);
  } catch (HttpClientErrorException e) {
    throw new RuntimeException("it was not possible to retrieve all users");
  }
  return mv;
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

/**
 * 为当前已验证用户得到购物车
 * @return an aggregate object derived from events performed by the user
 * @throws Exception
 */
public ShoppingCart getShoppingCart() throws Exception {
  User user = oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class);
  ShoppingCart shoppingCart = null;
  if(user != null) {
    Catalog catalog = restTemplate.getForObject("http://catalog-service/v1/catalog", Catalog.class);
    shoppingCart = aggregateCartEvents(user, catalog);
  }
  return shoppingCart;
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

/**
 * 验证账户号是否有效
 * @param accountNumber
 * @return 一个布尔值表示账户号是否有效
 * @throws Exception 账户号无效时抛出异常
 */
public boolean validateAccountNumber(String accountNumber) throws Exception {
  Account[] accounts = oAuth2RestTemplate.getForObject("http://account-service/v1/accounts", Account[].class);
  // 确保账户号被当前验证用户拥有
  if(accounts != null && !Arrays.stream(accounts).anyMatch(account -> Objects.equals(account.getAccountNumber(), accountNumber))) {
    log.error("账户号无效:" + accountNumber);
    throw new Exception("账户号无效:" + accountNumber);
  }
  return true;
}

代码示例来源:origin: PacktPublishing/OAuth-2.0-Cookbook

private void tryToGetUserProfile(ModelAndView mv) {
  accessTokenRequest.add("username", "adolfo");
  accessTokenRequest.add("password", "123");
  String endpoint = "http://localhost:8080/api/profile";
  try {
    UserProfile userProfile = restTemplate.getForObject(endpoint, UserProfile.class);
    mv.addObject("profile", userProfile);
  } catch (HttpClientErrorException e) {
    throw new RuntimeException("it was not possible to retrieve user profile");
  }
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

public Order createOrder(List<OrderItem> orderItems) {
  Account[] accounts = oAuth2RestTemplate.getForObject("http://account-service/v1/accounts", Account[].class);
  Account defaultAccount = Arrays.stream(accounts)
      .filter(Account::isDefaultAccount)
      .findFirst().orElse(null);
  if(defaultAccount == null) {
    return null;
  }
  Order newOrder = new Order(defaultAccount.getAccountNumber(),
      defaultAccount.getAddresses().stream()
          .filter(address -> address.getAddressType() == AddressType.SHIPPING)
          .findFirst()
          .orElseThrow(() -> new RuntimeException("默认账户没有收货地址")));
  newOrder.setOrderItems(orderItems);
  newOrder = orderRepositroy.save(newOrder);
  return newOrder;
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

public List<Account> getUserAccount() {
    List<Account> accounts = null;
    User user = oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class);
    if(user != null) {
      accounts = accountRepository.findAccountsByUserId(user.getUsername()); // 用户名作为userId是合理的,因为登录时使用的用户名是唯一的
    }

    // 掩盖信用卡除最后四位以外的数字
    if(accounts != null) {
      accounts.forEach(account -> account.getCreditCards()
          .forEach(creditCard -> creditCard.setNumber(creditCard.getNumber().replaceAll("[\\d]{4}(?!$)", "****-"))));
    }
    return accounts;
  }
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

public List<Account> getUserAccount() {
    List<Account> accounts = null;
    User user = oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class);
    if(user != null) {
      accounts = accountRepository.findAccountsByUserId(user.getUsername()); // 用户名作为userId是合理的,因为登录时使用的用户名是唯一的
    }

    // 掩盖信用卡除最后四位以外的数字
    if(accounts != null) {
      accounts.forEach(account -> account.getCreditCards()
          .forEach(creditCard -> creditCard.setNumber(creditCard.getNumber().replaceAll("[\\d]{4}(?!$)", "****-"))));
    }
    return accounts;
  }
}

代码示例来源:origin: com.bosch.bis.monitoring/bis-event-publisher-impl

@Bean
@Autowired
public RuntimeDataServiceAddress getRuntimeDataServiceAddress(IntegrationsApiConfig integrationsApiConfig) {
  List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
  messageConverters.add(new GsonHttpMessageConverter());
  OAuth2RestTemplate oauth2RestTemplate = monitoringServicesConnection.getOauth2RestTemplate();
  oauth2RestTemplate.setMessageConverters(messageConverters);
  URI urlEndpoint = getWebsocketUrlEndpoint(integrationsApiConfig);
  String wsAddress;
  try {
    LOG.info("Use Racoon endpoint to retrieve Websocket address: " + urlEndpoint);
    wsAddress = oauth2RestTemplate.getForObject(urlEndpoint, String.class);
  } catch (HttpStatusCodeException e) {
    String errorMessage = "Failed to retrieve Websocket endpoint address via API Gateway using URL \'"
        + urlEndpoint + "' :" + e.getMessage() + ". Http response: " + e.getResponseBodyAsString();
    LOG.error(errorMessage);
    throw new BisMonitoringFatalException(errorMessage, e);
  }
  LOG.info("Retrieved Websocket address from Racoon: " + wsAddress);
  return new RuntimeDataServiceAddress(wsAddress);
}

代码示例来源:origin: kbastani/cloud-native-microservice-strangler-example

public Profile updateProfile(Profile profile) throws IOException {

    Assert.notNull(profile);

    // Get current authenticated user
    User user = oAuth2RestTemplate.getForObject("http://user-service/uaa/v1/me", User.class);

    // Get current profile
    Profile currentProfile = getProfile(user.getUsername());

    if (currentProfile != null) {
      if (currentProfile.getUsername().equals(profile.getUsername())) {
        // Save the profile
        profile.setId(currentProfile.getId());
        profile.setCreatedAt(currentProfile.getCreatedAt());
        profile = profileRepository.save(profile);

        // Replicate the write to the legacy customer service
        amqpTemplate.convertAndSend("customer.update",
            new ObjectMapper().writeValueAsString(profile));
      }
    }

    return profile;
  }
}

代码示例来源:origin: chaokunyang/microservices-event-sourcing

Inventory[] inventory = oAuth2RestTemplate.getForObject(String.format("http://inventory-service/v1/inventory?productIds=%s", currentCart.getCartItems()
    .stream()
    .map(CartItem::getProductId)
      User user = oAuth2RestTemplate.getForObject("http://user-service/auth/v1/me", User.class);

相关文章