如何在Rails form_tag提交操作被触发之前将MD5应用于密码?

时间:2022-11-24 10:19:14

I am a Rails newbie.

我是一个Rails新手。

I am using has_secure_password in the user controller. I realize it applies blowfish to store the password securely, but I want to have the password encrypted before it leaves the browser client. If this means the password is encrypted twice - that's fine by me.

我在用户控制器中使用has_secure_password。我意识到它应用blowfish来安全地存储密码,但是我希望在它离开浏览器客户端之前加密密码。如果这意味着密码被加密了两次 - 那对我来说没问题。

So, in a simple login form like the one below, how to I call a Javascript function I have already to apply MD5 to the password, before the form is submitted? Sure - I could just send 'standard' HTML and a onSubmit() javascript call, but surely there is a way to do this the 'Rails Way'. How?

那么,在如下所示的简单登录表单中,如何调用Javascript函数我已经在提交表单之前将MD5应用于密码?当然 - 我可以发送'标准'HTML和onSubmit()javascript调用,但肯定有一种方法可以实现'Rails Way'。怎么样?

Thanks in advance!

提前致谢!

<h1>Log In</h1>

<%= form_tag sessions_path do %>
  <div class="field">
    <%= label_tag :email %><br />
    <%= text_field_tag :email, params[:email] %>
  </div>
  <div class="field">
    <%= label_tag :password %><br />
    <%= password_field_tag :password %>
  </div>
  <div class="actions"><%= submit_tag "Log In" %></div>
<% end %>

UPDATE:

It seems like there is no 'Rails Way' to do this, so following the suggestion from Samar, I simply added a listener to the form click event using jQuery, after explicitly giving the form an id. Below is the complete code segment with changes and typo corrections included. Note that I am using my own MD5 library.

看起来没有'Rails Way'可以做到这一点,所以按照Samar的建议,我只是在明确给出表单id之后,使用jQuery添加了一个表单click事件的监听器。以下是包含更改和拼写错误更正的完整代码段。请注意,我使用自己的MD5库。

<h1>Log In</h1>

<%= form_tag sessions_path, :id => 'user_login_form' do %>
    <div class="field">
        <%= label_tag :email %><br />
        <%= text_field_tag :email, params[:email] %>
     </div>
    <div class="field">
        <%= label_tag :password %><br />
        <%= password_field_tag :password %>
    </div>
    <div class="actions"><%= submit_tag "Log In" %></div>
<% end %>

<script type="text/javascript">
    $("#user_login_form").click(function(){
        $("#password").val(EncryptMD5($("#password").val()));
    });
</script>

1 个解决方案

#1


-1  

Download cryptojs from http://code.google.com/p/crypto-js/ . If your password field has id "password" and submit button has id "form_submit". Then do the following:

从http://code.google.com/p/crypto-js/下载cryptojs。如果您的密码字段的ID为“password”,则提交按钮的ID为“form_submit”。然后执行以下操作:

<script type="text/javascript" src="crypto.js">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js">
    <script>
    $("#form_submit").click(function){
    $("#password").val(CryptoJS.MD5($("#password").val()));
    });
    </script>

#1


-1  

Download cryptojs from http://code.google.com/p/crypto-js/ . If your password field has id "password" and submit button has id "form_submit". Then do the following:

从http://code.google.com/p/crypto-js/下载cryptojs。如果您的密码字段的ID为“password”,则提交按钮的ID为“form_submit”。然后执行以下操作:

<script type="text/javascript" src="crypto.js">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js">
    <script>
    $("#form_submit").click(function){
    $("#password").val(CryptoJS.MD5($("#password").val()));
    });
    </script>