不要为特定的URL和HTTP方法调用Spring Security预身份验证筛选器

时间:2017-01-23 12:50:22

标签: java spring spring-security

我有一个自定义的预身份验证过滤器,它基本上验证并创建一个令牌。

我只想将此预身份验证过滤器仅用于安全API。

例如,我有这两个API:

  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IngredientTableViewCell cell.ingredientNameTextField.text = ingredients [indexPath.row].ingredientName cell.numberofItem.text = "1" let cellcalory = ingredients [indexPath.row].ingredientCalory cell.itemTotalCalory.text = cellcalory cell.plusButton.tag = Int(cell.itemTotalCalory.text!)! //indexPath.row cell.plusButton.addTarget(self, action:#selector(plusAction), for: .touchUpInside) cell.minusButton.tag = Int(cell.itemTotalCalory.text!)! cell.minusButton.addTarget(self, action:#selector(minusAction), for: .touchUpInside) return cell } @IBAction func plusAction(sender: UIButton) { let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell let buttonRow = sender.tag if cell.numberofItem.text == "1" || cell.numberofItem.text != "1" { cell.numberofItem.text = "1" let textValue1 = cell.numberofItem.text var textValue = Int(textValue1!) textValue = textValue! + 1 cell.numberofItem.text = String(describing: textValue) let oldcalory = buttonRow cell.itemTotalCalory.text = String (((textValue! * Int(oldcalory)) + Int(oldcalory))) let newcalory = cell.itemTotalCalory.text refresh(newcalory: newcalory!); } } func refresh(newcalory :String) { let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell cell.itemTotalCalory.text = newcalory DispatchQueue.main.async { self.ingredientTableView.reloadData() } } (不安全)
  • GET /api/products(安全)

当我使用时:

POST /api/products

我的预身份验证过滤器未被调用,但这样做我将两个API都置之不安全,因为两者都具有相同的模式<http pattern="/api/products" security="none"/>

我想为我的安全API获取名为的预身份验证过滤器,即/api/products

问题是:如何告诉POST /api/products区分HTTP方法(security="none"GET)?

1 个答案:

答案 0 :(得分:1)

您可以定义RequestMatcher AntPathRequestMatcher并添加参考,请参阅Spring Security Reference

  

request-matcher-ref 对实现RequestMatcher的bean的引用,该bean将确定是否应使用此FilterChain。这是模式的更强大的替代方案。

您修改后的配置:

<http request-matcher-ref="myMatcher"/>

<b:bean id="myMatcher" class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
    <b:constructor-arg value="/api/products"/>
    <b:constructor-arg value="POST"/>
</b:bean>