21

Rails4 默认使用加密的 cookie 会话存储。当应用程序尝试加密 cookie 时,会引发以下错误:OpenSSL::Cipher::CipherError: Illegal key size: possibly you need to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your JRE(stacktrace:https ://gist.github.com/8ba56b18060ae30e4d44 )。

正如这里提到的,这可以通过降级密码学或安装 JCE 来解决——第一个是我真的不想做的事情,而后者在 heroku 上是不可能的(afaik)。

4

3 回答 3

18

不确定它是否适用于 Heroku,但这解决了我本地 Jruby 上的问题。

创建 config/initializers/unlimited_strength_cryptography.rb:

if RUBY_PLATFORM == 'java' # Allows the application to work with other Rubies if not JRuby
  require 'java'
  java_import 'java.lang.ClassNotFoundException'

  begin
    security_class = java.lang.Class.for_name('javax.crypto.JceSecurity')
    restricted_field = security_class.get_declared_field('isRestricted')
    restricted_field.accessible = true
    restricted_field.set nil, false
  rescue ClassNotFoundException => e
    # Handle Mac Java, etc not having this configuration setting
    $stderr.print "Java told me: #{e}n"
  end
end
于 2013-11-15T05:01:09.747 回答
3

Heroku 开发中心现在有这篇文章:“自定义 JDK”

在某些情况下,需要将文件与 JDK 捆绑在一起,以便在运行时 JVM 中公开功能。例如,为了利用更强大的密码库,通常会将无限强度的 Java 加密扩展 (JCE) 添加到 JDK。为了处理这种情况,Heroku 会将应用程序指定的 .jdk-overlay 文件夹中的文件复制到 JDK 的目录结构中。

以下是将 JCE 文件添加到您的应用程序的方法:

  1. 在应用程序的根目录中,创建一个.jdk-overlay文件夹

  2. 复制 JCElocal_policy.jarUS_export_policy.jar进入.jdk-overlay/jre/lib/security/

  3. 提交文件

    $ git add .jdk-overlay
    $ git commit -m "自定义 JCE 文件"

  4. 部署到 Heroku

    $ git push heroku 大师

于 2013-08-09T14:17:11.873 回答
1

使用Leons的方法,这解决了我在生产中的问题,但在没有救援的情况下破坏了开发。

# config/initializers/unrestricted_crypto.rb
begin # Enable 'restricted' cipher libraries on crippled systems
  prop = Java::JavaxCrypto::JceSecurity.get_declared_field 'isRestricted'
  prop.accessible = true
  prop.set nil, false
rescue NameError
end

这是因为不同的javas有不同的flavas......我会让自己出去。

于 2013-11-27T14:26:55.410 回答