如何在WSo2 API管理器中调用/配置安全的API

时间:2019-07-15 11:01:11

标签: wso2 wso2-am

我需要配置一组api,这些api是受保护的,并且仅将客户端凭据和密码与客户端密钥和秘密密钥一起使用。 以及如何跟踪尝试访问API的用户。

我尝试添加代理介体。

3 个答案:

答案 0 :(得分:0)

默认情况下,您将在WSO2 API管理器中创建的API具有安全功能。 您可以使用以下方法保护API 应用程序内置的oauth或 您可以集成WSO2身份服务器以用作WSO2 APIM的身份提供程序。 以下链接将显示如何在APIM Manager中创建和发布API。 [https://docs.wso2.com/display/AM250/Create+and+Publish+an+API]

一旦您能够调用创建的API,就可以通过在Analytics(分析)配置文件中进行检查来检查该API的使用情况。您可以将其与API Manager集成。 以下链接对于设置APIM 2.5.0的Analytics(分析)配置文件很有用 [https://docs.wso2.com/display/AM250/Configuring+APIM+Analytics/]

一旦为APIM设置了Google Analytics(分析),您就可以使用数据库来获取API的使用情况。

答案 1 :(得分:0)

要解决您的查询,这就是您应该继续的方法。

您可以在API流入中添加自定义序列(只能通过对其进行编辑才能从API Publisher中进行添加)[https://docs.wso2.com/display/AM260/Adding+Mediation+Extensions]

顺序代码如下:

import React, { useState } from "react";
import PropTypes from "prop-types";
import deburr from "lodash/deburr";
import Downshift from "downshift";
import { makeStyles } from "@material-ui/core/styles";
import TextField from "../MyTextField/MyTextField";
import { Paper, MenuItem, Chip } from "@material-ui/core";

import invariant from "invariant";
import { isPlainObject } from "lodash";

function getStringLabelKey(labelKey) {
  return typeof labelKey === "string" ? labelKey : "label";
}

function getOptionLabel(option, labelKey) {
  //console.log("getOptionLabel", option, labelKey);

  if (option.paginationOption || option.customOption) {
    return option[getStringLabelKey(labelKey)];
  }

  let optionLabel;

  if (typeof option === "string") {
    optionLabel = option;
  }

  if (typeof labelKey === "function") {
    // This overwrites string options, but we assume the consumer wants to do
    // something custom if `labelKey` is a function.
    optionLabel = labelKey(option);
  } else if (typeof labelKey === "string" && isPlainObject(option)) {
    optionLabel = option[labelKey];
  }

  invariant(
    typeof optionLabel === "string",
    "One or more options does not have a valid label string. Check the " +
      "`labelKey` prop to ensure that it matches the correct option key and " +
      "provides a string for filtering and display."
  );

  return optionLabel;
}

function renderInput(inputProps) {
  const { InputProps, classes, ref, ...other } = inputProps;
  return (
    <span className={classes.noDisplay}>
      <TextField
        InputProps={{
          inputRef: ref,
          classes: {
            root: classes.inputRoot,
            input: classes.inputInput
          },
          ...InputProps
        }}
        {...other}
      />
    </span>
  );
}

renderInput.propTypes = {
  /**
   * Override or extend the styles applied to the component.
   */
  classes: PropTypes.object.isRequired,
  InputProps: PropTypes.object
};

function renderSuggestion(suggestionProps) {
  const {
    suggestion,
    itemProps,
    //selectedItem,
    renderMenuItemChildren
  } = suggestionProps;
  return (
    <MenuItem
      {...itemProps}
      key={suggestion.email}
      component="div"
      style={{
        fontWeight: 400
      }}
    >
      {getOptionLabel(suggestion, renderMenuItemChildren)}
    </MenuItem>
  );
}

IntegrationDownshift.defaultProps = {
  labelKey: "label",
  renderMenuItemChildren: "label",
  placeholder: "Search...",
  label: "Search",
  noFound: "No matches found.",
  fullWidth: false
};

renderSuggestion.propTypes = {
  highlightedIndex: PropTypes.oneOfType([
    PropTypes.oneOf([null]),
    PropTypes.number
  ]).isRequired,
  index: PropTypes.number.isRequired,
  itemProps: PropTypes.object.isRequired,
  selectedItem: PropTypes.string.isRequired,
  suggestion: PropTypes.shape({
    label: PropTypes.string.isRequired
  }).isRequired,
  fullWidth: PropTypes.bool
};

function getSuggestions(value, { options, showEmpty = false } = {}) {
  const inputValue = deburr(value.trim()).toLowerCase();
  const inputLength = inputValue.length;
  let el = inputLength === 0 && !showEmpty ? [] : options;
  return el;
}

const useStyles = makeStyles(theme => ({
  root: {
    flexGrow: 1,
    height: 50
  },
  container: {
    flexGrow: 1,
    position: "relative"
  },
  paper: {
    position: "absolute",
    zIndex: 1,
    marginTop: theme.spacing(1),
    left: 0,
    right: 0
  },
  chip: {
    margin: theme.spacing(0.5, 0.25)
  },
  inputRoot: {
    flexWrap: "wrap"
  },
  inputInput: {
    width: "auto",
    flexGrow: 1
  },
  divider: {
    height: theme.spacing(2)
  },
  noDisplay: {
    "& .MuiInputBase-inputAdornedEnd": {
      display: "none !important"
    },
    "& .MuiChip-root": {
      //width: "100%",
      justifyContent: "flex-start !important"
    },
    "& .MuiChip-deleteIcon": {
      position: "absolute",
      right: "5px"
    }
  },
  myClass: {
    width: "100%",
    backgroundColor: "#ffccaa"
  }
}));

export default function IntegrationDownshift({
  options,
  onSearch,
  onChange,
  selectedItem,
  labelKey,
  renderMenuItemChildren,
  placeholder,
  label,
  noResult,
  noFound,
  fullWidth,
  backgroundColorChip,
  colorTextChip
}) {
  const [search, setSearch] = useState("");

  const classes = useStyles();
  function handleInputChange(event) {
    //console.log("handleInputChangeINT", event.target.value);
    onSearch(event.target.value);
  }

  function handleChange(event) {
    //console.log("handleChangeINT", event);
    onChange(event);
  }

  function invCheck() {
    console.log("label", options[0] && options[0].label);

    invariant(
      typeof options[0].label === "string",
      "One or more options does not have a valid label string. Check the " +
        "`labelKey` prop to ensure that it matches the correct option key and " +
        "provides a string for filtering and display."
    );
  }

  invCheck();

  return (
    <div className={classes.root}>
      <Downshift
        id="downshift-options"
        onChange={handleChange}
        selectedItem={selectedItem}
        //itemToString={item => (item ? item.value : search)}
        itemToString={item => search}
      >
        {({
          clearSelection,
          getInputProps,
          getItemProps,
          getLabelProps,
          getMenuProps,
          highlightedIndex,
          inputValue,
          isOpen,
          openMenu,
          selectedItem
        }) => {
          const { onBlur, onChange, onFocus, ...inputProps } = getInputProps({
            onChange: event => {
              //console.log("onChangeDINT", event.target.value);
              setSearch(event.target.value);
              if (event.target.value === "") {
                clearSelection();
              }
            },
            onFocus: openMenu,
            placeholder
          });

          return (
            <div className={classes.container}>
              {renderInput({
                fullWidth: true,
                classes,
                label,
                InputLabelProps: getLabelProps({ shrink: true }),
                InputProps: {
                  endAdornment: selectedItem !== null && (
                    <Chip
                      key={1}
                      tabIndex={-1}
                      label={getOptionLabel(selectedItem, labelKey)}
                      className={classes.chip}
                      onDelete={() => clearSelection()}
                      style={{
                        width: fullWidth && "100%",
                        backgroundColor: backgroundColorChip,
                        color: colorTextChip
                      }}
                    />
                  ),
                  onBlur,
                  onChange: event => {
                    handleInputChange(event);
                    onChange(event);
                  },
                  onFocus
                },
                inputProps
              })}

              <div {...getMenuProps()}>
                {isOpen ? (
                  <Paper className={classes.paper} square>
                    {getSuggestions(inputValue, {
                      options,
                      showEmpty: false
                    }).map((suggestion, index) =>
                      renderSuggestion({
                        suggestion,
                        index,
                        renderMenuItemChildren,
                        itemProps: getItemProps({
                          item: suggestion
                        }),
                        highlightedIndex,
                        selectedItem
                      })
                    )}
                    {inputValue.length > 0 &&
                      getSuggestions(inputValue, { options, showEmpty: true })
                        .length === 0 && (
                        <MenuItem
                          key={"noFound"}
                          component="div"
                          style={{
                            fontWeight: 400
                          }}
                          onClick={() => {
                            if (noResult !== undefined) noResult(true);
                          }}
                        >
                          {noFound}
                        </MenuItem>
                      )}
                  </Paper>
                ) : null}
              </div>
            </div>
          );
        }}
      </Downshift>
    </div>
  );
}

以上是用于静态令牌的后端令牌。对于动态令牌,您可以相应地创建一个序列。

这应该可以解决您的查询:)

要获取动态信息,请转到[How the external authentication handled in WSO2 APIM for an external/Third Party APIs

答案 2 :(得分:0)

如果您的后端受oauth保护,则可以使用以下方法之一

  1. 方法1

OAuth中介程序,可用于生成OAuth 2.0令牌和调用由OAuth 2.0协议保护的后端。请参阅this

  1. 方法2

编写中介序列,该序列为后端生成访问令牌并建立与OAuth 2.0安全后端的连接。请访问this博客。