0%

Open Authorization,一个授权标准。本文先讲概念,再讲具体实现最佳实践案例。

应用场景:

  • 使用第三方账户登录
  • API鉴权

只凭理论既不好表达,也不好理解。无效的类比会增加读者的理解负担,更利于表达者而不是听众,所以最好的方式就是尽可能地结合应用场景去举例理解。

JWT(JsonWebToken), session, token, access token, refresh token
OAuth, SSO( Single Sign On, 单点登录 ), SAML(Security Assertion Markup Language), OpenID

申请个人测试权限
下载demo

使用

1
_.difference(array, [values])

该方法用于对数组进行过滤,排除给定的值,剩下的值组成数组返回。

  • array (Array): 目标数组
  • [values] (…Array): 要排除的值组成的数组
1
_.difference([1, 2, 2, 3, 3], [1, 2, 2]) //[ 3, 3 ]

代码

核心逻辑在baseDifference.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import SetCache from './SetCache.js'
import arrayIncludes from './arrayIncludes.js'
import arrayIncludesWith from './arrayIncludesWith.js'
import map from '../map.js'
import cacheHas from './cacheHas.js'

/** Used as the size to enable large array optimizations. */
const LARGE_ARRAY_SIZE = 200

/**
* The base implementation of methods like `difference` without support
* for excluding multiple arrays.
*
* @private
* @param {Array} array The array to inspect.
* @param {Array} values The values to exclude.
* @param {Function} [iteratee] The iteratee invoked per element.
* @param {Function} [comparator] The comparator invoked per element.
* @returns {Array} Returns the new array of filtered values.
*/
function baseDifference(array, values, iteratee, comparator) {
let includes = arrayIncludes
let isCommon = true
const result = []
const valuesLength = values.length

if (!array.length) {
return result
}
if (iteratee) {
values = map(values, (value) => iteratee(value))
}
if (comparator) {
includes = arrayIncludesWith
isCommon = false
}
else if (values.length >= LARGE_ARRAY_SIZE) {
includes = cacheHas
isCommon = false
values = new SetCache(values)
}
outer:
for (let value of array) {
const computed = iteratee == null ? value : iteratee(value)

value = (comparator || value !== 0) ? value : 0
if (isCommon && computed === computed) {
let valuesIndex = valuesLength
while (valuesIndex--) {
if (values[valuesIndex] === computed) {
continue outer
}
}
result.push(value)
}
else if (!includes(values, computed, comparator)) {
result.push(value)
}
}
return result
}

export default baseDifference

参考

lodash/baseDifference.js at master · lodash/lodash · GitHub

Lodash 文档