@Autowired
簡単な質問です。コントローラーで@Service クラスを作成しました。
コントローラーのメソッドの 1 つに少しセキュリティを設定しようとしています。簡単にするために、これをテストしてみました
@PreAuthorize("@myService.helloThere()")
public void someControllerMethod() {
...
}
しかし、実際には成功しません。メソッド呼び出し中に例外を取得しています。
java.lang.IllegalArgumentException: 式 '@myService.helloThere()' を評価できませんでした
ここでELに何か欠けていますか?
アップデート
最後の原因例外を追加するだけです
原因: org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): Bean 'dummyServiceImpl' へのアクセスを解決するコンテキストに Bean リゾルバーが登録されていません
@Autowired を使用している場合、 StandardEvaluationContext でアクセスできない理由がわかりませんか?
更新 2
GlobalMethodSecurityConfiguration
カスタム拡張クラスに独自のロール階層を接続していたため、デフォルトでDefaultMethodSecurityExpressionHandler
はセットがありませんでした。applicationContext
これが仕様によるものなのか、明らかな何かが欠けていたのかはわかりません。リファレンス ページを検索したところ、問題の解決に役立つ別のSO スレッドが見つかりました。更新されたセキュリティ構成を投稿しています。
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Autowired
ApplicationContext applicationContext; //added this
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
final DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
handler.setApplicationContext(applicationContext); //added this
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER_MANAGER > ROLE_USER");
handler.setRoleHierarchy(roleHierarchy);
return handler;
}
}