使用Spring Data Solr验证SolrCore(Authenticating SolrCore using Spring Data Solr)
我有一个服务(API),它连接到solr以获取数据并作为响应发送。现在我们在Solr上没有任何类型的安全层,因此我的API工作正常。但事情发生了变化,并且所有核心都添加了有界性层进行认证。 我在API中连接到Core时遇到问题。 以下是我的Java连接类:
@Configuration @EnableConfigurationProperties public class SolrConfigs { private static final Logger LOG = LoggerFactory.getLogger(SolrConfigs.class); @Value("${solr.core.FirstCore}") private String coreFirstCore; @Value("${solr.core.SecondCore}") private String coreSecondCore; @Value("${solr.core.ThirdCore}") private String coreThirdCore; @Bean @ConfigurationProperties(prefix = "solr.client") public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) { LOG.info("Connecting to solr : {}", solrBaseUrl); final HttpSolrClient client = new HttpSolrClient(solrBaseUrl); return client; } @Bean public MulticoreSolrClientFactory multicoreSolrClientFactory(final SolrClient solrClient) { final MulticoreSolrClientFactory factory = new MulticoreSolrClientFactory(solrClient, Arrays.asList(coreFirstCore, coreSecondCore, coreThirdCore)); return factory; } @Bean(name = "solrFirstCoreTemplate") public SolrTemplate solrFirstCoreTemplate(MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreFirstCore); return template; } @Bean(name = "solrSecondCoreTemplate") public SolrTemplate solrSecondCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreSecondCore); return template; } @Bean(name = "solrThirdCoreTemplate") public SolrTemplate solrThirdCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreThirdCore); return template; } }
有没有办法将凭据传递给每个核心? 我尝试过这样的东西
@Bean @ConfigurationProperties(prefix = "solr.client") public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) { LOG.info("Connecting to solr : {}", solrBaseUrl); final HttpSolrClient client = new HttpSolrClient(solrBaseUrl); return client; } @Bean(name = "solrFirstCoreTemplate") public SolrTemplate solrFirstCoreTemplate(SolrClient solrClient) { Credentials credentials = new UsernamePasswordCredentials(username, password); final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreFirstCore, credentials , "BASIC")); template.setSolrCore(coreFirstCore); return template; } @Bean(name = "solrSecondCoreTemplate") public SolrTemplate solrSecondCoreTemplate(SolrClient solrClient) { Credentials credentials = new UsernamePasswordCredentials(username, password); final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreSecondCore, credentials , "BASIC")); template.setSolrCore(coreSecondCore); return template; }
它没有工作。
I have an Service(API) which connects to solr to fetch data and sends as response.Till now we didn't had any type of security layer on Solr so my API was working fine.But things changed and all core's were added with sceurity layer for authentication. I am facing problem while connecting to Core in my API. Following is my Java class for connection :
@Configuration @EnableConfigurationProperties public class SolrConfigs { private static final Logger LOG = LoggerFactory.getLogger(SolrConfigs.class); @Value("${solr.core.FirstCore}") private String coreFirstCore; @Value("${solr.core.SecondCore}") private String coreSecondCore; @Value("${solr.core.ThirdCore}") private String coreThirdCore; @Bean @ConfigurationProperties(prefix = "solr.client") public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) { LOG.info("Connecting to solr : {}", solrBaseUrl); final HttpSolrClient client = new HttpSolrClient(solrBaseUrl); return client; } @Bean public MulticoreSolrClientFactory multicoreSolrClientFactory(final SolrClient solrClient) { final MulticoreSolrClientFactory factory = new MulticoreSolrClientFactory(solrClient, Arrays.asList(coreFirstCore, coreSecondCore, coreThirdCore)); return factory; } @Bean(name = "solrFirstCoreTemplate") public SolrTemplate solrFirstCoreTemplate(MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreFirstCore); return template; } @Bean(name = "solrSecondCoreTemplate") public SolrTemplate solrSecondCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreSecondCore); return template; } @Bean(name = "solrThirdCoreTemplate") public SolrTemplate solrThirdCoreTemplate(final MulticoreSolrClientFactory multicoreSolrClientFactory) { final SolrTemplate template = new SolrTemplate(multicoreSolrClientFactory); template.setSolrCore(coreThirdCore); return template; } }
Is there any way to pass on credentials to each core ? I tried someting like this
@Bean @ConfigurationProperties(prefix = "solr.client") public SolrClient solrClient(@Value("${solr.client.baseURL}") final String solrBaseUrl) { LOG.info("Connecting to solr : {}", solrBaseUrl); final HttpSolrClient client = new HttpSolrClient(solrBaseUrl); return client; } @Bean(name = "solrFirstCoreTemplate") public SolrTemplate solrFirstCoreTemplate(SolrClient solrClient) { Credentials credentials = new UsernamePasswordCredentials(username, password); final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreFirstCore, credentials , "BASIC")); template.setSolrCore(coreFirstCore); return template; } @Bean(name = "solrSecondCoreTemplate") public SolrTemplate solrSecondCoreTemplate(SolrClient solrClient) { Credentials credentials = new UsernamePasswordCredentials(username, password); final SolrTemplate template = new SolrTemplate(new HttpSolrClientFactory(solrClient, coreSecondCore, credentials , "BASIC")); template.setSolrCore(coreSecondCore); return template; }
It didnt work out.
原文:https://stackoverflow.com/questions/49733669
最满意答案
在你的例子中,这是一个编译器错误。
然而,我认为你想要做的是这样的:
int n =23, *p; p = &n; //Change the value of p to 3000, p now points to address 3000 p = reinterpret_cast<int*>(3000); //Check if the address of n has changed std::cout << "Address of n : " << reinterpret_cast<int>(&n) << std::endl;
正如你在运行这段代码时所说的那样。 n的地址不变。
对于你的第二个问题。
是和否:)
如果你定义了两个相邻的变量,它们可能在内存中彼此相邻。
int a,b,c,d; char c = 1; short s = 1; void* p = nullptr; int i = 1; std::cout << "a is at: " << reinterpret_cast<int>(&a) << std::endl; std::cout << "b is at: " << reinterpret_cast<int>(&b) << std::endl; std::cout << "c is at: " << reinterpret_cast<int>(&c) << std::endl; std::cout << "d is at: " << reinterpret_cast<int>(&d) << std::endl; std::cout << "Char is at: " << reinterpret_cast<int>(&c) << std::endl; std::cout << "Short is at: " << reinterpret_cast<int>(&s) << std::endl; std::cout << "Pointer is at: " << reinterpret_cast<int>(p) << std::endl; std::cout << "Int is at: " << reinterpret_cast<int>(&i) << std::endl;
这种行为会导致编译器确定在哪里进行转义。 它可能相邻,也可能不相邻。 如果您想确保它们彼此相邻,请使用数组。
int arr[] = {1,2,3,4,5,6,7}; int * p = &arr[0]; //get address of first element for(int i = 0 ; i < 7; ++i) std::cout << "Value at address: " << reinterpret_cast<int>(p+i) << " is: " << *( p + i) << std::endl;
In your example this is a compiler error.
However what I assume you wanted to do was this:
int n =23, *p; p = &n; //Change the value of p to 3000, p now points to address 3000 p = reinterpret_cast<int*>(3000); //Check if the address of n has changed std::cout << "Address of n : " << reinterpret_cast<int>(&n) << std::endl;
As you can tell when you run this code. The address of n does not change.
For your second question.
Yes and No :)
If you define two variables next to each other they may be next to each other in memory.
int a,b,c,d; char c = 1; short s = 1; void* p = nullptr; int i = 1; std::cout << "a is at: " << reinterpret_cast<int>(&a) << std::endl; std::cout << "b is at: " << reinterpret_cast<int>(&b) << std::endl; std::cout << "c is at: " << reinterpret_cast<int>(&c) << std::endl; std::cout << "d is at: " << reinterpret_cast<int>(&d) << std::endl; std::cout << "Char is at: " << reinterpret_cast<int>(&c) << std::endl; std::cout << "Short is at: " << reinterpret_cast<int>(&s) << std::endl; std::cout << "Pointer is at: " << reinterpret_cast<int>(p) << std::endl; std::cout << "Int is at: " << reinterpret_cast<int>(&i) << std::endl;
This behavior is caused the compiler determining where to stick everthting. It may or may not exist next to each other. If you want to guarantee they exist next to one another, use an array.
int arr[] = {1,2,3,4,5,6,7}; int * p = &arr[0]; //get address of first element for(int i = 0 ; i < 7; ++i) std::cout << "Value at address: " << reinterpret_cast<int>(p+i) << " is: " << *( p + i) << std::endl;
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
无论它是什么, operator new函数都不是任何类的非静态成员函数。 即使您声明并定义了一个特定于类的运算符new,它也被认为是一个静态成员函数; 如果它需要一个size_t参数,那么指向它的指针将为void* (*ptrToNew)( size_t ) 。 (如果它是一个新的位置,当然会有更多的参数。) 当您执行new Foo ,编译器首先在Foo的范围内对operator new进行名称查找(如果Foo是类类型),然后在全局命名空间中。 (有趣的是,编译器不会查看声明Foo的命名空间Foo也不会应 ...
-
为不指向C ++中任何内容的指针赋值(Assigning a value to a pointer that does not point to anything in C++)[2023-09-08]
我正在定义一个指向什么都没有然后设置其值的指针? 它为什么设置了值? 随机内存插槽? 在哪里? 在堆栈还是堆? 好吧,相当。 没有答案。 从字面上看,结果是不确定的。 它可以向我的右眼发送电磁脉冲。 为什么有人创建一个类只是为了写一个做某事的函数? 存在无能。 我真的不明白MyClass这个类,doSomething实际上是否有可能填充某些数据结构? 这是可能的,但我们不能在没有看到它的情况下告诉你。 如果是,那么它是如何以及在哪里它也是一个指向什么的指针? 有些人认为他们可以通过狡猾的指针调用成员函数,只 ... -
为int指针赋值时,为什么我的C程序崩溃了?(Why does my C program crash when assigning a value to an int pointer?)[2023-06-29]
你正在传递指针,但你没有分配内存。 所以他们指向内存中的随机位置。 int computeMoveLocation(int* r, int* c, char* board) { //some code up here *r = 0; //This breaks the program *c = 0; } 坏主要: int main() { int *r; int *c; char *board; // bad, passing in point ... -
赋值给指针(Assigning values to a pointer)[2023-05-07]
在你的例子中,这是一个编译器错误。 然而,我认为你想要做的是这样的: int n =23, *p; p = &n; //Change the value of p to 3000, p now points to address 3000 p = reinterpret_cast(3000); //Check if the address of n has changed std::cout << "Address of n : " << reinterpret_cast (&n) < ... -
uint8_t* buffer, curr, next; 你写它的方式, buffer是一个指针和curr , next只是uint8_t s。 你可能意思是: uint8_t *buffer, *curr, *next; 更好的(不太容易出错)就是让每个领域都有自己的路线。 uint8_t* buffer, curr, next; The way you wrote it, buffer is a pointer and curr and next are mere uint8_t ...
-
*p = 1将最后一个字节(char)设置为000000001 当你将char转换为int类型时, (int)1000的(binary)0000001111101000是(binary)0000001111101000你正在为最后8位i(e) (binary)0000001100000001即769 (binary)0000001100000001分配(int)1 。 使用256512工作,因为您更改的最后8位全部为零,即(int)256512 is (binary)111110101000000000因此 ...
-
尝试*(up)->ux; 这是up指针保存的值,并从该值中选择ux字段 Even I do not understand why it is not working by the standard way, I've found the working solution: void point3D_Compute(struct point3D *uP) { double cx, cy, cz; double *pCx, *pCy; pCx = &cx; pCy = &cy; ...
-
我的回答:你很幸运! 事实是,你碰巧在p中的值是一个有效的内存地址,你可以实际写入,这就是为什么它的工作原理... 尝试在编译器中使用优化选项(-O3),而不是获得不同的结果...... My answers: You're lucky! The fact is, the value you happen to have in p is a valid memory address, that you can actually write to, this is why it works... Try pl ...