java - 更新 API Manager 2.0 和 IDS 5.2 的自定义声明 jar 文件

标签 java wso2 wso2-api-manager wso2-identity-server

我正在尝试更新我们的自定义声明 java 文件中的导入。到目前为止,我发现没有什么太大改变,只是确实发生了一些变化。 org.wso2.carbon.apimgt.impl.token.URLSafeJWTGenerator 更改为 org.wso2.carbon.apimgt.keymgt.token.URLSafeJWTGenerator。当我将此更改添加到文件时,它表示 populateCustomClaims 方法不再有效。

JAVA代码

import edu.wso2.is.helper.DomainEntity;
import edu.wso2.is.helper.DomainEntityHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO;
import org.wso2.carbon.apimgt.impl.token.URLSafeJWTGenerator;
import org.wso2.carbon.apimgt.api.*;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.apache.commons.codec.binary.Base64;
import java.util.HashMap;
import java.util.Map;


public class CustomTokenGenerator extends URLSafeJWTGenerator {

private static final Log log = LogFactory.getLog(CustomTokenGenerator.class);

static String DOMAIN_DIALECT = "http://domain.edu/claims";
private final DOMAINEntityHelper DOMAINEntityHelper = new DOMAINEntityHelper();

public CustomTokenGenerator() {
}



//there is no access to the api call headers, etc. only what was passed in the DTO

public Map<String, String> populateCustomClaims(APIKeyValidationInfoDTO keyValidationInfoDTO, String apiContext, String version, String accessToken)
        throws APIManagementException {
    if (log.isDebugEnabled())
        log.debug("populateCustomClaims starting");
    Map<String, String> map = new HashMap<>();//map for custom claims
    Map<String, String> claims = super.populateCustomClaims(keyValidationInfoDTO,apiContext,version,accessToken);

    boolean isApplicationToken =
            keyValidationInfoDTO.getUserType().equalsIgnoreCase(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION) ? true : false;
    if (isApplicationToken) {
        if (log.isDebugEnabled())
            log.debug("Application Token detected - no resource owner claims will be added");
    }
    else {
        String netid = extractNetId(keyValidationInfoDTO.getEndUserName());

        if (log.isDebugEnabled())
            log.debug("adding resource owner claims to map - netid " + netid);

        map = addResourceOwnerClaims(netid, map);
    }

    String consumerKey = keyValidationInfoDTO.getConsumerKey();
    String dialect = getDialectURI();
    String subscriberNetId = extractNetId(keyValidationInfoDTO.getSubscriber());

    if (log.isDebugEnabled())
        log.debug("adding client claims to map - subscriberNetId " + subscriberNetId + " client_id " + consumerKey);

    map.put(dialect + "/client_id",consumerKey);

    map = addClientClaims(consumerKey, subscriberNetId, map);

    if (log.isDebugEnabled())
        log.debug("populateCustomClaims ending");

    return map;
}

private Map<String, String> addClientClaims(String consumerKey, String subscriberNetId, Map<String, String> map) {

    if (log.isDebugEnabled())
        log.debug("addClientClaims starting");

    if (consumerKey == null) {
        return map;
    }
    boolean isConsumerClaims = true;
    DOMAINEntity identifiers = DOMAINEntityHelper.getDOMAINEntityFromConsumerKey(consumerKey);
    if (identifiers == null) {
        if (log.isDebugEnabled())
            log.debug("No claims found for consumerKey, using subscriberNetId");
        isConsumerClaims = false;
        identifiers = DOMAINEntityHelper.getDOMAINEntityFromNetId(subscriberNetId);
        if (identifiers == null)
            return map;
    }
    if (isConsumerClaims)
        map.put(DOMAIN_DIALECT + "/client_claim_source", "CLIENT_ID");
    else
        map.put(DOMAIN_DIALECT + "/client_claim_source", "CLIENT_SUBSCRIBER");

    map.put(DOMAIN_DIALECT + "/client_subscriber_net_id", subscriberNetId);
    map.put(DOMAIN_DIALECT + "/client_person_id", identifiers.getPersonId());
    map.put(DOMAIN_DIALECT + "/client_net_id", identifiers.getNetId());
    map.put(DOMAIN_DIALECT + "/client_surname", identifiers.getSurname());

    if (log.isDebugEnabled())
        log.debug("addClientClaims ending");
    return map;
}
/* adds resource owner credentials to the map */
private Map<String, String> addResourceOwnerClaims(String netid, Map<String, String> map) {

    if (log.isDebugEnabled())
        log.debug("addResourceOwnerClaims starting");

    if (netid == null) {
        return map;
    }
    DOMAINEntity identifiers = DOMAINEntityHelper.getDOMAINEntityFromNetId(netid);
    if (identifiers == null) {
        return map;
    }
    map.put(DOMAIN_DIALECT + "/resourceowner_person_id", identifiers.getPersonId());
    map.put(DOMAIN_DIALECT + "/resourceowner_domain_id", identifiers.getDomainId());
    map.put(DOMAIN_DIALECT + "/resourceowner_surname", identifiers.getSurname());
    map.put(DOMAIN_DIALECT + "/resourceowner_rest_of_name", identifiers.getRestOfName());
    map.put(DOMAIN_DIALECT + "/resourceowner_surname_position", identifiers.getSurnamePosition());

    if (log.isDebugEnabled())
        log.debug("addResourceOwnerClaims ending");
    return map;
}

private String extractNetId(String carbonIdentifier) {
    if (log.isDebugEnabled()) {
        log.debug("extractNetId starting");
        log.debug("step 1: carbonIdentifier is " + carbonIdentifier);
    }
    String netid = UserCoreUtil.removeDomainFromName(carbonIdentifier);
    if (log.isDebugEnabled())
        log.debug("step 2: after remove domain netid is " + netid);
    if (netid != null) {
        if (netid.endsWith("@carbon.super")) {
            netid = netid.replace("@carbon.super", "");
        }
    }
    if (log.isDebugEnabled())
        log.debug("extractNetId ending with result " + netid);
    return netid;
  }
}

