|
| 1 | += Authentication Migrations |
| 2 | + |
| 3 | +The following steps relate to how to finish migrating authentication support. |
| 4 | + |
| 5 | +== Propagate ``AuthenticationServiceException``s |
| 6 | + |
| 7 | +{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/web/AuthenticationEntryPoint.html[`AuthenticationEntryPoint`]. |
| 8 | +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. |
| 9 | + |
| 10 | +So, if you opted into this behavior by setting `rethrowAuthenticationServiceException` to `true`, you can now remove it like so: |
| 11 | + |
| 12 | +[tabs] |
| 13 | +====== |
| 14 | +Java:: |
| 15 | ++ |
| 16 | +[source,java,role="primary"] |
| 17 | +---- |
| 18 | +AuthenticationFilter authenticationFilter = new AuthenticationFilter(...); |
| 19 | +AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...); |
| 20 | +handler.setRethrowAuthenticationServiceException(true); |
| 21 | +authenticationFilter.setAuthenticationFailureHandler(handler); |
| 22 | +---- |
| 23 | +
|
| 24 | +Kotlin:: |
| 25 | ++ |
| 26 | +[source,kotlin,role="secondary"] |
| 27 | +---- |
| 28 | +val authenticationFilter: AuthenticationFilter = AuthenticationFilter(...) |
| 29 | +val handler: AuthenticationEntryPointFailureHandler = AuthenticationEntryPointFailureHandler(...) |
| 30 | +handler.setRethrowAuthenticationServiceException(true) |
| 31 | +authenticationFilter.setAuthenticationFailureHandler(handler) |
| 32 | +---- |
| 33 | +
|
| 34 | +Xml:: |
| 35 | ++ |
| 36 | +[source,xml,role="secondary"] |
| 37 | +---- |
| 38 | +<bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter"> |
| 39 | + <!-- ... --> |
| 40 | + <property ref="authenticationFailureHandler"/> |
| 41 | +</bean> |
| 42 | +
|
| 43 | +<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler"> |
| 44 | + <property name="rethrowAuthenticationServiceException" value="true"/> |
| 45 | +</bean> |
| 46 | +---- |
| 47 | +====== |
| 48 | + |
| 49 | +changes to: |
| 50 | + |
| 51 | +[tabs] |
| 52 | +====== |
| 53 | +Java:: |
| 54 | ++ |
| 55 | +[source,java,role="primary"] |
| 56 | +---- |
| 57 | +AuthenticationFilter authenticationFilter = new AuthenticationFilter(...); |
| 58 | +AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(...); |
| 59 | +authenticationFilter.setAuthenticationFailureHandler(handler); |
| 60 | +---- |
| 61 | +
|
| 62 | +Kotlin:: |
| 63 | ++ |
| 64 | +[source,kotlin,role="secondary"] |
| 65 | +---- |
| 66 | +val authenticationFilter: AuthenticationFilter = AuthenticationFilter(...) |
| 67 | +val handler: AuthenticationEntryPointFailureHandler = AuthenticationEntryPointFailureHandler(...) |
| 68 | +authenticationFilter.setAuthenticationFailureHandler(handler) |
| 69 | +---- |
| 70 | +
|
| 71 | +Xml:: |
| 72 | ++ |
| 73 | +[source,xml,role="secondary"] |
| 74 | +---- |
| 75 | +<bean id="authenticationFilter" class="org.springframework.security.web.authentication.AuthenticationFilter"> |
| 76 | + <!-- ... --> |
| 77 | + <property ref="authenticationFailureHandler"/> |
| 78 | +</bean> |
| 79 | +
|
| 80 | +<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler"> |
| 81 | + <!-- ... --> |
| 82 | +</bean> |
| 83 | +---- |
| 84 | +====== |
| 85 | + |
| 86 | +[[servlet-opt-in-sha256-rememberme]] |
| 87 | +== Use SHA-256 in Remember Me |
| 88 | + |
| 89 | +In 6.0, the `TokenBasedRememberMeServices` uses SHA-256 to encode and match the token. |
| 90 | +To complete the migration, any default values can be removed. |
| 91 | + |
| 92 | +For example, if you opted in to the 6.0 default for `encodingAlgorithm` and `matchingAlgorithm` like so: |
| 93 | + |
| 94 | +[tabs] |
| 95 | +====== |
| 96 | +Java:: |
| 97 | ++ |
| 98 | +[source,java,role="primary"] |
| 99 | +---- |
| 100 | +@Configuration |
| 101 | +@EnableWebSecurity |
| 102 | +public class SecurityConfig { |
| 103 | + @Bean |
| 104 | + SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception { |
| 105 | + http |
| 106 | + // ... |
| 107 | + .rememberMe((remember) -> remember |
| 108 | + .rememberMeServices(rememberMeServices) |
| 109 | + ); |
| 110 | + return http.build(); |
| 111 | + } |
| 112 | + @Bean |
| 113 | + RememberMeServices rememberMeServices(UserDetailsService userDetailsService) { |
| 114 | + RememberMeTokenAlgorithm encodingAlgorithm = RememberMeTokenAlgorithm.SHA256; |
| 115 | + TokenBasedRememberMeServices rememberMe = new TokenBasedRememberMeServices(myKey, userDetailsService, encodingAlgorithm); |
| 116 | + rememberMe.setMatchingAlgorithm(RememberMeTokenAlgorithm.SHA256); |
| 117 | + return rememberMe; |
| 118 | + } |
| 119 | +} |
| 120 | +---- |
| 121 | +
|
| 122 | +XML:: |
| 123 | ++ |
| 124 | +[source,xml,role="secondary"] |
| 125 | +---- |
| 126 | +<http> |
| 127 | + <remember-me services-ref="rememberMeServices"/> |
| 128 | +</http> |
| 129 | +<bean id="rememberMeServices" class= |
| 130 | +"org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices"> |
| 131 | + <property name="userDetailsService" ref="myUserDetailsService"/> |
| 132 | + <property name="key" value="springRocks"/> |
| 133 | + <property name="matchingAlgorithm" value="SHA256"/> |
| 134 | + <property name="encodingAlgorithm" value="SHA256"/> |
| 135 | +</bean> |
| 136 | +---- |
| 137 | +====== |
| 138 | + |
| 139 | +then the defaults can be removed: |
| 140 | + |
| 141 | +[tabs] |
| 142 | +====== |
| 143 | +Java:: |
| 144 | ++ |
| 145 | +[source,java,role="primary"] |
| 146 | +---- |
| 147 | +@Configuration |
| 148 | +@EnableWebSecurity |
| 149 | +public class SecurityConfig { |
| 150 | + @Bean |
| 151 | + SecurityFilterChain securityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception { |
| 152 | + http |
| 153 | + // ... |
| 154 | + .rememberMe((remember) -> remember |
| 155 | + .rememberMeServices(rememberMeServices) |
| 156 | + ); |
| 157 | + return http.build(); |
| 158 | + } |
| 159 | + @Bean |
| 160 | + RememberMeServices rememberMeServices(UserDetailsService userDetailsService) { |
| 161 | + return new TokenBasedRememberMeServices(myKey, userDetailsService); |
| 162 | + } |
| 163 | +} |
| 164 | +---- |
| 165 | +
|
| 166 | +XML:: |
| 167 | ++ |
| 168 | +[source,xml,role="secondary"] |
| 169 | +---- |
| 170 | +<http> |
| 171 | + <remember-me services-ref="rememberMeServices"/> |
| 172 | +</http> |
| 173 | +<bean id="rememberMeServices" class= |
| 174 | +"org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices"> |
| 175 | + <property name="userDetailsService" ref="myUserDetailsService"/> |
| 176 | + <property name="key" value="springRocks"/> |
| 177 | +</bean> |
| 178 | +---- |
| 179 | +====== |
| 180 | + |
| 181 | +== Default authorities for oauth2Login() |
| 182 | + |
| 183 | +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`. |
| 184 | + |
| 185 | +In Spring Security 6, the default authority given to a user authenticating with an OAuth2 provider is `OAUTH2_USER`. |
| 186 | +The default authority given to a user authenticating with an OpenID Connect 1.0 provider is `OIDC_USER`. |
| 187 | +If you configured the `GrantedAuthoritiesMapper` only for the purpose of updating to 6.0, you can remove it completely. |
0 commit comments