|
30 | 30 | import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
|
31 | 31 | import org.springframework.security.oauth2.server.resource.authentication.JwtReactiveAuthenticationManager;
|
32 | 32 | import org.springframework.security.rsocket.api.PayloadInterceptor;
|
| 33 | +import org.springframework.security.rsocket.authentication.AuthenticationPayloadExchangeConverter; |
33 | 34 | import org.springframework.security.rsocket.core.PayloadSocketAcceptorInterceptor;
|
34 | 35 | import org.springframework.security.rsocket.authentication.AnonymousPayloadInterceptor;
|
35 | 36 | import org.springframework.security.rsocket.authentication.AuthenticationPayloadInterceptor;
|
|
44 | 45 | import reactor.core.publisher.Mono;
|
45 | 46 |
|
46 | 47 | import java.util.ArrayList;
|
| 48 | +import java.util.Arrays; |
47 | 49 | import java.util.List;
|
48 | 50 |
|
49 | 51 | /**
|
@@ -116,6 +118,8 @@ public class RSocketSecurity {
|
116 | 118 |
|
117 | 119 | private BasicAuthenticationSpec basicAuthSpec;
|
118 | 120 |
|
| 121 | + private SimpleAuthenticationSpec simpleAuthSpec; |
| 122 | + |
119 | 123 | private JwtSpec jwtSpec;
|
120 | 124 |
|
121 | 125 | private AuthorizePayloadsSpec authorizePayload;
|
@@ -145,6 +149,58 @@ public RSocketSecurity authenticationManager(ReactiveAuthenticationManager authe
|
145 | 149 | return this;
|
146 | 150 | }
|
147 | 151 |
|
| 152 | + /** |
| 153 | + * Adds support for validating a username and password using |
| 154 | + * <a href="https://github.com/rsocket/rsocket/blob/5920ed374d008abb712cb1fd7c9d91778b2f4a68/Extensions/Security/Simple.md">Simple Authentication</a> |
| 155 | + * @param simple a customizer |
| 156 | + * @return RSocketSecurity for additional configuration |
| 157 | + * @since 5.3 |
| 158 | + */ |
| 159 | + public RSocketSecurity simpleAuthentication(Customizer<SimpleAuthenticationSpec> simple) { |
| 160 | + if (this.simpleAuthSpec == null) { |
| 161 | + this.simpleAuthSpec = new SimpleAuthenticationSpec(); |
| 162 | + } |
| 163 | + simple.customize(this.simpleAuthSpec); |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @since 5.3 |
| 169 | + */ |
| 170 | + public class SimpleAuthenticationSpec { |
| 171 | + private ReactiveAuthenticationManager authenticationManager; |
| 172 | + |
| 173 | + public SimpleAuthenticationSpec authenticationManager(ReactiveAuthenticationManager authenticationManager) { |
| 174 | + this.authenticationManager = authenticationManager; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + private ReactiveAuthenticationManager getAuthenticationManager() { |
| 179 | + if (this.authenticationManager == null) { |
| 180 | + return RSocketSecurity.this.authenticationManager; |
| 181 | + } |
| 182 | + return this.authenticationManager; |
| 183 | + } |
| 184 | + |
| 185 | + protected AuthenticationPayloadInterceptor build() { |
| 186 | + ReactiveAuthenticationManager manager = getAuthenticationManager(); |
| 187 | + AuthenticationPayloadInterceptor result = new AuthenticationPayloadInterceptor(manager); |
| 188 | + result.setAuthenticationConverter(new AuthenticationPayloadExchangeConverter()); |
| 189 | + result.setOrder(PayloadInterceptorOrder.AUTHENTICATION.getOrder()); |
| 190 | + return result; |
| 191 | + } |
| 192 | + |
| 193 | + private SimpleAuthenticationSpec() {} |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Adds authentication with BasicAuthenticationPayloadExchangeConverter. |
| 198 | + * |
| 199 | + * @param basic |
| 200 | + * @return |
| 201 | + * @deprecated Use {@link #simpleAuthentication(Customizer)} |
| 202 | + */ |
| 203 | + @Deprecated |
148 | 204 | public RSocketSecurity basicAuthentication(Customizer<BasicAuthenticationSpec> basic) {
|
149 | 205 | if (this.basicAuthSpec == null) {
|
150 | 206 | this.basicAuthSpec = new BasicAuthenticationSpec();
|
@@ -206,12 +262,17 @@ private ReactiveAuthenticationManager getAuthenticationManager() {
|
206 | 262 | return RSocketSecurity.this.authenticationManager;
|
207 | 263 | }
|
208 | 264 |
|
209 |
| - protected AuthenticationPayloadInterceptor build() { |
| 265 | + protected List<AuthenticationPayloadInterceptor> build() { |
210 | 266 | ReactiveAuthenticationManager manager = getAuthenticationManager();
|
211 |
| - AuthenticationPayloadInterceptor result = new AuthenticationPayloadInterceptor(manager); |
212 |
| - result.setAuthenticationConverter(new BearerPayloadExchangeConverter()); |
213 |
| - result.setOrder(PayloadInterceptorOrder.AUTHENTICATION.getOrder()); |
214 |
| - return result; |
| 267 | + AuthenticationPayloadInterceptor legacy = new AuthenticationPayloadInterceptor(manager); |
| 268 | + legacy.setAuthenticationConverter(new BearerPayloadExchangeConverter()); |
| 269 | + legacy.setOrder(PayloadInterceptorOrder.AUTHENTICATION.getOrder()); |
| 270 | + |
| 271 | + AuthenticationPayloadInterceptor standard = new AuthenticationPayloadInterceptor(manager); |
| 272 | + standard.setAuthenticationConverter(new AuthenticationPayloadExchangeConverter()); |
| 273 | + standard.setOrder(PayloadInterceptorOrder.AUTHENTICATION.getOrder()); |
| 274 | + |
| 275 | + return Arrays.asList(standard, legacy); |
215 | 276 | }
|
216 | 277 |
|
217 | 278 | private JwtSpec() {}
|
@@ -240,8 +301,11 @@ private List<PayloadInterceptor> payloadInterceptors() {
|
240 | 301 | if (this.basicAuthSpec != null) {
|
241 | 302 | result.add(this.basicAuthSpec.build());
|
242 | 303 | }
|
| 304 | + if (this.simpleAuthSpec != null) { |
| 305 | + result.add(this.simpleAuthSpec.build()); |
| 306 | + } |
243 | 307 | if (this.jwtSpec != null) {
|
244 |
| - result.add(this.jwtSpec.build()); |
| 308 | + result.addAll(this.jwtSpec.build()); |
245 | 309 | }
|
246 | 310 | result.add(anonymous());
|
247 | 311 |
|
|
0 commit comments