method-security.adoc 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. [[jc-method]]
  2. = Method Security
  3. From version 2.0 onwards, Spring Security has improved support substantially for adding security to your service layer methods.
  4. It provides support for JSR-250 annotation security as well as the framework's original `@Secured` annotation.
  5. From 3.0, you can also make use of new xref:servlet/authorization/expression-based.adoc#el-access[expression-based annotations].
  6. You can apply security to a single bean, by using the `intercept-methods` element to decorate the bean declaration, or you can secure multiple beans across the entire service layer by using AspectJ style pointcuts.
  7. [[jc-enable-method-security]]
  8. == EnableMethodSecurity
  9. In Spring Security 5.6, we can enable annotation-based security using the `@EnableMethodSecurity` annotation on any `@Configuration` instance.
  10. This improves upon `@EnableGlobalMethodSecurity` in a number of ways. `@EnableMethodSecurity`:
  11. 1. Uses the simplified `AuthorizationManager` API instead of metadata sources, config attributes, decision managers, and voters.
  12. This simplifies reuse and customization.
  13. 2. Favors direct bean-based configuration, instead of requiring extending `GlobalMethodSecurityConfiguration` to customize beans
  14. 3. Is built using native Spring AOP, removing abstractions and allowing you to use Spring AOP building blocks to customize
  15. 4. Checks for conflicting annotations to ensure an unambiguous security configuration
  16. 5. Complies with JSR-250
  17. 6. Enables `@PreAuthorize`, `@PostAuthorize`, `@PreFilter`, and `@PostFilter` by default
  18. [NOTE]
  19. ====
  20. For earlier versions, please read about similar support with <<jc-enable-global-method-security, @EnableGlobalMethodSecurity>>.
  21. ====
  22. For example, the following would enable Spring Security's `@PreAuthorize` annotation:
  23. .Method Security Configuration
  24. [tabs]
  25. ======
  26. Java::
  27. +
  28. [source,java,role="primary"]
  29. ----
  30. @Configuration
  31. @EnableMethodSecurity
  32. public class MethodSecurityConfig {
  33. // ...
  34. }
  35. ----
  36. Kotlin::
  37. +
  38. [source,kotlin,role="secondary"]
  39. ----
  40. @Configuration
  41. @EnableMethodSecurity
  42. class MethodSecurityConfig {
  43. // ...
  44. }
  45. ----
  46. Xml::
  47. +
  48. [source,xml,role="secondary"]
  49. ----
  50. <sec:method-security/>
  51. ----
  52. ======
  53. Adding an annotation to a method (on a class or interface) would then limit the access to that method accordingly.
  54. Spring Security's native annotation support defines a set of attributes for the method.
  55. These will be passed to the `DefaultAuthorizationMethodInterceptorChain` for it to make the actual decision:
  56. .Method Security Annotation Usage
  57. [tabs]
  58. ======
  59. Java::
  60. +
  61. [source,java,role="primary"]
  62. ----
  63. public interface BankService {
  64. @PreAuthorize("hasRole('USER')")
  65. Account readAccount(Long id);
  66. @PreAuthorize("hasRole('USER')")
  67. List<Account> findAccounts();
  68. @PreAuthorize("hasRole('TELLER')")
  69. Account post(Account account, Double amount);
  70. }
  71. ----
  72. Kotlin::
  73. +
  74. [source,kotlin,role="secondary"]
  75. ----
  76. interface BankService {
  77. @PreAuthorize("hasRole('USER')")
  78. fun readAccount(id : Long) : Account
  79. @PreAuthorize("hasRole('USER')")
  80. fun findAccounts() : List<Account>
  81. @PreAuthorize("hasRole('TELLER')")
  82. fun post(account : Account, amount : Double) : Account
  83. }
  84. ----
  85. ======
  86. You can enable support for Spring Security's `@Secured` annotation using:
  87. .@Secured Configuration
  88. [tabs]
  89. ======
  90. Java::
  91. +
  92. [source,java,role="primary"]
  93. ----
  94. @Configuration
  95. @EnableMethodSecurity(securedEnabled = true)
  96. public class MethodSecurityConfig {
  97. // ...
  98. }
  99. ----
  100. Kotlin::
  101. +
  102. [source,kotlin,role="secondary"]
  103. ----
  104. @Configuration
  105. @EnableMethodSecurity(securedEnabled = true)
  106. class MethodSecurityConfig {
  107. // ...
  108. }
  109. ----
  110. Xml::
  111. +
  112. [source,xml,role="secondary"]
  113. ----
  114. <sec:method-security secured-enabled="true"/>
  115. ----
  116. ======
  117. or JSR-250 using:
  118. .JSR-250 Configuration
  119. [tabs]
  120. ======
  121. Java::
  122. +
  123. [source,java,role="primary"]
  124. ----
  125. @Configuration
  126. @EnableMethodSecurity(jsr250Enabled = true)
  127. public class MethodSecurityConfig {
  128. // ...
  129. }
  130. ----
  131. Kotlin::
  132. +
  133. [source,kotlin,role="secondary"]
  134. ----
  135. @Configuration
  136. @EnableMethodSecurity(jsr250Enabled = true)
  137. class MethodSecurityConfig {
  138. // ...
  139. }
  140. ----
  141. Xml::
  142. +
  143. [source,xml,role="secondary"]
  144. ----
  145. <sec:method-security jsr250-enabled="true"/>
  146. ----
  147. ======
  148. === Customizing Authorization
  149. Spring Security's `@PreAuthorize`, `@PostAuthorize`, `@PreFilter`, and `@PostFilter` ship with rich expression-based support.
  150. [[jc-method-security-custom-expression-handler]]
  151. If you need to customize the way that expressions are handled, you can expose a custom `MethodSecurityExpressionHandler`, like so:
  152. .Custom MethodSecurityExpressionHandler
  153. [tabs]
  154. ======
  155. Java::
  156. +
  157. [source,java,role="primary"]
  158. ----
  159. @Bean
  160. static MethodSecurityExpressionHandler methodSecurityExpressionHandler() {
  161. DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
  162. handler.setTrustResolver(myCustomTrustResolver);
  163. return handler;
  164. }
  165. ----
  166. Kotlin::
  167. +
  168. [source,kotlin,role="secondary"]
  169. ----
  170. companion object {
  171. @Bean
  172. fun methodSecurityExpressionHandler() : MethodSecurityExpressionHandler {
  173. val handler = DefaultMethodSecurityExpressionHandler();
  174. handler.setTrustResolver(myCustomTrustResolver);
  175. return handler;
  176. }
  177. }
  178. ----
  179. Xml::
  180. +
  181. [source,xml,role="secondary"]
  182. ----
  183. <sec:method-security>
  184. <sec:expression-handler ref="myExpressionHandler"/>
  185. </sec:method-security>
  186. <bean id="myExpressionHandler"
  187. class="org.springframework.security.messaging.access.expression.DefaultMessageSecurityExpressionHandler">
  188. <property name="trustResolver" ref="myCustomTrustResolver"/>
  189. </bean>
  190. ----
  191. ======
  192. [TIP]
  193. ====
  194. We expose `MethodSecurityExpressionHandler` using a `static` method to ensure that Spring publishes it before it initializes Spring Security's method security `@Configuration` classes
  195. ====
  196. Also, for role-based authorization, Spring Security adds a default `ROLE_` prefix, which is uses when evaluating expressions like `hasRole`.
  197. [[jc-method-security-custom-granted-authority-defaults]]
  198. You can configure the authorization rules to use a different prefix by exposing a `GrantedAuthorityDefaults` bean, like so:
  199. .Custom MethodSecurityExpressionHandler
  200. [tabs]
  201. ======
  202. Java::
  203. +
  204. [source,java,role="primary"]
  205. ----
  206. @Bean
  207. static GrantedAuthorityDefaults grantedAuthorityDefaults() {
  208. return new GrantedAuthorityDefaults("MYPREFIX_");
  209. }
  210. ----
  211. Kotlin::
  212. +
  213. [source,kotlin,role="secondary"]
  214. ----
  215. companion object {
  216. @Bean
  217. fun grantedAuthorityDefaults() : GrantedAuthorityDefaults {
  218. return GrantedAuthorityDefaults("MYPREFIX_");
  219. }
  220. }
  221. ----
  222. Xml::
  223. +
  224. [source,xml,role="secondary"]
  225. ----
  226. <sec:method-security/>
  227. <bean id="grantedAuthorityDefaults" class="org.springframework.security.config.core.GrantedAuthorityDefaults">
  228. <constructor-arg value="MYPREFIX_"/>
  229. </bean>
  230. ----
  231. ======
  232. [TIP]
  233. ====
  234. We expose `GrantedAuthorityDefaults` using a `static` method to ensure that Spring publishes it before it initializes Spring Security's method security `@Configuration` classes
  235. ====
  236. [[jc-method-security-custom-authorization-manager]]
  237. === Custom Authorization Managers
  238. Method authorization is a combination of before- and after-method authorization.
  239. [NOTE]
  240. ====
  241. Before-method authorization is performed before the method is invoked.
  242. If that authorization denies access, the method is not invoked, and an `AccessDeniedException` is thrown.
  243. After-method authorization is performed after the method is invoked, but before the method returns to the caller.
  244. If that authorization denies access, the value is not returned, and an `AccessDeniedException` is thrown
  245. ====
  246. To recreate what adding `@EnableMethodSecurity` does by default, you would publish the following configuration:
  247. .Full Pre-post Method Security Configuration
  248. [tabs]
  249. ======
  250. Java::
  251. +
  252. [source,java,role="primary"]
  253. ----
  254. @Configuration
  255. @EnableMethodSecurity(prePostEnabled = false)
  256. class MethodSecurityConfig {
  257. @Bean
  258. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  259. Advisor preFilterAuthorizationMethodInterceptor() {
  260. return new PreFilterAuthorizationMethodInterceptor();
  261. }
  262. @Bean
  263. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  264. Advisor preAuthorizeAuthorizationMethodInterceptor() {
  265. return AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
  266. }
  267. @Bean
  268. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  269. Advisor postAuthorizeAuthorizationMethodInterceptor() {
  270. return AuthorizationManagerAfterMethodInterceptor.postAuthorize();
  271. }
  272. @Bean
  273. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  274. Advisor postFilterAuthorizationMethodInterceptor() {
  275. return new PostFilterAuthorizationMethodInterceptor();
  276. }
  277. }
  278. ----
  279. Kotlin::
  280. +
  281. [source,kotlin,role="secondary"]
  282. ----
  283. @Configuration
  284. @EnableMethodSecurity(prePostEnabled = false)
  285. class MethodSecurityConfig {
  286. @Bean
  287. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  288. fun preFilterAuthorizationMethodInterceptor() : Advisor {
  289. return PreFilterAuthorizationMethodInterceptor();
  290. }
  291. @Bean
  292. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  293. fun preAuthorizeAuthorizationMethodInterceptor() : Advisor {
  294. return AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
  295. }
  296. @Bean
  297. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  298. fun postAuthorizeAuthorizationMethodInterceptor() : Advisor {
  299. return AuthorizationManagerAfterMethodInterceptor.postAuthorize();
  300. }
  301. @Bean
  302. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  303. fun postFilterAuthorizationMethodInterceptor() : Advisor {
  304. return PostFilterAuthorizationMethodInterceptor();
  305. }
  306. }
  307. ----
  308. Xml::
  309. +
  310. [source,xml,role="secondary"]
  311. ----
  312. <sec:method-security pre-post-enabled="false"/>
  313. <aop:config/>
  314. <bean id="preFilterAuthorizationMethodInterceptor"
  315. class="org.springframework.security.authorization.method.PreFilterAuthorizationMethodInterceptor"/>
  316. <bean id="preAuthorizeAuthorizationMethodInterceptor"
  317. class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
  318. factory-method="preAuthorize"/>
  319. <bean id="postAuthorizeAuthorizationMethodInterceptor"
  320. class="org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor"
  321. factory-method="postAuthorize"/>
  322. <bean id="postFilterAuthorizationMethodInterceptor"
  323. class="org.springframework.security.authorization.method.PostFilterAuthorizationMethodInterceptor"/>
  324. ----
  325. ======
  326. Notice that Spring Security's method security is built using Spring AOP.
  327. So, interceptors are invoked based on the order specified.
  328. This can be customized by calling `setOrder` on the interceptor instances like so:
  329. .Publish Custom Advisor
  330. [tabs]
  331. ======
  332. Java::
  333. +
  334. [source,java,role="primary"]
  335. ----
  336. @Bean
  337. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  338. Advisor postFilterAuthorizationMethodInterceptor() {
  339. PostFilterAuthorizationMethodInterceptor interceptor = new PostFilterAuthorizationMethodInterceptor();
  340. interceptor.setOrder(AuthorizationInterceptorOrders.POST_AUTHORIZE.getOrder() - 1);
  341. return interceptor;
  342. }
  343. ----
  344. Kotlin::
  345. +
  346. [source,kotlin,role="secondary"]
  347. ----
  348. @Bean
  349. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  350. fun postFilterAuthorizationMethodInterceptor() : Advisor {
  351. val interceptor = PostFilterAuthorizationMethodInterceptor();
  352. interceptor.setOrder(AuthorizationInterceptorOrders.POST_AUTHORIZE.getOrder() - 1);
  353. return interceptor;
  354. }
  355. ----
  356. Xml::
  357. +
  358. [source,xml,role="secondary"]
  359. ----
  360. <bean id="postFilterAuthorizationMethodInterceptor"
  361. class="org.springframework.security.authorization.method.PostFilterAuthorizationMethodInterceptor">
  362. <property name="order"
  363. value="#{T(org.springframework.security.authorization.method.AuthorizationInterceptorsOrder).POST_AUTHORIZE.getOrder() -1}"/>
  364. </bean>
  365. ----
  366. ======
  367. You may want to only support `@PreAuthorize` in your application, in which case you can do the following:
  368. .Only @PreAuthorize Configuration
  369. [tabs]
  370. ======
  371. Java::
  372. +
  373. [source,java,role="primary"]
  374. ----
  375. @Configuration
  376. @EnableMethodSecurity(prePostEnabled = false)
  377. class MethodSecurityConfig {
  378. @Bean
  379. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  380. Advisor preAuthorize() {
  381. return AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
  382. }
  383. }
  384. ----
  385. Kotlin::
  386. +
  387. [source,kotlin,role="secondary"]
  388. ----
  389. @Configuration
  390. @EnableMethodSecurity(prePostEnabled = false)
  391. class MethodSecurityConfig {
  392. @Bean
  393. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  394. fun preAuthorize() : Advisor {
  395. return AuthorizationManagerBeforeMethodInterceptor.preAuthorize()
  396. }
  397. }
  398. ----
  399. Xml::
  400. +
  401. [source,xml,role="secondary"]
  402. ----
  403. <sec:method-security pre-post-enabled="false"/>
  404. <aop:config/>
  405. <bean id="preAuthorizeAuthorizationMethodInterceptor"
  406. class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
  407. factory-method="preAuthorize"/>
  408. ----
  409. ======
  410. Or, you may have a custom before-method `AuthorizationManager` that you want to add to the list.
  411. In this case, you will need to tell Spring Security both the `AuthorizationManager` and to which methods and classes your authorization manager applies.
  412. Thus, you can configure Spring Security to invoke your `AuthorizationManager` in between `@PreAuthorize` and `@PostAuthorize` like so:
  413. .Custom Before Advisor
  414. [tabs]
  415. ======
  416. Java::
  417. +
  418. [source,java,role="primary"]
  419. ----
  420. @Configuration
  421. @EnableMethodSecurity
  422. class MethodSecurityConfig {
  423. @Bean
  424. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  425. public Advisor customAuthorize() {
  426. JdkRegexpMethodPointcut pattern = new JdkRegexpMethodPointcut();
  427. pattern.setPattern("org.mycompany.myapp.service.*");
  428. AuthorizationManager<MethodInvocation> rule = AuthorityAuthorizationManager.isAuthenticated();
  429. AuthorizationManagerBeforeMethodInterceptor interceptor = new AuthorizationManagerBeforeMethodInterceptor(pattern, rule);
  430. interceptor.setOrder(AuthorizationInterceptorsOrder.PRE_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1);
  431. return interceptor;
  432. }
  433. }
  434. ----
  435. Kotlin::
  436. +
  437. [source,kotlin,role="secondary"]
  438. ----
  439. @Configuration
  440. @EnableMethodSecurity
  441. class MethodSecurityConfig {
  442. @Bean
  443. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  444. fun customAuthorize() : Advisor {
  445. val pattern = JdkRegexpMethodPointcut();
  446. pattern.setPattern("org.mycompany.myapp.service.*");
  447. val rule = AuthorityAuthorizationManager.isAuthenticated();
  448. val interceptor = AuthorizationManagerBeforeMethodInterceptor(pattern, rule);
  449. interceptor.setOrder(AuthorizationInterceptorsOrder.PRE_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1);
  450. return interceptor;
  451. }
  452. }
  453. ----
  454. Xml::
  455. +
  456. [source,xml,role="secondary"]
  457. ----
  458. <sec:method-security/>
  459. <aop:config/>
  460. <bean id="customAuthorize"
  461. class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor">
  462. <constructor-arg>
  463. <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
  464. <property name="pattern" value="org.mycompany.myapp.service.*"/>
  465. </bean>
  466. </constructor-arg>
  467. <constructor-arg>
  468. <bean class="org.springframework.security.authorization.AuthorityAuthorizationManager"
  469. factory-method="isAuthenticated"/>
  470. </constructor-arg>
  471. <property name="order"
  472. value="#{T(org.springframework.security.authorization.method.AuthorizationInterceptorsOrder).PRE_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1}"/>
  473. </bean>
  474. ----
  475. ======
  476. [TIP]
  477. ====
  478. You can place your interceptor in between Spring Security method interceptors using the order constants specified in `AuthorizationInterceptorsOrder`.
  479. ====
  480. The same can be done for after-method authorization.
  481. After-method authorization is generally concerned with analysing the return value to verify access.
  482. For example, you might have a method that confirms that the account requested actually belongs to the logged-in user like so:
  483. .@PostAuthorize example
  484. [tabs]
  485. ======
  486. Java::
  487. +
  488. [source,java,role="primary"]
  489. ----
  490. public interface BankService {
  491. @PreAuthorize("hasRole('USER')")
  492. @PostAuthorize("returnObject.owner == authentication.name")
  493. Account readAccount(Long id);
  494. }
  495. ----
  496. Kotlin::
  497. +
  498. [source,kotlin,role="secondary"]
  499. ----
  500. interface BankService {
  501. @PreAuthorize("hasRole('USER')")
  502. @PostAuthorize("returnObject.owner == authentication.name")
  503. fun readAccount(id : Long) : Account
  504. }
  505. ----
  506. ======
  507. You can supply your own `AuthorizationMethodInterceptor` to customize how access to the return value is evaluated.
  508. For example, if you have your own custom annotation, you can configure it like so:
  509. .Custom After Advisor
  510. [tabs]
  511. ======
  512. Java::
  513. +
  514. [source,java,role="primary"]
  515. ----
  516. @Configuration
  517. @EnableMethodSecurity
  518. class MethodSecurityConfig {
  519. @Bean
  520. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  521. public Advisor customAuthorize(AuthorizationManager<MethodInvocationResult> rules) {
  522. AnnotationMatchingPointcut pattern = new AnnotationMatchingPointcut(MySecurityAnnotation.class);
  523. AuthorizationManagerAfterMethodInterceptor interceptor = new AuthorizationManagerAfterMethodInterceptor(pattern, rules);
  524. interceptor.setOrder(AuthorizationInterceptorsOrder.POST_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1);
  525. return interceptor;
  526. }
  527. }
  528. ----
  529. Kotlin::
  530. +
  531. [source,kotlin,role="secondary"]
  532. ----
  533. @Configuration
  534. @EnableMethodSecurity
  535. class MethodSecurityConfig {
  536. @Bean
  537. @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
  538. fun customAuthorize(rules : AuthorizationManager<MethodInvocationResult>) : Advisor {
  539. val pattern = AnnotationMatchingPointcut(MySecurityAnnotation::class.java);
  540. val interceptor = AuthorizationManagerAfterMethodInterceptor(pattern, rules);
  541. interceptor.setOrder(AuthorizationInterceptorsOrder.POST_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1);
  542. return interceptor;
  543. }
  544. }
  545. ----
  546. Xml::
  547. +
  548. [source,xml,role="secondary"]
  549. ----
  550. <sec:method-security/>
  551. <aop:config/>
  552. <bean id="customAuthorize"
  553. class="org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor">
  554. <constructor-arg>
  555. <bean class="org.springframework.aop.support.annotation.AnnotationMethodMatcher">
  556. <constructor-arg value="#{T(org.mycompany.MySecurityAnnotation)}"/>
  557. </bean>
  558. </constructor-arg>
  559. <constructor-arg>
  560. <bean class="org.springframework.security.authorization.AuthorityAuthorizationManager"
  561. factory-method="isAuthenticated"/>
  562. </constructor-arg>
  563. <property name="order"
  564. value="#{T(org.springframework.security.authorization.method.AuthorizationInterceptorsOrder).PRE_AUTHORIZE_ADVISOR_ORDER.getOrder() + 1}"/>
  565. </bean>
  566. ----
  567. ======
  568. and it will be invoked after the `@PostAuthorize` interceptor.
  569. [[jc-enable-global-method-security]]
  570. == EnableGlobalMethodSecurity
  571. We can enable annotation-based security by using the `@EnableGlobalMethodSecurity` annotation on any `@Configuration` instance.
  572. The following example enables Spring Security's `@Secured` annotation:
  573. [tabs]
  574. ======
  575. Java::
  576. +
  577. [source,java,role="primary"]
  578. ----
  579. @Configuration
  580. @EnableGlobalMethodSecurity(securedEnabled = true)
  581. public class MethodSecurityConfig {
  582. // ...
  583. }
  584. ----
  585. Kotlin::
  586. +
  587. [source,kotlin,role="secondary"]
  588. ----
  589. @Configuration
  590. @EnableGlobalMethodSecurity(securedEnabled = true)
  591. open class MethodSecurityConfig {
  592. // ...
  593. }
  594. ----
  595. ======
  596. Adding an annotation to a method (on a class or interface) would then limit the access to that method accordingly.
  597. Spring Security's native annotation support defines a set of attributes for the method.
  598. These are passed to the `AccessDecisionManager` for it to make the actual decision:
  599. [tabs]
  600. ======
  601. Java::
  602. +
  603. [source,java,role="primary"]
  604. ----
  605. public interface BankService {
  606. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  607. public Account readAccount(Long id);
  608. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  609. public Account[] findAccounts();
  610. @Secured("ROLE_TELLER")
  611. public Account post(Account account, double amount);
  612. }
  613. ----
  614. Kotlin::
  615. +
  616. [source,kotlin,role="secondary"]
  617. ----
  618. interface BankService {
  619. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  620. fun readAccount(id: Long): Account
  621. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  622. fun findAccounts(): Array<Account>
  623. @Secured("ROLE_TELLER")
  624. fun post(account: Account, amount: Double): Account
  625. }
  626. ----
  627. ======
  628. Support for JSR-250 annotations can be enabled by using:
  629. [tabs]
  630. ======
  631. Java::
  632. +
  633. [source,java,role="primary"]
  634. ----
  635. @Configuration
  636. @EnableGlobalMethodSecurity(jsr250Enabled = true)
  637. public class MethodSecurityConfig {
  638. // ...
  639. }
  640. ----
  641. Kotlin::
  642. +
  643. [source,kotlin,role="secondary"]
  644. ----
  645. @Configuration
  646. @EnableGlobalMethodSecurity(jsr250Enabled = true)
  647. open class MethodSecurityConfig {
  648. // ...
  649. }
  650. ----
  651. ======
  652. These are standards-based and let simple role-based constraints be applied but do not have the power Spring Security's native annotations.
  653. To use the new expression-based syntax, you would use:
  654. [tabs]
  655. ======
  656. Java::
  657. +
  658. [source,java,role="primary"]
  659. ----
  660. @Configuration
  661. @EnableGlobalMethodSecurity(prePostEnabled = true)
  662. public class MethodSecurityConfig {
  663. // ...
  664. }
  665. ----
  666. Kotlin::
  667. +
  668. [source,kotlin,role="secondary"]
  669. ----
  670. @Configuration
  671. @EnableGlobalMethodSecurity(prePostEnabled = true)
  672. open class MethodSecurityConfig {
  673. // ...
  674. }
  675. ----
  676. ======
  677. The equivalent Java code is:
  678. [tabs]
  679. ======
  680. Java::
  681. +
  682. [source,java,role="primary"]
  683. ----
  684. public interface BankService {
  685. @PreAuthorize("isAnonymous()")
  686. public Account readAccount(Long id);
  687. @PreAuthorize("isAnonymous()")
  688. public Account[] findAccounts();
  689. @PreAuthorize("hasAuthority('ROLE_TELLER')")
  690. public Account post(Account account, double amount);
  691. }
  692. ----
  693. Kotlin::
  694. +
  695. [source,kotlin,role="secondary"]
  696. ----
  697. interface BankService {
  698. @PreAuthorize("isAnonymous()")
  699. fun readAccount(id: Long): Account
  700. @PreAuthorize("isAnonymous()")
  701. fun findAccounts(): Array<Account>
  702. @PreAuthorize("hasAuthority('ROLE_TELLER')")
  703. fun post(account: Account, amount: Double): Account
  704. }
  705. ----
  706. ======
  707. == GlobalMethodSecurityConfiguration
  708. Sometimes, you may need to perform operations that are more complicated than are possible with the `@EnableGlobalMethodSecurity` annotation.
  709. For these instances, you can extend the `GlobalMethodSecurityConfiguration`, ensuring that the `@EnableGlobalMethodSecurity` annotation is present on your subclass.
  710. For example, if you wanted to provide a custom `MethodSecurityExpressionHandler`, you could use the following configuration:
  711. [tabs]
  712. ======
  713. Java::
  714. +
  715. [source,java,role="primary"]
  716. ----
  717. @Configuration
  718. @EnableGlobalMethodSecurity(prePostEnabled = true)
  719. public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
  720. @Override
  721. protected MethodSecurityExpressionHandler createExpressionHandler() {
  722. // ... create and return custom MethodSecurityExpressionHandler ...
  723. return expressionHandler;
  724. }
  725. }
  726. ----
  727. Kotlin::
  728. +
  729. [source,kotlin,role="secondary"]
  730. ----
  731. @Configuration
  732. @EnableGlobalMethodSecurity(prePostEnabled = true)
  733. open class MethodSecurityConfig : GlobalMethodSecurityConfiguration() {
  734. override fun createExpressionHandler(): MethodSecurityExpressionHandler {
  735. // ... create and return custom MethodSecurityExpressionHandler ...
  736. return expressionHandler
  737. }
  738. }
  739. ----
  740. ======
  741. For additional information about methods that can be overridden, see the Javadoc for the {security-api-url}org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.html[`GlobalMethodSecurityConfiguration`] class.
  742. [[ns-global-method]]
  743. == The <global-method-security> Element
  744. This element is used to enable annotation-based security in your application (by setting the appropriate attributes on the element) and to group together security pointcut declarations that are applied across your entire application context.
  745. You should only declare one `<global-method-security>` element.
  746. The following declaration enables support for Spring Security's `@Secured`:
  747. [source,xml]
  748. ----
  749. <global-method-security secured-annotations="enabled" />
  750. ----
  751. Adding an annotation to a method (on a class or interface) would then limit the access to that method accordingly.
  752. Spring Security's native annotation support defines a set of attributes for the method.
  753. These are passed to the `AccessDecisionManager` for it to make the actual decision.
  754. The following example shows the `@Secured` annotation in a typical interface:
  755. [tabs]
  756. ======
  757. Java::
  758. +
  759. [source,java,role="primary"]
  760. ----
  761. public interface BankService {
  762. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  763. public Account readAccount(Long id);
  764. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  765. public Account[] findAccounts();
  766. @Secured("ROLE_TELLER")
  767. public Account post(Account account, double amount);
  768. }
  769. ----
  770. Kotlin::
  771. +
  772. [source,kotlin,role="secondary"]
  773. ----
  774. interface BankService {
  775. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  776. fun readAccount(id: Long): Account
  777. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  778. fun findAccounts(): Array<Account>
  779. @Secured("ROLE_TELLER")
  780. fun post(account: Account, amount: Double): Account
  781. }
  782. ----
  783. ======
  784. Support for JSR-250 annotations can be enabled by using:
  785. [source,xml]
  786. ----
  787. <global-method-security jsr250-annotations="enabled" />
  788. ----
  789. These are standards-based and allow simple role-based constraints to be applied, but they do not have the power Spring Security's native annotations.
  790. To use the expression-based syntax, use:
  791. [source,xml]
  792. ----
  793. <global-method-security pre-post-annotations="enabled" />
  794. ----
  795. The equivalent Java code is:
  796. [tabs]
  797. ======
  798. Java::
  799. +
  800. [source,java,role="primary"]
  801. ----
  802. public interface BankService {
  803. @PreAuthorize("isAnonymous()")
  804. public Account readAccount(Long id);
  805. @PreAuthorize("isAnonymous()")
  806. public Account[] findAccounts();
  807. @PreAuthorize("hasAuthority('ROLE_TELLER')")
  808. public Account post(Account account, double amount);
  809. }
  810. ----
  811. Kotlin::
  812. +
  813. [source,kotlin,role="secondary"]
  814. ----
  815. interface BankService {
  816. @PreAuthorize("isAnonymous()")
  817. fun readAccount(id: Long): Account
  818. @PreAuthorize("isAnonymous()")
  819. fun findAccounts(): Array<Account>
  820. @PreAuthorize("hasAuthority('ROLE_TELLER')")
  821. fun post(account: Account, amount: Double): Account
  822. }
  823. ----
  824. ======
  825. Expression-based annotations are a good choice if you need to define simple rules that go beyond checking the role names against the user's list of authorities.
  826. [NOTE]
  827. ====
  828. The annotated methods will only be secured for instances which are defined as Spring beans (in the same application context in which method-security is enabled).
  829. If you want to secure instances which are not created by Spring (using the `new` operator, for example) then you need to use AspectJ.
  830. ====
  831. [NOTE]
  832. ====
  833. You can enable more than one type of annotation in the same application, but only one type should be used for any interface or class as the behaviour will not be well-defined otherwise.
  834. If two annotations are found which apply to a particular method, then only one of them will be applied.
  835. ====
  836. [[ns-protect-pointcut]]
  837. == Adding Security Pointcuts by using protect-pointcut
  838. `protect-pointcut` is particularly powerful, as it lets you apply security to many beans with only a simple declaration.
  839. Consider the following example:
  840. [source,xml]
  841. ----
  842. <global-method-security>
  843. <protect-pointcut expression="execution(* com.mycompany.*Service.*(..))"
  844. access="ROLE_USER"/>
  845. </global-method-security>
  846. ----
  847. d.
  848. This configuration protects all methods on beans declared in the application context whose classes are in the `com.mycompany` package and whose class names end in `Service`.
  849. Only users with the `ROLE_USER` role can invoke these methods.
  850. As with URL matching, the most specific matches must come first in the list of pointcuts, as the first matching expression is used.
  851. Security annotations take precedence over pointcuts.