2016-04-13 12 views
3

ユーザー入力を暗号化してデータベースに格納したいと考えています。 I正しくすべてのユーザー入力をStringとして扱われる支柱2型変換を使用して、そして、次作品としての変換午前:文字列から文字列への変換Struts 2での変換は機能しませんか?

  • StringIntegerから
  • StringLong
  • Stringbyte[]

しかし、ターゲットタイプに変換しようとすると:String、それは動作せず、convertFromString()メソッド呼び出されません。

@Override 
public Object convertFromString(Map context, String[] value, Class arg2) { 
    String val = value[0];  

    try { 
     return ASEEncDecUtil.encrypt(val.getBytes("UTF-8")); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     return null; 
    } 

} 

私は何が間違っているのか分かりません。

ユーザー入力を暗号化するために使用するのがベストプラクティスですか?

+0

* *ユーザー入力*とは、*パスワード*または*コンテンツ*(例:テキスト)を意味しますか? –

+0

これは、暗号化したい機密情報を持つテキストコンテンツです。 – dpilwal

答えて

2

コンバータの仕事は、さまざまなフォーマット間の変換を実行することです。

ではありません。オブジェクトをフォーマットで取得し、ビジネスを実行してから同じフォーマットで返す正しいツールです。

これは、Struts2インターセプタやJava EEデコレータのような直交したメソッドや、ActionメソッドやMutators/Accessorのようないくつかのメカニズムを使用することができます。あなたがそれらを使用する必要がある時間/場所。

最も簡単な方法は、(私はKISSのパラダイムのファンなんだ)アクセサ/ミューテータの方法です:

public class KeepItSimpleStupidAction extends ActionSupport { 

    @Inject Logger LOG; 

    private String text; // text is always encrypted inside the action 

    public String getText() { // but it can also be read decrypted by calling the getter 
     return ASEEncDecUtil.decrypt(text.getBytes("UTF-8")); 
    } 

    public void setText(String text) { // the setter automatically encrypts it 
     this.text = ASEEncDecUtil.encrypt(text.getBytes("UTF-8")); 
    } 

    public String execute() { 
     LOG.debug("text decrypted: " + getText()); 
     LOG.debug("text encrypted: " + text); 
     return SUCCESS; 
    } 

} 
4

あなたはおそらく、あなたのカスタムコンバータでStrutsTypeConverterクラスを拡張しています。その中でconvertFromStringconvertToString方法は、そのようなものになりますconvertValueメソッドから呼び出されます。

public Object convertValue(Map context, Object o, Class toClass) { 
    if (toClass.equals(String.class)) { 
     return convertToString(context, o); 
    } else if (o instanceof String[]) { 
     return convertFromString(context, (String[]) o, toClass); 
    } else if (o instanceof String) { 
     return convertFromString(context, new String[]{(String) o}, toClass); 
    } else { 
     return performFallbackConversion(context, o, toClass); 
    } 
} 

のでtoClassStringクラスであるならば、convertFromStringが呼び出されることはありません。

代わりにcom.opensymphony.xwork2.conversion.impl.DefaultTypeConverterを拡張し、public Object convertValue(Map context, Object o, Class toClass)メソッドを上書きします。

+2

convertFromString()メソッドが呼び出されない理由を説明してくれてありがとう。 – dpilwal

関連する問題