【发布时间】:2018-04-17 14:27:05
【问题描述】:
我需要确保类中的方法仅由来自不同包的选定方法执行。
package myApp.security;
public class SecurityStuff{
public static final SecurityStuff security = new SecurityStuff();
public KeyStore getKeyStore(){
//if invoked by MySock.getTLSServerSocket()
return keyStore;
//if not, should return null
}
}
package myApp.socks;
public class MySock{
public void getTLSServerSocket(){
KeyStore keyStore = SecurityStuff.security.getKeyStore();
}
}
对于上述 2 个类,我如何确保 SecurityStuff.getKeyStore() 将返回 KeyStore 如果它来自我允许的类和方法?
请注意 jar 稍后会被混淆。
【问题讨论】:
-
听起来像XY Problem。
-
GhostCat - 混淆会使该解决方案无用。阅读最后一段,谢谢。
-
是的,重新考虑您的设计。这是一个坏主意。多想想objects——你应该考虑谁能接触到objects。如果您阻止将对象分发给无效的“持有者” - 那么您会隐式阻止调用方法!
-
那么请您更明确地说明您已经研究过其他答案!而且我仍然认为给 DUP 的第二个答案是你应该关注的!
-
我猜
SecurityManager可能会派上用场。这些secure coding guidelines for Java SE 解释了如何正确使用它。另请参阅this SO post。
标签: java