gpt4 book ai didi

java - Eclipse RCP 应用程序 - 创建首选项页面条目,该条目已加密保存

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:03 25 4
gpt4 key购买 nike

我发现可以为我的 Eclipse 3.7 RCP 应用程序创建首选项页面。
描述了如何做到这一点 here .

问题:
是否可以在首选页面中添加一个输入字段,该字段将被保存在安全存储中加密

到目前为止,我只找到了一种以编程方式使用安全存储的方法。
这不是我的解决方案,因为首选项页面条目是自动保存的。

解决方案:我做到了,Tonny Madsen 对他说:

  • 扩展了 SecureIPreferenceStore 类,这是一个 IPreferenceStore
  • 使用 ISecurePreferences 类型的私有(private)变量使新类将所有首选项存储在安全存储中
  • 将新类设置为首选项页面的 PreferenceStore

ScopedPreferenceStore 的扩展


public class SecureIPreferenceStore extends ScopedPreferenceStore {<p></p>

<p>/* Current class will override all storage and loading methods, using this secure preferenceStore, to store all preferences in an encrypted way. */
private static final ISecurePreferences SECURE_PREFERENCES = SecurePreferencesFactory.getDefault();</p>

<p>/**
* Use this class to retrieve an instance of {@code SecureIPreferenceStore}, which will store all preferences in the default secure storage.
*
* @param context - the scope to store to, e.g. {@link org.eclipse.core.runtime.preferences.ConfigurationScope ConfigurationScope}
* or {@link org.eclipse.core.runtime.preferences.InstanceScope InstanceScope}
*
* @param qualifier - the qualifier to look up the preference node. An example of a qualifier would be the plug-in identifier that the preference is associated with,
* (e.g. the "org.eclipse.core.resources" uses the preference node in the instance scope where the preferences for "org.eclipse.core.resources" are stored)
*/
public SecureIPreferenceStore(IScopeContext context, String qualifier){
super(context, qualifier);
}</p>

<p>@Override
public void setValue(String name, boolean value) {
try {
SECURE_PREFERENCES.putBoolean(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public void setValue(String name, double value) {
try {
SECURE_PREFERENCES.putDouble(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public void setValue(String name, float value) {
try {
SECURE_PREFERENCES.putFloat(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public void setValue(String name, int value) {
try {
SECURE_PREFERENCES.putInt(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public void setValue(String name, long value) {
try {
SECURE_PREFERENCES.putLong(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public void setValue(String name, String value) {
try {
SECURE_PREFERENCES.put(name, value, true);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</p>

<p>@Override
public boolean getBoolean(String name) {
Boolean result = false;
try {
result = SECURE_PREFERENCES.getBoolean(name, result);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}</p>

<p>@Override
public double getDouble(String name) {
Double result = 0.0;
try {
result = SECURE_PREFERENCES.getDouble(name, result);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}</p>

<p>@Override
public float getFloat(String name) {
Float result = 0.0f;
try {
result = SECURE_PREFERENCES.getFloat(name, result);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}</p>

<p>@Override
public int getInt(String name) {
int result = 0;
try {
result = SECURE_PREFERENCES.getInt(name, result);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}</p>

<p>@Override
public long getLong(String name) {
Long result = 0L;
try {
result = SECURE_PREFERENCES.getLong(name, result);
} catch (StorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
</p>

FieldEditorPreferencePage 中新类的用法:


@Override
public void init(IWorkbench workbench) {
setPreferenceStore(
new SecureIPreferenceStore(ConfigurationScope.INSTANCE,
"de.vogella.rcp.preferences.secure") );
setDescription("A demonstration of a preference page implementation");
}

最佳答案

我没试过,但是...

我会添加自己的 IPreferenceStore 用于安全存储,然后在首选项页面中使用常用的 FieldEditors

执行此操作的最简单方法是子类化 ScopedPreferenceStore 覆盖 getStorePreferences() 以返回新的 IEclipsePreferences。这可能是 EclipsePreferences 的子类,它覆盖了各种 internal...(...) 方法以连接到 ISecurePreferences。鉴于内部方法与 ISecurePreferences 的方法非常匹配,这可能不会非常困难:-)

关于java - Eclipse RCP 应用程序 - 创建首选项页面条目,该条目已加密保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com