简体   繁体   English

使用授权将带有太阳黑子的rails应用程序连接到tomcat / solr服务器

[英]Connecting a rails application with sunspot to a tomcat/solr server with authorization

Okay, so I have a separate server with solr and this connects perfectly fine with the app. 好的,所以我有一个单独的服务器与solr,这与应用程序完全正常。 Using the usual sunspot.yml we have something like: 使用通常的sunspot.yml我们有类似的东西:

production:
  solr:
    hostname: domain
    port: 8080
    log_level: WARNING
    path: path/to/data

The problem is I wanted to authorization to my tomcat app. 问题是我想授权我的tomcat应用程序。 It seems that as long as you know the domain and port you could just go and visit solr/admin. 看来只要你知道域名和端口就可以去访问solr / admin。 So in my tomcat web.xml I added: 所以在我的tomcat web.xml中我添加了:

<security-constraint>
    <web-resource-collection>
      <web-resource-name> 
        Solr authenticated application
      </web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>GET</http-method>
      <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>role</role-name>
    </auth-constraint>
  </security-constraint>
   <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>REALM</realm-name>
  </login-config>
  <security-role>
    <description>ROLE NAME</description>
    <role-name>role</role-name>
  </security-role>

This would require anyone visiting solr/admin a username and password before being granted access. 这将要求任何人在被授予访问权限之前访问solr / admin用户名和密码。 The problem is how do I tell my rails app about this? 问题是如何告诉我的rails应用程序? After doing this and I get an expected "Unauthorized" response when my rails app tries to access the solr server. 执行此操作后,当我的rails应用程序尝试访问solr服务器时,我得到了预期的“未授权”响应。 I know it would probably go into the sunspot.yml file but what do I have to add? 我知道它可能会进入sunspot.yml文件,但我需要添加什么?

I'm not sure that the Sunspot configuration supports a Basic Auth username and password via the YAML. 我不确定Sunspot配置是否通过YAML支持Basic Auth用户名和密码。 Those options are all concatenated together to be assigned to Sunspot.config.solr.url and later given to RSolr. 这些选项都连接在一起,分配给Sunspot.config.solr.url ,然后提供给RSolr。 That's a simple oversight — a pull request at https://github.com/outoftime/sunspot would be welcome to fix that. 这是一个简单的疏忽 - 欢迎来自https://github.com/outoftime/sunspot的拉取请求来解决这个问题。

You will have better luck either assigning Sunspot.config.solr.url directly in an initializer, or by specifying the URL via an environment variable. 您可以更好地在初始化Sunspot.config.solr.url直接分配Sunspot.config.solr.url ,或者通过环境变量指定URL。 Sunspot supports the SOLR_URL and WEBSOLR_URL environment variables, and in doing so should preserve a Basic Auth username and password. Sunspot支持SOLR_URLWEBSOLR_URL环境变量,这样做应保留Basic Auth用户名和密码。

To test this in your development environment: 要在开发环境中测试它:

rake sunspot:reindex SOLR_URL=http://admin:foobar@localhost:8983/solr
rails server SOLR_URL=http://admin:foobar@localhost:8983/solr

https://github.com/justinko/sunspot-rails-http-basic-auth https://github.com/justinko/sunspot-rails-http-basic-auth

gem install this. 宝石安装这个。 its HTTP Basic Authentication support for sunspot_rails. 它对sunspot_rails的HTTP基本身份验证支持。 You can then put the username and password in the sunspot.yml that is generated. 然后,您可以将用户名和密码放在生成的sunspot.yml中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM