在SaaS环境中使用客户端密钥的最佳做法(Best practice to make use of Client Secret Key in SaaS environment)
当全新客户端获得注册并存储在数据库列(对其有唯一约束 )时, Asp.NET Web API会生成一个秘密密钥 ( 使用32个字符长的对称算法 )。 在整个过程中,客户端必须在授权标头中提供密钥才能访问自己的资源( 多租户SaaS环境 )。 目前不需要访问令牌。 期望客户端密钥的主要原因是从数据库中过滤并提供适当的数据!
我认为在WHERE子句中使用ClientSecretKey从SQL表中获取客户端数据不会对性能友好,例如:
SELECT Multiple_Columns from ClientTable WHERE ClientSecretKey='X3i1aBer'
理想情况下,我宁愿使用在表中定义为IDENTITY列的ClientId来获取客户端记录。
问题:
- 如何最好地设计一个唯一的客户端密钥,在对数据库执行查询以获取特定的客户端记录时,该密钥应该绝对性能友好?
- 我的想法或设计中是否有缺陷?
任何想法都会非常感谢。
谢谢!
The Asp.NET Web API generates a Secret Key (using Symmetric Algorithm 32 characters long) when a brand new client gets registered which is stored in a database column (has unique constraint on it). Throughout, the API usage a client must provide the Secret Key in an Authorization header in order to access the it's own resources (Multi-Tenant SaaS environment). There is no requirement for Access Token at the moment. The main reason of expecting Client Secret Key is to filter and deliver an appropriate data from the database!
I think fetching Client Data from SQL Table using the ClientSecretKey in a WHERE clause is not going to be performance friendly, for an example:
SELECT Multiple_Columns from ClientTable WHERE ClientSecretKey='X3i1aBer'
Ideally, I would prefer to fetch the client records using ClientId which is defined as an IDENTITY column in a table.
Questions:
- How best to design a Unique Client Key that should be absolutely performance friendly when executing the query against database to fetch that specific client records?
- Is there any flaw in my thinking or the design?
Any ideas would be REALLY appreciated.
Thanks!
原文:https://stackoverflow.com/questions/34417742
最满意答案
你的
delta
有这个值:-64E9*1E-8
,实际上是-64
。那就是:它是一个负值。 由于delta
预计是实际值和期望值之间差异的上限,因此您需要将其设为正数。看一下
Assert
类的源代码 。 比较双打的地方是doubleIsDifferent
方法:static private boolean doubleIsDifferent(double d1, double d2, double delta) { if (Double.compare(d1, d2) == 0) { return false; } if ((Math.abs(d1 - d2) <= delta)) { return false; } return true; }
如您所见,相关表达式为
Math.abs(d1 - d2) <= delta
。 由于它使用Math.abs
因此左侧始终为0或正数。 因此它永远不会小于负值,因此此方法始终返回true
从而向调用者(assertEquals
)指示您的值不同。换句话说:将delta的定义更改为:
double delta = Math.abs(n * 1E-8);
Your
delta
has this value:-64E9*1E-8
which is actually-64
.That is: it is a negative value. Asdelta
is expected to be the upper limit on the difference between the actual and expected value you need to make it a positive number.Take a look at the source code of the
Assert
class. The place where doubles are compared is thedoubleIsDifferent
method:static private boolean doubleIsDifferent(double d1, double d2, double delta) { if (Double.compare(d1, d2) == 0) { return false; } if ((Math.abs(d1 - d2) <= delta)) { return false; } return true; }
As you can see the relevant expression is
Math.abs(d1 - d2) <= delta
. As it usesMath.abs
the left-hand side is always 0 or positive. Thus it can never be less than a negative value so this method always returnstrue
thus indicating to the caller (assertEquals
) that your values are different.In other words: change the definition of delta to:
double delta = Math.abs(n * 1E-8);
相关问答
更多-
getDataAsString()不返回null,因为return new ObjectMapper().writeValueAsString(this.data); 不抛出异常,而是返回一个字符串,表示"< null >" ,当然不等于null 。 如果writeValueAsString()返回此自定义null类型,则应该返回null,而不是仅在捕获异常时返回null。 getDataAsString() does not return null because return new ObjectMap ...
-
使用不同的数据多次运行相同的JUnit测试用例(Running the same JUnit test case multiple time with different data)[2022-10-30]
看看破解4.4理论 : import org.junit.Test; import org.junit.experimental.theories.*; import org.junit.runner.RunWith; @RunWith(Theories.class) public class PrimeTest { @Theory public void isPrime(int candidate) { // called with candidate=1, can ... -
有些情况我发现有用: 标记一个不完整的测试,因此它失败并警告您,直到您完成它 确保抛出异常: try{ // do stuff... fail("Exception not thrown"); }catch(Exception e){ assertTrue(e.hasSomeFlag()); } 注意: 自从JUnit4以来,有一种更优雅的方式来测试异常被抛出:使用注释@Test(expected=IndexOutOfBoundsException.class) 但是,如果您还想检查异常,那么 ...
-
您不需要完整路径来运行特定测试, test选项的值实际上是一种模式。 从官方文档 指定此参数以按文件名运行单个测试,覆盖包含/排除参数。 您在此处指定的每个模式将用于创建格式为** / $ {test} .java的包含模式,因此您只需键入“-Dtest = MyTest”即可运行名为“foo / MyTest.java”的单个测试。 测试图案前缀为! 将被排除在外。 所以你甚至可以简单地运行: mvn -Dtest=hello_Test test 或者在发生任何拼写错误的情况下仔细检查您的输入(根据您的 ...
-
Ant JUnit测试用例失败报告未报告测试用例名称(Ant JUnit Test case failure report not reporting testcase name)[2022-10-25]
根据我的经验,只有在没有附带的testcase属性的情况下获得error属性时,测试才会爆炸,就像它无法解析类时一样。 这是一个例子......java.lang.ClassNotFoundException: thinClientTests.userTests.NewUserTe ... -
看来我把TestNg和JUnit混淆了。 看看我误用的进口产品: import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; It seems I have confused TestNg with JUnit. Look at the imports I mistakingly used: import org.testng ...
-
你可以使用@Parameterized 。 这会给你你想要的。 这是一个简单的例子,对单个目录中的所有文件运行测试: @RunWith(Parameterized.class) public class ParameterizedTest { @Parameters(name = "{index}: file {0}") public static Iterable
-
我认为这是因为您只在第一次测试中导航到所需的URL,并且没有其他测试导航到所需的URL 尝试将导航步骤添加到setUpBeforeClass方法 尝试以下: package JUnitTesting; import static org.junit.Assert.*; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; i ...
-
可以在设置中识别正在执行哪个JUnit测试用例?(Possible to identify in setup which JUnit test case is being executed?)[2023-05-28]
事实证明,这个问题的答案很容易。 junit.framework.TestCase有一个getName方法,它返回当前测试用例的名称。 完善。 It turns out the answer to this question is very easy. junit.framework.TestCase has a getName method that returns the name of the current test case. Perfect. -
JUnit测试用例失败(JUnit test case failure)[2024-05-18]
你的delta有这个值: -64E9*1E-8 ,实际上是-64 。那就是:它是一个负值。 由于delta预计是实际值和期望值之间差异的上限,因此您需要将其设为正数。 看一下Assert类的源代码 。 比较双打的地方是doubleIsDifferent方法: static private boolean doubleIsDifferent(double d1, double d2, double delta) { if (Double.compare(d1, d2) == 0) { ...