4

現時点では、Spring Security が指定された URL、式、および注釈をどのように評価するかを理解しようとしています。security-context.xmlこれまでのところ、常に最初からエントリをチェックしているようです。それが である場合denyAllは、リクエストの処理を停止するだけです。

いくつかの構成オプションを設定するのを忘れたのかもしれませんが、(私の目には) Spring Security の注釈 ( 、 など) を使用して適切なホワイトリストを作成することはできませ@Secured@PermitAll

私が望むのは、基本的に、アクセスを許可@Controllerするために a 内のメソッドに注釈を付けることです。例えば:

@Controller
@RequestMapping("/test")
public MyController {
    @RequestMapping("")
    public void tryToGetSomething() {
      // no security annotation -> denyAll
    }

    @RequestMapping("/public")
    @PermitAll
    public void tryToGetSomethingPublic() {
      // this will always have access allowed
    }

    @RequestMapping("/admin")
    @Secured({"ROLE_ADMIN"})
    public void tryToGetSomethingReallyImportant() {
      // this can only be accessed by admins
    }
}

このアプローチの主な理由は次のとおりです。セキュリティ;-)。コードを書いているときに、いくつかの注釈を忘れる可能性は常にあります。また、この方法を使用すると、そのような間違いが機密データのセキュリティに影響を与えることはありません.

私の質問は次のとおりです。どうすればこれを達成できますか?

4

2 に答える 2

0

私は同じことを達成しようとしましたが、問題は、メソッド セキュリティ レベルが AOP を通じて呼び出されるすべてのメソッドに適用されることです。デフォルトでアクセスを拒否する場合、ほとんどすべてに注釈を付ける必要があります:)

URL ベースのセキュリティでは、ホワイトリストを使用して続行できます。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.anyRequest().denyAll();
}

残念ながら、明らかな欠点は、ここですべての URL を承認する必要があり、一種の依存関係が作成されることです。しかし、URL パス マッピングを一元化するのは良いことでしょうか?

于 2015-02-19T10:56:42.900 に答える
0

注釈と組み合わせてセキュリティ ポイントカットを使用することができます。

<global-method-security pre-post-annotations="enabled">
    <!-- Disable access to all controller methods -->
    <protect-pointcut expression="execution(* com.mycompany.controllers.*Controller.*(..))"
         access="ROLE_THAT_DOES_NOT_EXIST"/>
</global-method-security>
@Controller
@RequestMapping("/test")
public MyController {

    @RequestMapping("")
    public void tryToGetSomething() {
      // pointcut rule -> no one has ROLE_THAT_DOES_NOT_EXIST -> no one can call this code
    }

    @RequestMapping("/public")
    @PreAuthorized("permitAll")
    public void tryToGetSomethingPublic() {
      // annotations take precedence over pointcuts, so anyone can call this code due to @PreAuthorized("permitAll") rule
    }
}

公式ドキュメントの対応するエントリを参照してください。denyAllの代わりに使えるかもしれませんROLE_THAT_DOES_NOT_EXIST

お役に立てれば。

于 2013-07-17T13:21:23.077 に答える