12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236 |
- [[migration]]
- = Migrating to 6.0
- The Spring Security team has prepared the 5.8 release to simplify upgrading to Spring Security 6.0.
- Use 5.8 and the steps below to minimize changes when
- ifdef::spring-security-version[]
- xref:6.0.0@migration.adoc[updating to 6.0]
- endif::[]
- ifndef::spring-security-version[]
- updating to 6.0
- endif::[]
- .
- == Update to Spring Security 5.8
- The first step is to ensure you are the latest patch release of Spring Boot 2.7.
- Next, you should ensure you are on the latest patch release of Spring Security 5.8.
- If you are using Spring Boot, you will need to override the Spring Boot version from Spring Security 5.7 to 5.8.
- Spring Security 5.8 is fully compatible with Spring Security 5.7 and thus Spring Boot 2.7.
- For directions, on how to update to Spring Security 5.8 visit the xref::getting-spring-security.adoc[] section of the reference guide.
- == Servlet
- === Explicit SessionAuthenticationStrategy
- In Spring Security 5, the default configuration relies on `SessionManagementFilter` to detect if a user just authenticated and invoke the `SessionAuthenticationStrategy`.
- The problem with this is that it means that in a typical setup, the `HttpSession` must be read for every request.
- In Spring Security 6, the default is that authentication mechanisms themselves must invoke the `SessionAuthenticationStrategy`.
- This means that there is no need to detect when `Authentication` is done and thus the `HttpSession` does not need to be read for every request.
- To opt into the new Spring Security 6 default, the following configuration can be used.
- .Require Explicit `SessionAuthenticationStrategy` Invocation
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
- http
- // ...
- .sessionManagement((sessions) -> sessions
- .requireExplicitAuthenticationStrategy(true)
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
- http {
- sessionManagement {
- requireExplicitAuthenticationStrategy = true
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <!-- ... -->
- <session-management authentication-strategy-explicit-invocation="true"/>
- </http>
- ----
- ====
- If this breaks your application, then you can explicitly opt into the 5.8 defaults using the following configuration:
- .Explicit use Spring Security 5.8 defaults for `SessionAuthenticationStrategy`
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
- http
- // ...
- .sessionManagement((sessions) -> sessions
- .requireExplicitAuthenticationStrategy(false)
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
- http {
- sessionManagement {
- requireExplicitAuthenticationStrategy = false
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <!-- ... -->
- <session-management authentication-strategy-explicit-invocation="false"/>
- </http>
- ----
- ====
- === Defer Loading CsrfToken
- In Spring Security 5, the default behavior is that the `CsrfToken` will be loaded on every request.
- This means that in a typical setup, the `HttpSession` must be read for every request even if it is unnecessary.
- In Spring Security 6, the default is that the lookup of the `CsrfToken` will be deferred until it is needed.
- To opt into the new Spring Security 6 default, the following configuration can be used.
- .Defer Loading `CsrfToken`
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
- CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName("_csrf");
- http
- // ...
- .csrf((csrf) -> csrf
- .csrfTokenRequestHandler(requestHandler)
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
- val requestHandler = CsrfTokenRequestAttributeHandler()
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName("_csrf")
- http {
- csrf {
- csrfTokenRequestHandler = requestHandler
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <!-- ... -->
- <csrf request-handler-ref="requestHandler"/>
- </http>
- <b:bean id="requestHandler"
- class="org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler"
- p:csrfRequestAttributeName="_csrf"/>
- ----
- ====
- If this breaks your application, then you can explicitly opt into the 5.8 defaults using the following configuration:
- .Explicit Configure `CsrfToken` with 5.8 Defaults
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
- CsrfTokenRequestAttributeHandler requestHandler = new CsrfTokenRequestAttributeHandler();
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName(null);
- http
- // ...
- .csrf((csrf) -> csrf
- .csrfTokenRequestHandler(requestHandler)
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
- val requestHandler = CsrfTokenRequestAttributeHandler()
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName(null)
- http {
- csrf {
- csrfTokenRequestHandler = requestHandler
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <!-- ... -->
- <csrf request-handler-ref="requestHandler"/>
- </http>
- <b:bean id="requestHandler"
- class="org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler">
- <b:property name="csrfRequestAttributeName">
- <b:null/>
- </b:property>
- </b:bean>
- ----
- ====
- === CSRF BREACH Protection
- If the steps for <<Defer Loading CsrfToken>> work for you, then you can also opt into Spring Security 6's default support for BREACH protection of the `CsrfToken` using the following configuration:
- .`CsrfToken` BREACH Protection
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
- XorCsrfTokenRequestAttributeHandler requestHandler = new XorCsrfTokenRequestAttributeHandler();
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName("_csrf");
- http
- // ...
- .csrf((csrf) -> csrf
- .csrfTokenRequestHandler(requestHandler)
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- open fun springSecurity(http: HttpSecurity): SecurityFilterChain {
- val requestHandler = XorCsrfTokenRequestAttributeHandler()
- // set the name of the attribute the CsrfToken will be populated on
- requestHandler.setCsrfRequestAttributeName("_csrf")
- http {
- csrf {
- csrfTokenRequestHandler = requestHandler
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <!-- ... -->
- <csrf request-handler-ref="requestHandler"/>
- </http>
- <b:bean id="requestHandler"
- class="org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler"
- p:csrfRequestAttributeName="_csrf"/>
- ----
- ====
- === Explicit Save SecurityContextRepository
- In Spring Security 5, the default behavior is for the xref:servlet/authentication/architecture.adoc#servlet-authentication-securitycontext[`SecurityContext`] to automatically be saved to the xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] using the xref:servlet/authentication/persistence.adoc#securitycontextpersistencefilter[`SecurityContextPersistenceFilter`].
- Saving must be done just prior to the `HttpServletResponse` being committed and just before `SecurityContextPersistenceFilter`.
- Unfortunately, automatic persistence of the `SecurityContext` can surprise users when it is done prior to the request completing (i.e. just prior to committing the `HttpServletResponse`).
- It also is complex to keep track of the state to determine if a save is necessary causing unnecessary writes to the `SecurityContextRepository` (i.e. `HttpSession`) at times.
- In Spring Security 6, the default behavior is that the xref:servlet/authentication/persistence.adoc#securitycontextholderfilter[`SecurityContextHolderFilter`] will only read the `SecurityContext` from `SecurityContextRepository` and populate it in the `SecurityContextHolder`.
- Users now must explicitly save the `SecurityContext` with the `SecurityContextRepository` if they want the `SecurityContext` to persist between requests.
- This removes ambiguity and improves performance by only requiring writing to the `SecurityContextRepository` (i.e. `HttpSession`) when it is necessary.
- To opt into the new Spring Security 6 default, the following configuration can be used.
- include::partial$servlet/architecture/security-context-explicit.adoc[]
- === Multiple SecurityContextRepository
- In Spring Security 5, the default xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] is `HttpSessionSecurityContextRepository`.
- In Spring Security 6, the default `SecurityContextRepository` is `DelegatingSecurityContextRepository`.
- To opt into the new Spring Security 6 default, the following configuration can be used.
- .Configure SecurityContextRepository with 6.0 defaults
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- // ...
- .securityContext((securityContext) -> securityContext
- .securityContextRepository(new DelegatingSecurityContextRepository(
- new RequestAttributeSecurityContextRepository(),
- new HttpSessionSecurityContextRepository()
- ))
- );
- return http.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- // ...
- securityContext {
- securityContextRepository = DelegatingSecurityContextRepository(
- RequestAttributeSecurityContextRepository(),
- HttpSessionSecurityContextRepository()
- )
- }
- }
- return http.build()
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http security-context-repository-ref="contextRepository">
- <!-- ... -->
- </http>
- <bean name="contextRepository"
- class="org.springframework.security.web.context.DelegatingSecurityContextRepository">
- <constructor-arg>
- <bean class="org.springframework.security.web.context.RequestAttributeSecurityContextRepository" />
- </constructor-arg>
- <constructor-arg>
- <bean class="org.springframework.security.web.context.HttpSessionSecurityContextRepository" />
- </constructor-arg>
- </bean>
- ----
- ====
- [IMPORTANT]
- ====
- If you are already using an implementation other than `HttpSessionSecurityContextRepository`, you should replace it with your chosen implementation in the example above to ensure that it is used along with `RequestAttributeSecurityContextRepository`.
- ====
- === Deprecation in SecurityContextRepository
- In Spring Security 5.7, a new method was added to xref:servlet/authentication/persistence.adoc#securitycontextrepository[`SecurityContextRepository`] with the signature:
- Supplier<SecurityContext> loadContext(HttpServletRequest request)
- With the addition of xref:servlet/authentication/persistence.adoc#delegatingsecuritycontextrepository[`DelegatingSecurityContextRepository`] in Spring Security 5.8, that method was deprecated in favor of a new method with the signature:
- DeferredSecurityContext loadDeferredContext(HttpServletRequest request)
- In Spring Security 6, the deprecated method was removed.
- If you have implemented `SecurityContextRepository` yourself and added an implementation of the `loadContext(request)` method, you can prepare for Spring Security 6 by removing the implementation of that method and implementing the new method instead.
- To get started implementing the new method, use the following example to provide a `DeferredSecurityContext`:
- .Provide `DeferredSecurityContext`
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Override
- public DeferredSecurityContext loadDeferredContext(HttpServletRequest request) {
- return new DeferredSecurityContext() {
- private SecurityContext securityContext;
- private boolean isGenerated;
- @Override
- public SecurityContext get() {
- if (this.securityContext == null) {
- this.securityContext = getContextOrNull(request);
- if (this.securityContext == null) {
- SecurityContextHolderStrategy strategy = SecurityContextHolder.getContextHolderStrategy();
- this.securityContext = strategy.createEmptyContext();
- this.isGenerated = true;
- }
- }
- return this.securityContext;
- }
- @Override
- public boolean isGenerated() {
- get();
- return this.isGenerated;
- }
- };
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- override fun loadDeferredContext(request: HttpServletRequest): DeferredSecurityContext {
- return object : DeferredSecurityContext {
- private var securityContext: SecurityContext? = null
- private var isGenerated = false
- override fun get(): SecurityContext {
- if (securityContext == null) {
- securityContext = getContextOrNull(request)
- ?: SecurityContextHolder.getContextHolderStrategy().createEmptyContext()
- .also { isGenerated = true }
- }
- return securityContext!!
- }
- override fun isGenerated(): Boolean {
- get()
- return isGenerated
- }
- }
- }
- ----
- ====
- [[requestcache-query-optimization]]
- === Optimize Querying of `RequestCache`
- In Spring Security 5, the default behavior is to query the xref:servlet/architecture.adoc#savedrequests[saved request] on every request.
- This means that in a typical setup, that in order to use the xref:servlet/architecture.adoc#requestcache[`RequestCache`] the `HttpSession` is queried on every request.
- In Spring Security 6, the default is that `RequestCache` will only be queried for a cached request if the HTTP parameter `continue` is defined.
- This allows Spring Security to avoid unnecessarily reading the `HttpSession` with the `RequestCache`.
- In Spring Security 5 the default is to use `HttpSessionRequestCache` which will be queried for a cached request on every request.
- If you are not overriding the defaults (i.e. using `NullRequestCache`), then the following configuration can be used to explicitly opt into the Spring Security 6 behavior in Spring Security 5.8:
- include::partial$servlet/architecture/request-cache-continue.adoc[]
- === Use `AuthorizationManager` for Method Security
- xref:servlet/authorization/method-security.adoc[Method Security] has been xref:servlet/authorization/method-security.adoc#jc-enable-method-security[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
- Should you run into trouble with making these changes, note that `@EnableGlobalMethodSecurity`, while deprecated, will not be removed in 6.0, allowing you to opt out by sticking with the old annotation.
- [[servlet-replace-globalmethodsecurity-with-methodsecurity]]
- ==== Replace xref:servlet/authorization/method-security.adoc#jc-enable-global-method-security[global method security] with xref:servlet/authorization/method-security.adoc#jc-enable-method-security[method security]
- {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableGlobalMethodSecurity.html[`@EnableGlobalMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-global-method-security[`<global-method-security>`] are deprecated in favor of {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`] and xref:servlet/appendix/namespace/method-security.adoc#nsa-method-security[`<method-security>`], respectively.
- The new annotation and XML element activate Spring's xref:servlet/authorization/method-security.adoc#jc-enable-method-security[pre-post annotations] by default and use `AuthorizationManager` internally.
- This means that the following two listings are functionally equivalent:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <global-method-security pre-post-enabled="true"/>
- ----
- ====
- and:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableMethodSecurity
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableMethodSecurity
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <method-security/>
- ----
- ====
- For applications not using the pre-post annotations, make sure to turn it off to avoid activating unwanted behavior.
- For example, a listing like:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableGlobalMethodSecurity(securedEnabled = true)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableGlobalMethodSecurity(securedEnabled = true)
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <global-method-security secured-enabled="true"/>
- ----
- ====
- should change to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableMethodSecurity(securedEnabled = true, prePostEnabled = false)
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <method-security secured-enabled="true" pre-post-enabled="false"/>
- ----
- ====
- [[servlet-replace-permissionevaluator-bean-with-methodsecurityexpression-handler]]
- ==== Publish a `MethodSecurityExpressionHandler` instead of a `PermissionEvaluator`
- `@EnableMethodSecurity` does not pick up a `PermissionEvaluator`.
- This helps keep its API simple.
- If you have a custom {security-api-url}org/springframework/security/access/PermissionEvaluator.html[`PermissionEvaluator`] `@Bean`, please change it from:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- static PermissionEvaluator permissionEvaluator() {
- // ... your evaluator
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- companion object {
- @Bean
- fun permissionEvaluator(): PermissionEvaluator {
- // ... your evaluator
- }
- }
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- static MethodSecurityExpressionHandler expressionHandler() {
- var expressionHandler = new DefaultMethodSecurityExpressionHandler();
- expressionHandler.setPermissionEvaluator(myPermissionEvaluator);
- return expressionHandler;
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- companion object {
- @Bean
- fun expressionHandler(): MethodSecurityExpressionHandler {
- val expressionHandler = DefaultMethodSecurityExpressionHandler
- expressionHandler.setPermissionEvaluator(myPermissionEvaluator)
- return expressionHandler
- }
- }
- ----
- ====
- ==== Replace any custom method-security ``AccessDecisionManager``s
- Your application may have a custom {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] or {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] arrangement.
- The preparation strategy will depend on your reason for each arrangement.
- Read on to find the best match for your situation.
- ===== I use `UnanimousBased`
- If your application uses {security-api-url}org/springframework/security/access/vote/UnanimousBased.html[`UnanimousBased`] with the default voters, you likely need do nothing since unanimous-based is the default behavior with {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableMethodSecurity.html[`@EnableMethodSecurity`].
- However, if you do discover that you cannot accept the default authorization managers, you can use `AuthorizationManagers.allOf` to compose your own arrangement.
- Having done that, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
- ===== I use `AffirmativeBased`
- If your application uses {security-api-url}org/springframework/security/access/vote/AffirmativeBased.html[`AffirmativeBased`], then you can construct an equivalent {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`], like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- AuthorizationManager<MethodInvocation> authorization = AuthorizationManagers.anyOf(
- // ... your list of authorization managers
- )
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val authorization = AuthorizationManagers.anyOf(
- // ... your list of authorization managers
- )
- ----
- ====
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
- ===== I use `ConsensusBased`
- There is no framework-provided equivalent for {security-api-url}org/springframework/security/access/vote/ConsensusBased.html[`ConsensusBased`].
- In that case, please implement a composite {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] that takes the set of delegate ``AuthorizationManager``s into account.
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
- ===== I use a custom `AccessDecisionVoter`
- You should either change the class to implement {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] or create an adapter.
- Without knowing what your custom voter is doing, it is impossible to recommend a general-purpose solution.
- By way of example, though, here is what adapting {security-api-url}org/springframework/security/access/SecurityMetadataSource.html[`SecurityMetadataSource`] and {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] for `@PreAuthorize` would look like:
- ====
- .Java
- [source,java,role="primary"]
- ----
- public final class PreAuthorizeAuthorizationManagerAdapter implements AuthorizationManager<MethodInvocation> {
- private final SecurityMetadataSource metadata;
- private final AccessDecisionVoter voter;
- public PreAuthorizeAuthorizationManagerAdapter(MethodSecurityExpressionHandler expressionHandler) {
- ExpressionBasedAnnotationAttributeFactory attributeFactory =
- new ExpressionBasedAnnotationAttributeFactory(expressionHandler);
- this.metadata = new PrePostAnnotationSecurityMetadataSource(attributeFactory);
- ExpressionBasedPreInvocationAdvice expressionAdvice = new ExpressionBasedPreInvocationAdvice();
- expressionAdvice.setExpressionHandler(expressionHandler);
- this.voter = new PreInvocationAuthorizationAdviceVoter(expressionAdvice);
- }
- public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation invocation) {
- List<ConfigAttribute> attributes = this.metadata.getAttributes(invocation, AopUtils.getTargetClass(invocation.getThis()));
- int decision = this.voter.vote(authentication.get(), invocation, attributes);
- if (decision == ACCESS_GRANTED) {
- return new AuthorizationDecision(true);
- }
- if (decision == ACCESS_DENIED) {
- return new AuthorizationDecision(false);
- }
- return null; // abstain
- }
- }
- ----
- ====
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
- ===== I use a custom `AfterInvocationManager`
- {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] replaces both {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] and {security-api-url}org/springframework/security/access/intercept/AfterInvocationManager.html[`AfterInvocationManager`].
- The difference is that `AuthorizationManager<MethodInvocation>` replaces `AccessDecisionManager` and `AuthorizationManager<MethodInvocationResult>` replaces `AfterInvocationManager`.
- Given that, <<_i_use_a_custom_accessdecisionvoter,the same rules apply for adaptation>>, where the goal this time is to implement `AuthorizationManager<MethodInvocationResult>` instead of `AuthorizationManager<MethodInvocation>` and use `AuthorizationManagerAfterMethodInterceptor` instead of `AuthorizationManagerBeforeMethodInterceptor`.
- ===== I use `RunAsManager`
- There is currently https://github.com/spring-projects/spring-security/issues/11331[no replacement for `RunAsManager`] though one is being considered.
- It is quite straightforward to adapt a `RunAsManager`, though, to the `AuthorizationManager` API, if needed.
- Here is some pseudocode to get you started:
- ====
- .Java
- [source,java,role="primary"]
- ----
- public final class RunAsAuthorizationManagerAdapter<T> implements AuthorizationManager<T> {
- private final RunAsManager runAs = new RunAsManagerImpl();
- private final SecurityMetadataSource metadata;
- private final AuthorizationManager<T> authorization;
- // ... constructor
- public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
- Supplier<Authentication> wrapped = (auth) -> {
- List<ConfigAttribute> attributes = this.metadata.getAttributes(object);
- return this.runAs.buildRunAs(auth, object, attributes);
- };
- return this.authorization.check(wrapped, object);
- }
- }
- ----
- ====
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/method-security.adoc#jc-method-security-custom-authorization-manager[adding a custom `AuthorizationManager`].
- [[servlet-check-for-annotationconfigurationexceptions]]
- ==== Check for ``AnnotationConfigurationException``s
- `@EnableMethodSecurity` and `<method-security>` activate stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
- If after moving to either you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
- === Use `AuthorizationManager` for Message Security
- xref:servlet/integrations/websocket.adoc[Message Security] has been xref:servlet/integrations/websocket.adoc#websocket-configuration[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
- Should you run into trouble with making these changes, you can follow the <<servlet-authorizationmanager-messages-opt-out,opt out steps>> at the end of this section.
- ==== Ensure all messages have defined authorization rules
- The now-deprecated {security-api-url}org/springframework/security/config/annotation/web/socket/AbstractSecurityWebSocketMessageBrokerConfigurer.html[message security support] permits all messages by default.
- xref:servlet/integrations/websocket.adoc[The new support] has the stronger default of denying all messages.
- To prepare for this, ensure that authorization rules exist are declared for every request.
- For example, an application configuration like:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Override
- protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
- messages
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN");
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
- messages
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker>
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- </websocket-message-broker>
- ----
- ====
- should change to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Override
- protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll()
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker>
- <intercept-message type="CONNECT" access="permitAll"/>
- <intercept-message type="DISCONNECT" access="permitAll"/>
- <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- <intercept-message pattern="/**" access="denyAll"/>
- </websocket-message-broker>
- ----
- ====
- ==== Add `@EnableWebSocketSecurity`
- [NOTE]
- ====
- If you want to have CSRF disabled and you are using Java configuration, the migration steps are slightly different.
- Instead of using `@EnableWebSocketSecurity`, you will override the appropriate methods in `WebSocketMessageBrokerConfigurer` yourself.
- Please see xref:servlet/integrations/websocket.adoc#websocket-sameorigin-disable[the reference manual] for details about this step.
- ====
- If you are using Java Configuration, add {security-api-url}org/springframework/security/config/annotation/web/socket/EnableWebSocketSecurity.html[`@EnableWebSocketSecurity`] to your application.
- For example, you can add it to your websocket security configuration class, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
- // ...
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
- // ...
- }
- ----
- ====
- This will make a prototype instance of `MessageMatcherDelegatingAuthorizationManager.Builder` available to encourage configuration by composition instead of extension.
- ==== Use an `AuthorizationManager<Message<?>>` instance
- To start using `AuthorizationManager`, you can set the `use-authorization-manager` attribute in XML or you can publish an `AuthorizationManager<Message<?>>` `@Bean` in Java.
- For example, the following application configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Override
- protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- override fun configureInbound(messages: MessageSecurityMetadataSourceRegistry) {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll()
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker>
- <intercept-message type="CONNECT" access="permitAll"/>
- <intercept-message type="DISCONNECT" access="permitAll"/>
- <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- <intercept-message pattern="/**" access="denyAll"/>
- </websocket-message-broker>
- ----
- ====
- changes to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll();
- return messages.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
- messages
- .simpTypeMatchers(CONNECT, DISCONNECT, UNSUBSCRIBE).permitAll()
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- .anyMessage().denyAll()
- return messages.build()
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker use-authorization-manager="true">
- <intercept-message type="CONNECT" access="permitAll"/>
- <intercept-message type="DISCONNECT" access="permitAll"/>
- <intercept-message type="UNSUBSCRIBE" access="permitAll"/>
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- <intercept-message pattern="/**" access="denyAll"/>
- </websocket-message-broker>
- ----
- ====
- ==== Stop Implementing `AbstractSecurityWebSocketMessageBrokerConfigurer`
- If you are using Java configuration, you can now simply extend `WebSocketMessageBrokerConfigurer`.
- For example, if your class that extends `AbstractSecurityWebSocketMessageBrokerConfigurer` is called `WebSocketSecurityConfig`, then:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
- // ...
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- class WebSocketSecurityConfig: AbstractSecurityWebSocketMessageBrokerConfigurer() {
- // ...
- }
- ----
- ====
- changes to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- public class WebSocketSecurityConfig implements WebSocketMessageBrokerConfigurer {
- // ...
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableWebSocketSecurity
- @Configuration
- class WebSocketSecurityConfig: WebSocketMessageBrokerConfigurer {
- // ...
- }
- ----
- ====
- [[servlet-authorizationmanager-messages-opt-out]]
- ==== Opt-out Steps
- In case you had trouble, take a look at these scenarios for optimal opt out behavior:
- ===== I cannot declare an authorization rule for all requests
- If you are having trouble setting an `anyRequest` authorization rule of `denyAll`, please use {security-api-url}org/springframework/security/messaging/access/intercept/MessageMatcherDelegatingAuthorizationManager.Builder.Constraint.html#permitAll()[`permitAll`] instead, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- AuthorizationManager<Message<?>> messageSecurity(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
- messages
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- // ...
- .anyMessage().permitAll();
- return messages.build();
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun messageSecurity(val messages: MessageMatcherDelegatingAuthorizationManager.Builder): AuthorizationManager<Message<?>> {
- messages
- .simpDestMatchers("/user/queue/errors").permitAll()
- .simpDestMatchers("/admin/**").hasRole("ADMIN")
- // ...
- .anyMessage().permitAll();
- return messages.build()
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker use-authorization-manager="true">
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- <!-- ... -->
- <intercept-message pattern="/**" access="permitAll"/>
- </websocket-message-broker>
- ----
- ====
- ===== I cannot get CSRF working, need some other `AbstractSecurityWebSocketMessageBrokerConfigurer` feature, or am having trouble with `AuthorizationManager`
- In the case of Java, you may continue using `AbstractMessageSecurityWebSocketMessageBrokerConfigurer`.
- Even though it is deprecated, it will not be removed in 6.0.
- In the case of XML, you can opt out of `AuthorizationManager` by setting `use-authorization-manager="false"`:
- ====
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker>
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- </websocket-message-broker>
- ----
- ====
- to:
- ====
- .Xml
- [source,xml,role="secondary"]
- ----
- <websocket-message-broker use-authorization-manager="false">
- <intercept-message pattern="/user/queue/errors" access="permitAll"/>
- <intercept-message pattern="/admin/**" access="hasRole('ADMIN')"/>
- </websocket-message-broker>
- ----
- ====
- === Use `AuthorizationManager` for Request Security
- xref:servlet/authorization/authorize-requests.adoc[HTTP Request Security] has been xref:servlet/authorization/authorize-http-requests.adoc[simplified] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API].
- Should you run into trouble with making these changes, you can follow the <<servlet-authorizationmanager-requests-opt-out,opt out steps>> at the end of this section.
- ==== Ensure that all requests have defined authorization rules
- In Spring Security 5.8 and earlier, requests with no authorization rule are permitted by default.
- It is a stronger security position to deny by default, thus requiring that authorization rules be clearly defined for every endpoint.
- As such, in 6.0, Spring Security by default denies any request that is missing an authorization rule.
- The simplest way to prepare for this change is to introduce an appropriate {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#anyRequest()[`anyRequest`] rule as the last authorization rule.
- The recommendation is {security-api-url}org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.AuthorizedUrl.html#denyAll()[`denyAll`] since that is the implied 6.0 default.
- [NOTE]
- ====
- You may already have an `anyRequest` rule defined that you are happy with in which case this step can be skipped.
- ====
- Adding `denyAll` to the end looks like changing:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/app/**", hasRole("APP"))
- // ...
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http once-per-request="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- </http>
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http once-per-request="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- ----
- ====
- If you have already migrated to `authorizeHttpRequests`, the recommended change is the same.
- ==== Switch to `AuthorizationManager`
- To opt in to using `AuthorizationManager`, you can use `authorizeHttpRequests` or xref:servlet/appendix/namespace/http.adoc#nsa-http-use-authorization-manager[`use-authorization-manager`] for Java or XML, respectively.
- Change:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http once-per-request="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .shouldFilterAllDispatcherTypes(false)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- shouldFilterAllDispatcherTypes = false
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http filter-all-dispatcher-types="false" use-authorization-manager="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- ----
- ====
- ==== Migrate SpEL expressions to `AuthorizationManager`
- For authorization rules, Java tends to be easier to test and maintain than SpEL.
- As such, `authorizeHttpRequests` does not have a method for declaring a `String` SpEL.
- Instead, you can implement your own `AuthorizationManager` implementation or use `WebExpressionAuthorizationManager`.
- For completeness, both options will be demonstrated.
- First, if you have the following SpEL:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/complicated/**").access("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/complicated/**", access("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- ====
- Then you can compose your own `AuthorizationManager` with Spring Security authorization primitives like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .shouldFilterAllDispatcherTypes(false)
- .mvcMatchers("/complicated/**").access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- shouldFilterAllDispatcherTypes = false
- authorize("/complicated/**", access(anyOf(hasRole("ADMIN"), hasAuthority("SCOPE_read"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- ====
- Or you can use `WebExpressionAuthorizationManager` in the following way:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/complicated/**").access(
- new WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')")
- )
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/complicated/**", access(
- WebExpressionAuthorizationManager("hasRole('ADMIN') || hasAuthority('SCOPE_read')"))
- )
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- ====
- ==== Switch to filter all dispatcher types
- Spring Security 5.8 and earlier only xref:servlet/authorization/architecture.adoc[perform authorization] once per request.
- This means that dispatcher types like `FORWARD` and `INCLUDE` that run after `REQUEST` are not secured by default.
- It's recommended that Spring Security secure all dispatch types.
- As such, in 6.0, Spring Security changes this default.
- So, finally, change your authorization rules to filter all dispatcher types.
- To do this, change:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .shouldFilterAllDispatcherTypes(false)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- shouldFilterAllDispatcherTypes = false
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http filter-all-dispatcher-types="false" use-authorization-manager="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .shouldFilterAllDispatcherTypes(true)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- shouldFilterAllDispatcherTypes = true
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http filter-all-dispatcher-types="true" use-authorization-manager="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- ----
- ====
- ==== Replace any custom filter-security ``AccessDecisionManager``s
- Your application may have a custom {security-api-url}org/springframework/security/access/AccessDecisionManager.html[`AccessDecisionManager`] or {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] arrangement.
- The preparation strategy will depend on your reason for each arrangement.
- Read on to find the best match for your situation.
- ===== I use `UnanimousBased`
- If your application uses {security-api-url}org/springframework/security/access/vote/UnanimousBased.html[`UnanimousBased`], you should first adapt or replace any ``AccessDecisionVoter``s and then you can construct an `AuthorizationManager` like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- AuthorizationManager<RequestAuthorizationContext> requestAuthorization() {
- PolicyAuthorizationManager policy = ...;
- LocalAuthorizationManager local = ...;
- return AuthorizationMangers.allOf(policy, local);
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun requestAuthorization(): AuthorizationManager<RequestAuthorizationContext> {
- val policy: PolicyAuthorizationManager = ...
- val local: LocalAuthorizationManager = ...
- return AuthorizationMangers.allOf(policy, local)
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <bean id="requestAuthorization" class="org.springframework.security.authorization.AuthorizationManagers"
- factory-method="allOf">
- <constructor-arg>
- <util:list>
- <bean class="my.PolicyAuthorizationManager"/>
- <bean class="my.LocalAuthorizationManager"/>
- </util:list>
- </constructor-arg>
- </bean>
- ----
- ====
- then, wire it into the DSL like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize.anyRequest().access(requestAuthorization))
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- authorize(anyRequest, requestAuthorization)
- }
- // ...
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http authorization-manager-ref="requestAuthorization"/>
- ----
- ====
- [NOTE]
- ====
- `authorizeHttpRequests` is designed so that you can apply a custom `AuthorizationManager` to any url pattern.
- See xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[the reference] for more details.
- ====
- ===== I use `AffirmativeBased`
- If your application uses {security-api-url}org/springframework/security/access/vote/AffirmativeBased.html[`AffirmativeBased`], then you can construct an equivalent {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`], like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- AuthorizationManager<RequestAuthorizationContext> requestAuthorization() {
- PolicyAuthorizationManager policy = ...;
- LocalAuthorizationManager local = ...;
- return AuthorizationMangers.anyOf(policy, local);
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun requestAuthorization(): AuthorizationManager<RequestAuthorizationContext> {
- val policy: PolicyAuthorizationManager = ...
- val local: LocalAuthorizationManager = ...
- return AuthorizationMangers.anyOf(policy, local)
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <bean id="requestAuthorization" class="org.springframework.security.authorization.AuthorizationManagers"
- factory-method="anyOf">
- <constructor-arg>
- <util:list>
- <bean class="my.PolicyAuthorizationManager"/>
- <bean class="my.LocalAuthorizationManager"/>
- </util:list>
- </constructor-arg>
- </bean>
- ----
- ====
- then, wire it into the DSL like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize.anyRequest().access(requestAuthorization))
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- authorize(anyRequest, requestAuthorization)
- }
- // ...
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http authorization-manager-ref="requestAuthorization"/>
- ----
- ====
- [NOTE]
- ====
- `authorizeHttpRequests` is designed so that you can apply a custom `AuthorizationManager` to any url pattern.
- See xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[the reference] for more details.
- ====
- ===== I use `ConsensusBased`
- There is no framework-provided equivalent for {security-api-url}org/springframework/security/access/vote/ConsensusBased.html[`ConsensusBased`].
- In that case, please implement a composite {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] that takes the set of delegate ``AuthorizationManager``s into account.
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[adding a custom `AuthorizationManager`].
- ===== I use a custom `AccessDecisionVoter`
- You should either change the class to implement {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[`AuthorizationManager`] or create an adapter.
- Without knowing what your custom voter is doing, it is impossible to recommend a general-purpose solution.
- By way of example, though, here is what adapting {security-api-url}org/springframework/security/access/SecurityMetadataSource.html[`SecurityMetadataSource`] and {security-api-url}org/springframework/security/access/AccessDecisionVoter.html[`AccessDecisionVoter`] for `anyRequest().authenticated()` would look like:
- ====
- .Java
- [source,java,role="primary"]
- ----
- public final class AnyRequestAuthenticatedAuthorizationManagerAdapter implements AuthorizationManager<RequestAuthorizationContext> {
- private final SecurityMetadataSource metadata;
- private final AccessDecisionVoter voter;
- public PreAuthorizeAuthorizationManagerAdapter(SecurityExpressionHandler expressionHandler) {
- Map<RequestMatcher, List<ConfigAttribute>> requestMap = Collections.singletonMap(
- AnyRequestMatcher.INSTANCE, Collections.singletonList(new SecurityConfig("authenticated")));
- this.metadata = new DefaultFilterInvocationSecurityMetadataSource(requestMap);
- WebExpressionVoter voter = new WebExpressionVoter();
- voter.setExpressionHandler(expressionHandler);
- this.voter = voter;
- }
- public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext context) {
- List<ConfigAttribute> attributes = this.metadata.getAttributes(context);
- int decision = this.voter.vote(authentication.get(), invocation, attributes);
- if (decision == ACCESS_GRANTED) {
- return new AuthorizationDecision(true);
- }
- if (decision == ACCESS_DENIED) {
- return new AuthorizationDecision(false);
- }
- return null; // abstain
- }
- }
- ----
- ====
- Once you have implemented `AuthorizationManager`, please follow the details in the reference manual for xref:servlet/authorization/authorize-http-requests.adoc#custom-authorization-manager[adding a custom `AuthorizationManager`].
- [[servlet-authorizationmanager-requests-opt-out]]
- ==== Opt-out Steps
- In case you had trouble, take a look at these scenarios for optimal opt out behavior:
- ===== I cannot secure all dispatcher types
- If you cannot secure all dispatcher types, first try and declare which dispatcher types should not require authorization like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .shouldFilterAllDispatcherTypes(true)
- .dispatcherTypeMatchers(FORWARD, INCLUDE).permitAll()
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- .anyRequest().denyAll()
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- shouldFilterAllDispatcherTypes = true
- authorize(DispatcherTypeRequestMatcher(FORWARD, INCLUDE), permitAll)
- authorize("/app/**", hasRole("APP"))
- // ...
- authorize(anyRequest, denyAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http filter-all-dispatcher-types="true" use-authorization-manager="true">
- <intercept-url request-matcher-ref="dispatchers"/>
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="denyAll"/>
- </http>
- <bean id="dispatchers" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
- <constructor-arg>
- <util:list value-type="javax.servlet.DispatcherType">
- <value>FORWARD</value>
- <value>INCLUDE</value>
- </util:list>
- </constructor-arg>
- </bean>
- ----
- ====
- Or, if that doesn't work, then you can explicitly opt out of the behavior by setting `filter-all-dispatcher-types` and `filterAllDispatcherTypes` to `false`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpRequests((authorize) -> authorize
- .filterAllDispatcherTypes(false)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- filterAllDispatcherTypes = false
- authorize("/messages/**", hasRole("APP"))
- // ...
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http filter-all-dispatcher-types="false" use-authorization-manager="true">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- </http>
- ----
- ====
- or, if you are still using `authorizeRequests` or `use-authorization-manager="false"`, set `oncePerRequest` to `true`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeRequests((authorize) -> authorize
- .filterSecurityInterceptorOncePerRequest(true)
- .mvcMatchers("/app/**").hasRole("APP")
- // ...
- )
- // ...
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeRequests {
- filterSecurityInterceptorOncePerRequest = true
- authorize("/messages/**", hasRole("APP"))
- // ...
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http once-per-request="true" use-authorization-manager="false">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- </http>
- ----
- ====
- ===== I cannot declare an authorization rule for all requests
- If you are having trouble setting an `anyRequest` authorization rule of `denyAll`, please use {security-api-url}org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.AuthorizedUrl.html#permitAll()[`permitAll`] instead, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .authorizeHttpReqeusts((authorize) -> authorize
- .mvcMatchers("/app/*").hasRole("APP")
- // ...
- .anyRequest().permitAll()
- )
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- authorizeHttpRequests {
- authorize("/app*", hasRole("APP"))
- // ...
- authorize(anyRequest, permitAll)
- }
- }
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <http>
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- <intercept-url pattern="/**" access="permitAll"/>
- </http>
- ----
- ====
- ===== I cannot migrate my SpEL or my `AccessDecisionManager`
- If you are having trouble with SpEL, `AccessDecisionManager`, or there is some other feature that you are needing to keep using in `<http>` or `authorizeRequests`, try the following.
- First, if you still need `authorizeRequests`, you are welcome to keep using it. Even though it is deprecated, it is not removed in 6.0.
- Second, if you still need your custom `access-decision-manager-ref` or have some other reason to opt out of `AuthorizationManager`, do:
- ====
- .Xml
- [source,xml,role="secondary"]
- ----
- <http use-authorization-manager="false">
- <intercept-url pattern="/app/*" access="hasRole('APP')"/>
- <!-- ... -->
- </http>
- ----
- ====
- === Propagate ``AuthenticationServiceException``s
- {security-api-url}org/springframework/security/web/authentication/AuthenticationFilter.html[`AuthenticationFilter`] propagates {security-api-url}org/springframework/security/authentication/AuthenticationServiceException.html[``AuthenticationServiceException``]s to the {security-api-url}org/springframework/security/authentication/AuthenticationEntryPoint.html[`AuthenticationEntryPoint`].
- Because ``AuthenticationServiceException``s represent a server-side error instead of a client-side error, in 6.0, this changes to propagate them to the container.
- ==== Configure `AuthenticationFailureHandler` to rethrow ``AuthenticationServiceException``s
- To prepare for the 6.0 default, wire `AuthenticationFilter` instances with a `AuthenticationFailureHandler` that rethrows ``AuthenticationServiceException``s, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
- AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
- handler.setRethrowAuthenticationServiceException(true);
- authenticationFilter.setAuthenticationFailureHandler(handler);
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
- val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
- handler.setRethrowAuthenticationServiceException(true)
- authenticationFilter.setAuthenticationFailureHandler(handler)
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
- <!-- ... -->
- <property ref="authenticationFailureHandler"/>
- </bean>
- <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
- <property name="rethrowAuthenticationServiceException" value="true"/>
- </bean>
- ----
- ====
- [[servlet-authenticationfailurehandler-opt-out]]
- ==== Opt-out Steps
- If rethrowing ``AuthenticationServiceException``s gives you trouble, you can set the value to false instead of taking the 6.0 default, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- AuthenticationFilter authenticationFilter = new AuthenticationFilter(...);
- AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...);
- handler.setRethrowAuthenticationServiceException(false);
- authenticationFilter.setAuthenticationFailureHandler(handler);
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val authenticationFilter: AuthenticationFilter = new AuthenticationFilter(...)
- val handler: AuthenticationEntryPointFailureHandler = new AuthenticationEntryPointFailureHandler(...)
- handler.setRethrowAuthenticationServiceException(false)
- authenticationFilter.setAuthenticationFailureHandler(handler)
- ----
- .Xml
- [source,xml,role="secondary"]
- ----
- <bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter">
- <!-- ... -->
- <property ref="authenticationFailureHandler"/>
- </bean>
- <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler">
- <property name="rethrowAuthenticationServiceException" value="false"/>
- </bean>
- ----
- ====
- [[servlet-opt-in-sha256-rememberme]]
- === Use SHA-256 in Remember Me
- The `TokenBasedRememberMeServices` implementation now supports SHA-256 for the Remember Me token and this is the default in Spring Security 6.
- This change makes the implementation more secure by default since MD5 is already proven to be a weak hashing algorithm and vulnerable against collision attacks and modular differential attacks.
- The new generated tokens now have the information of which algorithm was used to generate the token and that information is used in order to match it.
- If the algorithm name is not present, then the `matchingAlgorithm` property is used to check the token.
- This allows for a smooth transition from MD5 to SHA-256.
- To opt into the new Spring Security 6 default to encode the tokens while still being able to decode tokens encoded with MD5, you can set the `encodingAlgorithm` property to SHA-256 and the `matchingAlgorithm` property to MD5.
- See the xref:servlet/authentication/rememberme.adoc#_tokenbasedremembermeservices[reference documentation] and the {security-api-url}org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServices.html[API docs] for more information.
- [[servlet-opt-in-sha256-sha256-encoding]]
- .Use Spring Security 6 defaults for encoding, SHA-256 for encoding and MD5 for matching
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
- http
- // ...
- .rememberMe((remember) -> remember
- .rememberMeServices(rememberMeServices)
- );
- return http.build();
- }
- @Bean
- RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
- RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
- TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
- rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
- return rememberMe;
- }
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <remember-me services-ref="rememberMeServices"/>
- </http>
- <bean id="rememberMeServices" class=
- "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
- <property name="userDetailsService" ref="myUserDetailsService"/>
- <property name="key" value="springRocks"/>
- <property name="matchingAlgorithm" value="MD5"/>
- <property name="encodingAlgorithm" value="SHA256"/>
- </bean>
- ----
- ====
- At some point, you will want to fully migrate to Spring Security 6 defaults. But how do you know when it is safe to do so?
- Let's suppose that you deployed your application using SHA-256 as the encoding algorithm (as you have done <<servlet-opt-in-sha256-sha256-encoding,here>>) on November 1st, if you have the value for the `tokenValiditySeconds` property set to N days (14 is the default), you can migrate to SHA-256 N days after November 1st (which is November 15th in this example).
- By that time, all the tokens generated with MD5 will have expired.
- .Use Spring Security 6 defaults, SHA-256 for both encoding and matching
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
- http
- // ...
- .rememberMe((remember) -> remember
- .rememberMeServices(rememberMeServices)
- );
- return http.build();
- }
- @Bean
- RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
- RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256;
- TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
- rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.SHA256);
- return rememberMe;
- }
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <remember-me services-ref="rememberMeServices"/>
- </http>
- <bean id="rememberMeServices" class=
- "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
- <property name="userDetailsService" ref="myUserDetailsService"/>
- <property name="key" value="springRocks"/>
- <property name="matchingAlgorithm" value="SHA256"/>
- <property name="encodingAlgorithm" value="SHA256"/>
- </bean>
- ----
- ====
- If you are having problems with the Spring Security 6 defaults, you can explicitly opt into 5.8 defaults using the following configuration:
- .Use MD5 for both encoding and matching algorithms
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {
- http
- // ...
- .rememberMe((remember) -> remember
- .rememberMeServices(rememberMeServices)
- );
- return http.build();
- }
- @Bean
- RememberMeServices rememberMeServices(UserDetailsService userDetailsService) {
- RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.MD5;
- TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm);
- rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.MD5);
- return rememberMe;
- }
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <remember-me services-ref="rememberMeServices"/>
- </http>
- <bean id="rememberMeServices" class=
- "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
- <property name="userDetailsService" ref="myUserDetailsService"/>
- <property name="key" value="springRocks"/>
- <property name="matchingAlgorithm" value="MD5"/>
- <property name="encodingAlgorithm" value="MD5"/>
- </bean>
- ----
- ====
- === Stop Using SAML 2.0 `Converter` constructors
- In an early release of Spring Security's SAML 2.0 support, `Saml2MetadataFilter` and `Saml2AuthenticationTokenConverter` shipped with constructors of type `Converter`.
- This level of abstraction made it tricky to evolve the class and so a dedicated interface `RelyingPartyRegistrationResolver` was introduced in a later release.
- In 6.0, the `Converter` constructors are removed.
- To prepare for this in 5.8, change classes that implement `Converter<HttpServletRequest, RelyingPartyRegistration>` to instead implement `RelyingPartyRegistrationResolver`.
- === Change to Using `Saml2AuthenticationRequestResolver`
- `Saml2AuthenticationContextResolver` and `Saml2AuthenticationRequestFactory` are removed in 6.0 as is the `Saml2WebSsoAuthenticationRequestFilter` that requires them.
- They are replaced by `Saml2AuthenticationRequestResolver` and a new constructor in `Saml2WebSsoAuthenticationRequestFilter`.
- The new interface removes an unnecessary transport object between the two classes.
- Most applications need do nothing; however, if you use or configure `Saml2AuthenticationRequestContextResolver` or `Saml2AuthenticationRequestFactory`, try the following steps to convert instead use `Saml2AuthenticationRequestResolver`.
- ==== Use `setAuthnRequestCustomizer` instead of `setAuthenticationRequestContextConverter`
- If you are calling `OpenSaml4AuthenticationReqeustFactory#setAuthenticationRequestContextConverter`, for example, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- Saml2AuthenticationRequestFactory authenticationRequestFactory() {
- OpenSaml4AuthenticationRequestFactory factory = new OpenSaml4AuthenticationRequestFactory();
- factory.setAuthenticationRequestContextConverter((context) -> {
- AuthnRequestBuilder authnRequestBuilder = ConfigurationService.get(XMLObjectProviderRegistry.class)
- .getBuilderFactory().getBuilder(AuthnRequest.DEFAULT_ELEMENT_NAME);
- IssuerBuilder issuerBuilder = ConfigurationService.get(XMLObjectProviderRegistry.class)
- .getBuilderFactory().getBuilder(Issuer.DEFAULT_ELEMENT_NAME);
- tring issuer = context.getIssuer();
- String destination = context.getDestination();
- String assertionConsumerServiceUrl = context.getAssertionConsumerServiceUrl();
- String protocolBinding = context.getRelyingPartyRegistration().getAssertionConsumerServiceBinding().getUrn();
- AuthnRequest auth = authnRequestBuilder.buildObject();
- auth.setID("ARQ" + UUID.randomUUID().toString().substring(1));
- auth.setIssueInstant(Instant.now());
- auth.setForceAuthn(Boolean.TRUE);
- auth.setIsPassive(Boolean.FALSE);
- auth.setProtocolBinding(SAMLConstants.SAML2_POST_BINDING_URI);
- Issuer iss = issuerBuilder.buildObject();
- iss.setValue(issuer);
- auth.setIssuer(iss);
- auth.setDestination(destination);
- auth.setAssertionConsumerServiceURL(assertionConsumerServiceUrl);
- });
- return factory;
- }
- ----
- ====
- to ensure that ForceAuthn is set to `true`, you can instead do:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- Saml2AuthenticationRequestResolver authenticationRequestResolver(RelyingPartyRegistrationResolver registrations) {
- OpenSaml4AuthenticationRequestResolver reaolver = new OpenSaml4AuthenticationRequestResolver(registrations);
- resolver.setAuthnRequestCustomizer((context) -> context.getAuthnRequest().setForceAuthn(Boolean.TRUE));
- return resolver;
- }
- ----
- ====
- Also, since `setAuthnRequestCustomizer` has direct access to the `HttpServletRequest`, there is no need for a `Saml2AuthenticationRequestContextResolver`.
- Simply use `setAuthnRequestCustomizer` to read directly from `HttpServletRequest` this information you need.
- ==== Use `setAuthnRequestCustomizer` instead of `setProtocolBinding`
- Instead of doing:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- Saml2AuthenticationRequestFactory authenticationRequestFactory() {
- OpenSaml4AuthenticationRequestFactory factory = new OpenSaml4AuthenticationRequestFactory();
- factory.setProtocolBinding("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST")
- return factory;
- }
- ----
- ====
- you can do:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- Saml2AuthenticationRequestResolver authenticationRequestResolver() {
- OpenSaml4AuthenticationRequestResolver reaolver = new OpenSaml4AuthenticationRequestResolver(registrations);
- resolver.setAuthnRequestCustomizer((context) -> context.getAuthnRequest()
- .setProtocolBinding("urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"));
- return resolver;
- }
- ----
- ====
- [NOTE]
- ====
- Since Spring Security only supports the `POST` binding for authentication, there is not very much value in overriding the protocol binding at this point in time.
- ====
- === Use the latest `Saml2AuthenticationToken` constructor
- In an early release, `Saml2AuthenticationToken` took several individual settings as constructor parameters.
- This created a challenge each time a new parameter needed to be added.
- Since most of these settings were part of `RelyingPartyRegistration`, a new constructor was added where a `RelyingPartyRegistration` could be provided, making the constructor more stable.
- It also is valuable in that it more closely aligns with the design of `OAuth2LoginAuthenticationToken`.
- Most applications do not construct this class directly since `Saml2WebSsoAuthenticationFilter` does.
- However, in the event that your application constructs one, please change from:
- ====
- .Java
- [source,java,role="primary"]
- ----
- new Saml2AuthenticationToken(saml2Response, registration.getSingleSignOnServiceLocation(),
- registration.getAssertingParty().getEntityId(), registration.getEntityId(), registration.getCredentials())
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- Saml2AuthenticationToken(saml2Response, registration.getSingleSignOnServiceLocation(),
- registration.getAssertingParty().getEntityId(), registration.getEntityId(), registration.getCredentials())
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- new Saml2AuthenticationToken(saml2Response, registration)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- Saml2AuthenticationToken(saml2Response, registration)
- ----
- ====
- === Use `RelyingPartyRegistration` updated methods
- In an early release of Spring Security's SAML support, there was some ambiguity on the meaning of certain `RelyingPartyRegistration` methods and their function.
- As more capabilities were added to `RelyingPartyRegistration`, it became necessary to clarify this ambiguity by changing method names to ones that aligned with spec language.
- The deprecated methods in `RelyingPartyRegstration` are removed.
- To prepare for that, consider the following representative usage of `RelyingPartyRegistration`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- String idpEntityId = registration.getRemoteIdpEntityId();
- String assertionConsumerServiceUrl = registration.getAssertionConsumerServiceUrlTemplate();
- String idpWebSsoUrl = registration.getIdpWebSsoUrl();
- String localEntityId = registration.getLocalEntityIdTemplate();
- List<Saml2X509Credential> verifying = registration.getCredentials().stream()
- .filter(Saml2X509Credential::isSignatureVerficationCredential)
- .collect(Collectors.toList());
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val idpEntityId: String = registration.getRemoteIdpEntityId()
- val assertionConsumerServiceUrl: String = registration.getAssertionConsumerServiceUrlTemplate()
- val idpWebSsoUrl: String = registration.getIdpWebSsoUrl()
- val localEntityId: String = registration.getLocalEntityIdTemplate()
- val verifying: List<Saml2X509Credential> = registration.getCredentials()
- .filter(Saml2X509Credential::isSignatureVerficationCredential)
- ----
- ====
- This should change to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- String assertingPartyEntityId = registration.getAssertingPartyDetails().getEntityId();
- String assertionConsumerServiceLocation = registration.getAssertionConsumerServiceLocation();
- String singleSignOnServiceLocation = registration.getAssertingPartyDetails().getSingleSignOnServiceLocation();
- String entityId = registration.getEntityId();
- List<Saml2X509Credential> verifying = registration.getAssertingPartyDetails().getVerificationX509Credentials();
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val assertingPartyEntityId: String = registration.getAssertingPartyDetails().getEntityId()
- val assertionConsumerServiceLocation: String = registration.getAssertionConsumerServiceLocation()
- val singleSignOnServiceLocation: String = registration.getAssertingPartyDetails().getSingleSignOnServiceLocation()
- val entityId: String = registration.getEntityId()
- val verifying: List<Saml2X509Credential> = registration.getAssertingPartyDetails().getVerificationX509Credentials()
- ----
- ====
- For a complete listing of all changed methods, please see {security-api-url}org/springframework/security/saml2/provider/service/registration/RelyingPartyRegistration.html[``RelyingPartyRegistration``'s JavaDoc].
- === Use OpenSAML 4
- OpenSAML 3 has reached its end-of-life.
- As such, Spring Security 6 drops support for it, bumping up its OpenSAML baseline to 4.
- To prepare for the upgrade, update your pom to depend on OpenSAML 4 instead of 3:
- ====
- .Maven
- [source,maven,role="primary"]
- ----
- <dependencyManagement>
- <dependency>
- <groupId>org.opensaml</groupId>
- <artifactId>opensaml-core</artifactId>
- <version>4.2.1</version>
- </dependency>
- <dependency>
- <groupId>org.opensaml</groupId>
- <artifactId>opensaml-saml-api</artifactId>
- <version>4.2.1</version>
- </dependency>
- <dependency>
- <groupId>org.opensaml</groupId>
- <artifactId>opensaml-saml-impl</artifactId>
- <version>4.2.1</version>
- </dependency>
- </dependencyManagement>
- ----
- .Gradle
- [source,gradle,role="secondary"]
- ----
- dependencies {
- constraints {
- api "org.opensaml:opensaml-core:4.2.1"
- api "org.opensaml:opensaml-saml-api:4.2.1"
- api "org.opensaml:opensaml-saml-impl:4.2.1"
- }
- }
- ----
- ====
- You must use at least OpenSAML 4.1.1 to update to Spring Security 6's SAML support.
- === Use `OpenSaml4AuthenticationProvider`
- In order to support both OpenSAML 3 and 4 at the same time, Spring Security released `OpenSamlAuthenticationProvider` and `OpenSaml4AuthenticationProvider`.
- In 6.0, because OpenSAML3 support is removed, `OpenSamlAuthenticationProvider` is removed as well.
- Not all methods in `OpenSamlAuthenticationProvider` were ported 1-to-1 to `OpenSaml4AuthenticationProvider`.
- As such, some adjustment will be required to make the challenge.
- Consider the following representative usage of `OpenSamlAuthenticationProvider`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- OpenSamlAuthenticationProvider versionThree = new OpenSamlAuthenticationProvider();
- versionThree.setAuthoritiesExtractor(myAuthoritiesExtractor);
- versionThree.setResponseTimeValidationSkew(myDuration);
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val versionThree: OpenSamlAuthenticationProvider = OpenSamlAuthenticationProvider()
- versionThree.setAuthoritiesExtractor(myAuthoritiesExtractor)
- versionThree.setResponseTimeValidationSkew(myDuration)
- ----
- ====
- This should change to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- Converter<ResponseToken, Saml2Authentication> delegate = OpenSaml4AuthenticationProvider
- .createDefaultResponseAuthenticationConverter();
- OpenSaml4AuthenticationProvider versionFour = new OpenSaml4AuthenticationProvider();
- versionFour.setResponseAuthenticationConverter((responseToken) -> {
- Saml2Authentication authentication = delegate.convert(responseToken);
- Assertion assertion = responseToken.getResponse().getAssertions().get(0);
- AuthenticatedPrincipal principal = (AuthenticatedPrincipal) authentication.getPrincipal();
- Collection<GrantedAuthority> authorities = myAuthoritiesExtractor.convert(assertion);
- return new Saml2Authentication(principal, authentication.getSaml2Response(), authorities);
- });
- Converter<AssertionToken, Saml2ResponseValidationResult> validator = OpenSaml4AuthenticationProvider
- .createDefaultAssertionValidatorWithParameters((p) -> p.put(CLOCK_SKEW, myDuration));
- versionFour.setAssertionValidator(validator);
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val delegate = OpenSaml4AuthenticationProvider.createDefaultResponseAuthenticationConverter()
- val versionFour = OpenSaml4AuthenticationProvider()
- versionFour.setResponseAuthenticationConverter({
- responseToken -> {
- val authentication = delegate.convert(responseToken)
- val assertion = responseToken.getResponse().getAssertions().get(0)
- val principal = (AuthenticatedPrincipal) authentication.getPrincipal()
- val authorities = myAuthoritiesExtractor.convert(assertion)
- return Saml2Authentication(principal, authentication.getSaml2Response(), authorities)
- }
- })
- val validator = OpenSaml4AuthenticationProvider
- .createDefaultAssertionValidatorWithParameters({ p -> p.put(CLOCK_SKEW, myDuration) })
- versionFour.setAssertionValidator(validator)
- ----
- ====
- [[use-new-requestmatchers]]
- === Use the new `requestMatchers` methods
- In Spring Security 5.8, the {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#antMatchers(java.lang.String...)[`antMatchers`], {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#mvcMatchers(java.lang.String...)[`mvcMatchers`], and {security-api-url}org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.html#regexMatchers(java.lang.String...)[`regexMatchers`] methods were deprecated in favor of new xref::servlet/authorization/authorize-http-requests.adoc#_request_matchers[`requestMatchers` methods].
- The new `requestMatchers` methods were added xref::servlet/authorization/authorize-http-requests.adoc[to `authorizeHttpRequests`], `authorizeRequests`, CSRF configuration, `WebSecurityCustomizer` and any other places that had the specialized `RequestMatcher` methods.
- The deprecated methods are removed in Spring Security 6.
- These new methods have more secure defaults since they choose the most appropriate `RequestMatcher` implementation for your application.
- In summary, the new methods choose the `MvcRequestMatcher` implementation if your application has Spring MVC in the classpath, falling back to the `AntPathRequestMatcher` implementation if Spring MVC is not present (aligning the behavior with the Kotlin equivalent methods).
- To start using the new methods, you can replace the deprecated methods with the new ones. For example, the following application configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .antMatchers("/api/admin/**").hasRole("ADMIN")
- .antMatchers("/api/user/**").hasRole("USER")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- can be changed to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/api/admin/**").hasRole("ADMIN")
- .requestMatchers("/api/user/**").hasRole("USER")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- If you have Spring MVC in the classpath and are using the `mvcMatchers` methods, you can replace it with the new methods and Spring Security will choose the `MvcRequestMatcher` implementation for you.
- The following configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- @EnableWebMvc
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .mvcMatchers("/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- is equivalent to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- @EnableWebMvc
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- If you are customizing the `servletPath` property of the `MvcRequestMatcher`, you can now use the `MvcRequestMatcher.Builder` to create `MvcRequestMatcher` instances that share the same servlet path:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- @EnableWebMvc
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .mvcMatchers("/admin").servletPath("/path").hasRole("ADMIN")
- .mvcMatchers("/user").servletPath("/path").hasRole("USER")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- The code above can be rewritten using the `MvcRequestMatcher.Builder` and the `requestMatchers` method:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Configuration
- @EnableWebSecurity
- @EnableWebMvc
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
- MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector).servletPath("/path");
- http
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers(mvcMatcherBuilder.pattern("/admin")).hasRole("ADMIN")
- .requestMatchers(mvcMatcherBuilder.pattern("/user")).hasRole("USER")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- If you are having problem with the new `requestMatchers` methods, you can always switch back to the `RequestMatcher` implementation that you were using.
- For example, if you still want to use `AntPathRequestMatcher` and `RegexRequestMatcher` implementations, you can use the `requestMatchers` method that accepts a `RequestMatcher` instance:
- ====
- .Java
- [source,java,role="primary"]
- ----
- import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;
- import static org.springframework.security.web.util.matcher.RegexRequestMatcher.regexMatcher;
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig {
- @Bean
- SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers(antMatcher("/user/**")).hasRole("USER")
- .requestMatchers(antMatcher(HttpMethod.POST, "/user/**")).hasRole("ADMIN")
- .requestMatchers(regexMatcher(".*\\?x=y")).hasRole("SPECIAL") // matches /any/path?x=y
- .anyRequest().authenticated()
- );
- return http.build();
- }
- }
- ----
- ====
- Note that the above sample uses static factory methods from {security-api-url}org/springframework/security/web/util/matcher/AntPathRequestMatcher.html[`AntPathRequestMatcher`] and {security-api-url}org/springframework/security/web/util/matcher/RegexRequestMatcher.html[`RegexRequestMatcher`] to improve readability.
- If you are using the `WebSecurityCustomizer` interface, you can replace the deprecated `antMatchers` methods:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public WebSecurityCustomizer webSecurityCustomizer() {
- return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
- }
- ----
- ====
- with their `requestMatchers` counterparts:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public WebSecurityCustomizer webSecurityCustomizer() {
- return (web) -> web.ignoring().requestMatchers("/ignore1", "/ignore2");
- }
- ----
- ====
- The same way, if you are customizing the CSRF configuration to ignore some paths, you can replace the deprecated methods with the `requestMatchers` methods:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .csrf((csrf) -> csrf
- .ignoringAntMatchers("/no-csrf")
- );
- return http.build();
- }
- ----
- ====
- can be changed to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .csrf((csrf) -> csrf
- .ignoringRequestMatchers("/no-csrf")
- );
- return http.build();
- }
- ----
- ====
- [[use-new-security-matchers]]
- === Use the new `securityMatchers` methods
- In Spring Security 5.8, the `antMatchers`, `mvcMatchers` and `requestMatchers` methods from `HttpSecurity` were deprecated in favor of new `securityMatchers` methods.
- Note that these methods are not the same from `authorizeHttpRequests` methods <<use-new-requestmatchers,which were deprecated>> in favor of the `requestMatchers` methods.
- However, the `securityMatchers` methods are similar to the `requestMatchers` methods in the sense that they will choose the most appropriate `RequestMatcher` implementation for your application.
- In summary, the new methods choose the `MvcRequestMatcher` implementation if your application has Spring MVC in the classpath, falling back to the `AntPathRequestMatcher` implementation if Spring MVC is not present (aligning the behavior with the Kotlin equivalent methods).
- Another reason for adding the `securityMatchers` methods is to avoid confusion with the `requestMatchers` methods from `authorizeHttpRequests`.
- The following configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .antMatcher("/api/**", "/app/**")
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/api/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- ----
- ====
- can be rewritten using the `securityMatchers` methods:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .securityMatcher("/api/**", "/app/**")
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/api/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- ----
- ====
- If you are using a custom `RequestMatcher` in your `HttpSecurity` configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .requestMatcher(new MyCustomRequestMatcher())
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/api/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- public class MyCustomRequestMatcher implements RequestMatcher {
- // ...
- }
- ----
- ====
- you can do the same using `securityMatcher`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .securityMatcher(new MyCustomRequestMatcher())
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/api/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- public class MyCustomRequestMatcher implements RequestMatcher {
- // ...
- }
- ----
- ====
- If you are combining multiple `RequestMatcher` implementations in your `HttpSecurity` configuration:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .requestMatchers((matchers) -> matchers
- .antMatchers("/api/**", "/app/**")
- .mvcMatchers("/admin/**")
- .requestMatchers(new MyCustomRequestMatcher())
- )
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- ----
- ====
- you can change it by using `securityMatchers`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .securityMatchers((matchers) -> matchers
- .requestMatchers("/api/**", "/app/**", "/admin/**")
- .requestMatchers(new MyCustomRequestMatcher())
- )
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers("/admin/**").hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- ----
- ====
- If you are having problems with the `securityMatchers` methods choosing the `RequestMatcher` implementation for you, you can always choose the `RequestMatcher` implementation yourself:
- ====
- .Java
- [source,java,role="primary"]
- ----
- import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- .securityMatcher(antMatcher("/api/**"), antMatcher("/app/**"))
- .authorizeHttpRequests((authz) -> authz
- .requestMatchers(antMatcher("/api/admin/**")).hasRole("ADMIN")
- .anyRequest().authenticated()
- );
- return http.build();
- }
- ----
- ====
- === Stop using `Encryptors.queryableText`
- `Encryptors.queryableText(CharSequence,CharSequence)` is unsafe since https://tanzu.vmware.com/security/cve-2020-5408[the same input data will produce the same output].
- It was deprecated and will be removed in 6.0; Spring Security no longer supports encrypting data in this way.
- To upgrade, you will either need to re-encrypt with a supported mechanism or store it decrypted.
- Consider the following pseudocode for reading each encrypted entry from a table, decrypting it, and then re-encrypting it using a supported mechanism:
- ====
- .Java
- [source,java,role="primary"]
- ----
- TextEncryptor deprecated = Encryptors.queryableText(password, salt);
- BytesEncryptor aes = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(12), CipherAlgorithm.GCM);
- TextEncryptor supported = new HexEncodingTextEncryptor(aes);
- for (MyEntry entry : entries) {
- String value = deprecated.decrypt(entry.getEncryptedValue()); <1>
- entry.setEncryptedValue(supported.encrypt(value)); <2>
- entryService.save(entry)
- }
- ----
- ====
- <1> - The above uses the deprecated `queryableText` to convert the value to plaintext.
- <2> - Then, the value is re-encrypted with a supported Spring Security mechanism.
- Please see the reference manual for more information on what xref:features/integrations/cryptography.adoc[encryption mechanisms Spring Security supports].
- === Default authorities for oauth2Login()
- In Spring Security 5, the default `GrantedAuthority` given to a user that authenticates with an OAuth2 or OpenID Connect 1.0 provider (via `oauth2Login()`) is `ROLE_USER`.
- [NOTE]
- ====
- See xref:servlet/oauth2/login/advanced.adoc#oauth2login-advanced-map-authorities[Mapping User Authorities] for more information.
- ====
- In Spring Security 6, the default authority given to a user authenticating with an OAuth2 provider is `OAUTH2_USER`.
- The default authority given to a user authenticating with an OpenID Connect 1.0 provider is `OIDC_USER`.
- These defaults allow clearer distinction of users that have authenticated with an OAuth2 or OpenID Connect 1.0 provider.
- If you are using authorization rules or expressions such as `hasRole("USER")` or `hasAuthority("ROLE_USER")` to authorize users with this specific authority, the new defaults in Spring Security 6 will impact your application.
- To opt into the new Spring Security 6 defaults, the following configuration can be used.
- .Configure oauth2Login() with 6.0 defaults
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- // ...
- .oauth2Login((oauth2Login) -> oauth2Login
- .userInfoEndpoint((userInfo) -> userInfo
- .userAuthoritiesMapper(grantedAuthoritiesMapper())
- )
- );
- return http.build();
- }
- private GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
- return (authorities) -> {
- Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
- authorities.forEach((authority) -> {
- GrantedAuthority mappedAuthority;
- if (authority instanceof OidcUserAuthority) {
- OidcUserAuthority userAuthority = (OidcUserAuthority) authority;
- mappedAuthority = new OidcUserAuthority(
- "OIDC_USER", userAuthority.getIdToken(), userAuthority.getUserInfo());
- } else if (authority instanceof OAuth2UserAuthority) {
- OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) authority;
- mappedAuthority = new OAuth2UserAuthority(
- "OAUTH2_USER", userAuthority.getAttributes());
- } else {
- mappedAuthority = authority;
- }
- mappedAuthorities.add(mappedAuthority);
- });
- return mappedAuthorities;
- };
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- // ...
- oauth2Login {
- userInfoEndpoint {
- userAuthoritiesMapper = grantedAuthoritiesMapper()
- }
- }
- }
- return http.build()
- }
- private fun grantedAuthoritiesMapper(): GrantedAuthoritiesMapper {
- return GrantedAuthoritiesMapper { authorities ->
- authorities.map { authority ->
- when (authority) {
- is OidcUserAuthority ->
- OidcUserAuthority("OIDC_USER", authority.idToken, authority.userInfo)
- is OAuth2UserAuthority ->
- OAuth2UserAuthority("OAUTH2_USER", authority.attributes)
- else -> authority
- }
- }
- }
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper" ... />
- </http>
- ----
- ====
- [[servlet-oauth2-login-authorities-opt-out]]
- ==== Opt-out Steps
- If configuring the new authorities gives you trouble, you can opt out and explicitly use the 5.8 authority of `ROLE_USER` with the following configuration.
- .Configure oauth2Login() with 5.8 defaults
- ====
- .Java
- [source,java,role="primary"]
- ----
- @Bean
- public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
- http
- // ...
- .oauth2Login((oauth2Login) -> oauth2Login
- .userInfoEndpoint((userInfo) -> userInfo
- .userAuthoritiesMapper(grantedAuthoritiesMapper())
- )
- );
- return http.build();
- }
- private GrantedAuthoritiesMapper grantedAuthoritiesMapper() {
- return (authorities) -> {
- Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
- authorities.forEach((authority) -> {
- GrantedAuthority mappedAuthority;
- if (authority instanceof OidcUserAuthority) {
- OidcUserAuthority userAuthority = (OidcUserAuthority) authority;
- mappedAuthority = new OidcUserAuthority(
- "ROLE_USER", userAuthority.getIdToken(), userAuthority.getUserInfo());
- } else if (authority instanceof OAuth2UserAuthority) {
- OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) authority;
- mappedAuthority = new OAuth2UserAuthority(
- "ROLE_USER", userAuthority.getAttributes());
- } else {
- mappedAuthority = authority;
- }
- mappedAuthorities.add(mappedAuthority);
- });
- return mappedAuthorities;
- };
- }
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @Bean
- fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- // ...
- oauth2Login {
- userInfoEndpoint {
- userAuthoritiesMapper = grantedAuthoritiesMapper()
- }
- }
- }
- return http.build()
- }
- private fun grantedAuthoritiesMapper(): GrantedAuthoritiesMapper {
- return GrantedAuthoritiesMapper { authorities ->
- authorities.map { authority ->
- when (authority) {
- is OidcUserAuthority ->
- OidcUserAuthority("ROLE_USER", authority.idToken, authority.userInfo)
- is OAuth2UserAuthority ->
- OAuth2UserAuthority("ROLE_USER", authority.attributes)
- else -> authority
- }
- }
- }
- }
- ----
- .XML
- [source,xml,role="secondary"]
- ----
- <http>
- <oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper" ... />
- </http>
- ----
- ====
- == Reactive
- === Use `AuthorizationManager` for Method Security
- xref:reactive/authorization/method.adoc[Method Security] has been xref:reactive/authorization/method.adoc#jc-enable-reactive-method-security-authorization-manager[improved] through {security-api-url}org/springframework/security/authorization/AuthorizationManager.html[the `AuthorizationManager` API] and direct use of Spring AOP.
- Should you run into trouble with making these changes, you can follow the
- <<reactive-authorizationmanager-methods-opt-out,opt out steps>> at the end of this section.
- In Spring Security 5.8, `useAuthorizationManager` was added to {security-api-url}org/springframework/security/config/annotation/method/configuration/EnableReactiveMethodSecurity.html[`@EnableReactiveMethodSecurity`] to allow applications to opt in to ``AuthorizationManager``'s features.
- [[reactive-change-to-useauthorizationmanager]]
- ==== Change `useAuthorizationManager` to `true`
- To opt in, change `useAuthorizationManager` to `true` like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableReactiveMethodSecurity
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableReactiveMethodSecurity
- ----
- ====
- changes to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableReactiveMethodSecurity(useAuthorizationManager = true)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableReactiveMethodSecurity(useAuthorizationManager = true)
- ----
- ====
- [[reactive-check-for-annotationconfigurationexceptions]]
- ==== Check for ``AnnotationConfigurationException``s
- `useAuthorizationManager` activates stricter enforcement of Spring Security's non-repeatable or otherwise incompatible annotations.
- If after turning on `useAuthorizationManager` you see ``AnnotationConfigurationException``s in your logs, follow the instructions in the exception message to clean up your application's method security annotation usage.
- [[reactive-authorizationmanager-methods-opt-out]]
- ==== Opt-out Steps
- If you ran into trouble with `AuthorizationManager` for reactive method security, you can opt out by changing:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableReactiveMethodSecurity
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableReactiveMethodSecurity
- ----
- ====
- to:
- ====
- .Java
- [source,java,role="primary"]
- ----
- @EnableReactiveMethodSecurity(useAuthorizationManager = false)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- @EnableReactiveMethodSecurity(useAuthorizationManager = false)
- ----
- ====
- === Propagate ``AuthenticationServiceException``s
- {security-api-url}org/springframework/security/web/server/Webauthentication/AuthenticationWebFilter.html[`AuthenticationFilter`] propagates {security-api-url}org/springframework/security/authentication/AuthenticationServiceException.html[``AuthenticationServiceException``]s to the {security-api-url}org/springframework/security/web/server/ServerAuthenticationEntryPoint.html[`ServerAuthenticationEntryPoint`].
- Because ``AuthenticationServiceException``s represent a server-side error instead of a client-side error, in 6.0, this changes to propagate them to the container.
- ==== Configure `ServerAuthenticationFailureHandler` to rethrow ``AuthenticationServiceException``s
- To prepare for the 6.0 default, `httpBasic` and `oauth2ResourceServer` should be configured to rethrow ``AuthenticationServiceException``s.
- For each, construct the appropriate authentication entry point for `httpBasic` and for `oauth2ResourceServer`:
- ====
- .Java
- [source,java,role="primary"]
- ----
- ServerAuthenticationEntryPoint bearerEntryPoint = new BearerTokenServerAuthenticationEntryPoint();
- ServerAuthenticationEntryPoint basicEntryPoint = new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED);
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val bearerEntryPoint: ServerAuthenticationEntryPoint = BearerTokenServerAuthenticationEntryPoint()
- val basicEntryPoint: ServerAuthenticationEntryPoint = HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED)
- ----
- ====
- [NOTE]
- ====
- If you use a custom `AuthenticationEntryPoint` for either or both mechanisms, use that one instead for the remaining steps.
- ====
- Then, construct and configure a `ServerAuthenticationEntryPointFailureHandler` for each one:
- ====
- .Java
- [source,java,role="primary"]
- ----
- AuthenticationFailureHandler bearerFailureHandler = new ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint);
- bearerFailureHandler.setRethrowAuthenticationServiceException(true);
- AuthenticationFailureHandler basicFailureHandler = new ServerAuthenticationEntryPointFailureHandler(basicEntryPoint);
- basicFailureHandler.setRethrowAuthenticationServiceException(true)
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- val bearerFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(bearerEntryPoint)
- bearerFailureHandler.setRethrowAuthenticationServiceException(true)
- val basicFailureHandler: AuthenticationFailureHandler = ServerAuthenticationEntryPointFailureHandler(basicEntryPoint)
- basicFailureHandler.setRethrowAuthenticationServiceException(true)
- ----
- ====
- Finally, wire each authentication failure handler into the DSL, like so:
- ====
- .Java
- [source,java,role="primary"]
- ----
- http
- .httpBasic((basic) -> basic.authenticationFailureHandler(basicFailureHandler))
- .oauth2ResourceServer((oauth2) -> oauth2.authenticationFailureHandler(bearerFailureHandler))
- ----
- .Kotlin
- [source,kotlin,role="secondary"]
- ----
- http {
- httpBasic {
- authenticationFailureHandler = basicFailureHandler
- }
- oauth2ResourceServer {
- authenticationFailureHandler = bearerFailureHandler
- }
- }
- ----
- ====
- [[reactive-authenticationfailurehandler-opt-out]]
- ==== Opt-out Steps
- To opt-out of the 6.0 defaults and instead continue to pass `AuthenticationServiceException` on to ``ServerAuthenticationEntryPoint``s, you can follow the same steps as above, except set `rethrowAuthenticationServiceException` to false.
|