我还更新了 pom.xml 依赖项

XML 代码

<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>edu.wso2.is</groupId>
<artifactId>edu.wso2.is.CustomClaimsGenerator</artifactId>
<version>1.3.0</version>

<packaging>jar</packaging>
<name>Custom Claims Generator</name>
<repositories>
    <repository>
         <releases>
           <enabled>true</enabled>
           <updatePolicy>daily</updatePolicy>
           <checksumPolicy>ignore</checksumPolicy>
         </releases>
         <id>wso2-nexus</id>
         <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
       </repository>

</repositories>
<dependencies>
  <dependency>
      <groupId>org.wso2.carbon.identity</groupId>
      <artifactId>org.wso2.carbon.identity.core</artifactId>
      <version>5.2.2</version>
  </dependency>
  <dependency>
      <groupId>org.wso2.carbon.identity</groupId>
      <artifactId>org.wso2.carbon.identity.application.common</artifactId>
      <version>5.2.2</version>
  </dependency>
  <dependency>
      <groupId>commons-codec.wso2</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4.0.wso2v1</version>
  </dependency>
  <dependency>
      <groupId>org.wso2.carbon.apimgt</groupId>
      <artifactId>org.wso2.carbon.apimgt.keymgt</artifactId>
      <version>6.0.4</version>
  </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

任何帮助或指向某个方向的指示将不胜感激。 谢谢你!

最佳答案

populateCustomClaims() 签名在 APIM 2.0.0 中进行了如下更改。现在需要 TokenValidationContext object .

public Map<String, String> populateCustomClaims(TokenValidationContext validationContext)  
    throws APIManagementException {

代码是here .

关于java - 更新 API Manager 2.0 和 IDS 5.2 的自定义声明 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475061/

相关文章:

wso2 - 如何使用 Log Mediator 在 WSO2 中选择服务名称

WSO2 身份服务器电子邮件作为用户名

CORS 不适用于 WSO2 API 管理器中的 oAuth2 UserInfo 端点

http - WSO2 APIM 系统无法从/devtest/1.0/users/admin中推断传输信息

java - 使用并行 Arraylists 从数组创建直方图

java - 如何在不命名类/类型的情况下动态解析方法 getter 及其值

wso2 - 如何从源代码构建 WSO2 4.X?

java - 如何将字符串排序到映射中并打印结果

java - Hibernate @Filter 枚举列类型

具有非 WSO2 ESB 的 WSO2 API 管理器?