使用invariantParams将基本身份验证参数添加到HttpSolrClient

时间:2016-11-28 11:56:15

标签: solrj

我正在尝试使用SolrJ HttpSolrClient编写SOLR客户端。 我必须使用基本身份验证。

SolrJ HttpSolrClient的文档说:

protected ModifiableSolrParams invariantParams

,描述是:

无论如何都添加到每个请求的参数。这可能是添加诸如身份验证令牌之类的地方。

之前使用过这个的人可以告诉我如何使用invariantParams进行基本身份验证吗?

public class MySolrClient extends HttpSolrClient{

    private String hostUrl;

    private String userName;

    private String password;

    public CloudForgeProcurementSolrClient(final String hostUrl, final String userName, final String password) {
        super(hostUrl);
        this.hostUrl = hostUrl;
        this.userName = userName;
        this.password = password;
        if (invariantParams == null) {
            invariantParams = new ModifiableSolrParams();
        }
        //        invariantParams.add(arg0, arg1);
    }
}

在我的情况下,arg0和arg1应该是什么。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您可以执行类似

的操作
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;

    private String surName;

    @JsonFormat(pattern="yyyy-MM-dd")
    private LocalDate dateOfBirth;

}

@Entity
@NoArgsConstructor
@Getter
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public final class User extends Person {

    @Builder
    public User(Long id, String firstName, String surName, LocalDate dateOfBirth, String documentId, String address) {
        super(id, firstName, surName, dateOfBirth);
        this.documentId = documentId;
        this.address = address;
    }

    private String documentId;

    private String address;

}

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@EqualsAndHashCode
@ToString
public final class UserInternalData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne
    private User user;

    boolean isDeleted;

    @JsonFormat(pattern="yyyy-MM-dd")
    private LocalDate dateStamp;

}

答案 1 :(得分:0)

您可以使用以下代码:

CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new 
UsernamePasswordCredentials(username,password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

HttpSolrClient solrClient = new HttpSolrClient.Builder()
            .withBaseSolrUrl(host)
            .withHttpClient(client).build();