使用自定义Spring Security过滤器,如果HTTP标头不包含特定的键值对,我想返回HTTP 401错误代码.
例:
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
final String val = request.getHeader(FOO_TOKEN)
if(val == null || !val.equals("FOO")) {
// token is not valid, return an HTTP 401 error code
...
}
else {
// token is good, let it proceed
chain.doFilter(req, res);
}
Run Code Online (Sandbox Code Playgroud)
据我了解,我可以做以下事情:
(1)((HttpServletResponse) res).setStatus(401)并跳过剩余的过滤链
要么
(2)抛出异常,最终导致Spring Security向客户端抛出401错误.
如果#1是更好的选择,如何在调用setStatus(401)响应后跳过过滤器链?
或者,如果#2是正确的方法,我应该抛出哪个例外?
// Assume class definition for Cat is here.
Cat makeCat() {
Cat lady = new Cat("fluffy");
return lady;
}
int main (...) {
Cat molly = makeCat();
molly->eatFood();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否会出现"免费使用"错误molly->eatFood()?
我刚读了一篇SO 帖子,说明FirstOrDefault()返回类型会根据所选元素的值而有所不同.
例:
ICollection<String> list = new List<String>();
list.Add("bye");
int a = (from x in list where (x == "hi") select x.Length).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
在此示例中,a由于int默认值为0 ,因此将等于0.
但是,我可以.Cast<int?>()根据已经链接的帖子追加,以便null在查询返回0结果时获取.
int? a = (from x in list where ... x.Length).Cast<int?>().FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
为什么我没有得到编译时错误(或至少是一个警告),对于我的第一个例子,我使用Nullable int(int?)而不是常规int?
如果我理解正确,使用int?when执行我的第一个查询将永远不会导致值null.
bind(>>=)Haskell中的以下代码无法编译:
ghci> [[1]] >>= Just
<interactive>:38:11:
Couldn't match type ‘Maybe’ with ‘[]’
Expected type: [t] -> [[t]]
Actual type: [t] -> Maybe [t]
In the second argument of ‘(>>=)’, namely ‘Just’
In the expression: [[1]] >>= Just
Run Code Online (Sandbox Code Playgroud)
但是,在Scala中,它确实编译并运行:
scala> List( List(1) ).flatMap(x => Some(x) )
res1: List[List[Int]] = List(List(1))
Run Code Online (Sandbox Code Playgroud)
Haskell的>>=签名是:
>>= :: Monad m => m a -> (a -> m b) -> m b
所以,在[[1]] >>= f,f类型应该是:a -> [b].
为什么Scala代码会编译?
给出以下从1到100的列表:
> let x = [1..100]
Run Code Online (Sandbox Code Playgroud)
我跑去sprint x观察其未评估的价值.
> :sprint x
x = _
Run Code Online (Sandbox Code Playgroud)
然后,我跑去seq评估它为弱头正常形式:
> seq x ()
()
Run Code Online (Sandbox Code Playgroud)
但重新运行的sprint x节目(我认为)是相同的价值.
> :sprint x
x = _
Run Code Online (Sandbox Code Playgroud)
这是为什么?
以下不起作用.
object Foo {
def union(s: Set[Int], t: Set[Int]): Set[Int] = t match {
case isEmpty => s
case (x:xs) => union(s + x, xs)
case _ => throw new Error("bad input")
}
}
Run Code Online (Sandbox Code Playgroud)
错误:未找到:输入xs
如何在一组上进行模式匹配?
Scala in Depth演示了Loaner模式:
def readFile[T](f: File)(handler: FileInputStream => T): T = {
val resource = new java.io.FileInputStream(f)
try {
handler(resource)
} finally {
resource.close()
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
readFile(new java.io.File("test.txt")) { input =>
println(input.readByte)
}
Run Code Online (Sandbox Code Playgroud)
此代码看似简单明了.什么是Scala中Loaner模式的"反模式",以便我知道如何避免它?
鉴于:
?: let f = putStrLn "foo" in 42
42
Run Code Online (Sandbox Code Playgroud)
什么是f类型?为什么"foo"在显示结果之前没有打印42?
最后,为什么以下不起作用?
?: :t f
<interactive>:1:1: Not in scope: ‘f’
Run Code Online (Sandbox Code Playgroud) 对于我的输入,可以有classname="half"或者"half.not-placeholder-value",Firebug显示两个输入都有一个固定的宽度25em.
input.half, input.half.not-placeholder-value {
max-width: 25em;
}
Run Code Online (Sandbox Code Playgroud)
然而,根据开发者工具,IE8似乎没有使用这个特定的max-width属性.我说"似乎没有使用",因为Firefox的行为与IE8在这个属性方面有所不同.
但是MDN 文档说IE7支持max-width.我错过了什么吗?
我尝试通过...导入7.4 MB的JSON文件
mongoimport -d mongoimport -c test --file jsonTest.json
但我看到了这个问题.
Wed Sep 04 13:08:52.378 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
这篇Stackoverflow 帖子提出了与1970年1月1日之前发生的日期类似的问题,但鉴于我的FailedToParse: Expecting '{': offset:0错误,这似乎不适用.