2016-05-04 14 views
2

これは実際には2つの質問です。なぜなら、これはSpring Securityのリファレンスで詳しく説明されていないからです。 hasRole()とhasPermission()Webセキュリティ式はどのように機能しますか?

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests() 
       .antMatchers("/admin").hasRole("ADMIN") 
       .antMatchers("/admin").access("hasRole('ADMIN')"); 

はその後、春には両方のケースでは、現在のユーザーのロールを探している:私の構成では、私はこのようなコードを持っているときに最初の質問は、ありますか? UserDetailsS​​erviceでloadUserByUsername()メソッドを呼び出した後、取得したUserDetailsでgetAuthorities()を呼び出すのでしょうか?

私の2番目の質問は、hasPermission()式に関するものです。私は、カスタムPermissionEvaluatorがあると、それはコンフィギュレーション・クラスに操作できるようにする方法は例えば、そこにある:

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests() 
       .antMatchers("/admin").access("hasPermission(...)") 

それともこれが唯一のメソッドレベルで動作表現のですか?

答えて

0

2番目の質問に答えて:はい、あなた自身のPermissionEvaluatorをHttpSecurityとともに使用することができます。その後、

@Bean 
public DefaultWebSecurityExpressionHandler webExpressionHandler(PermissionEvaluator myPermissionEvaluator) { 
    final DefaultWebSecurityExpressionHandler webSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler(); 
    webSecurityExpressionHandler.setPermissionEvaluator(siraPermissionEvaluator); 
    return webSecurityExpressionHandler; 
} 

/*this option then needs to autowire the bean*/ 

などHttpSecurityでそれを使用します:

http.authorizeRequests().expressionHandler(webSecurityExpressionHandler) 
.antMatchers("/admin").access("hasPermission(...)"); 
まず、あなたは

final DefaultWebSecurityExpressionHandler webSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();   
webSecurityExpressionHandler.setPermissionEvaluator(myPermissionEvaluator); 

や豆などのようなWebSecurityExpressionHandlerを定義する必要があります

関連する問題