Skip to content

Commit bf1bbd1

Browse files
committed
Allow configuration of openid login through nested builder
Issue: gh-5557
1 parent c3dad06 commit bf1bbd1

File tree

3 files changed

+370
-2
lines changed

3 files changed

+370
-2
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,128 @@ public OpenIDLoginConfigurer<HttpSecurity> openidLogin() throws Exception {
239239
return getOrApply(new OpenIDLoginConfigurer<>());
240240
}
241241

242+
/**
243+
* Allows configuring OpenID based authentication.
244+
*
245+
* <h2>Example Configurations</h2>
246+
*
247+
* A basic example accepting the defaults and not using attribute exchange:
248+
*
249+
* <pre>
250+
* &#064;Configuration
251+
* &#064;EnableWebSecurity
252+
* public class OpenIDLoginConfig extends WebSecurityConfigurerAdapter {
253+
*
254+
* &#064;Override
255+
* protected void configure(HttpSecurity http) {
256+
* http
257+
* .authorizeRequests(authorizeRequests ->
258+
* authorizeRequests
259+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
260+
* )
261+
* .openidLogin(openidLogin ->
262+
* openidLogin
263+
* .permitAll()
264+
* );
265+
* }
266+
*
267+
* &#064;Override
268+
* protected void configure(AuthenticationManagerBuilder auth) throws Exception {
269+
* auth.inMemoryAuthentication()
270+
* // the username must match the OpenID of the user you are
271+
* // logging in with
272+
* .withUser(
273+
* &quot;https://www.google.com/accounts/o8/id?id=lmkCn9xzPdsxVwG7pjYMuDgNNdASFmobNkcRPaWU&quot;)
274+
* .password(&quot;password&quot;).roles(&quot;USER&quot;);
275+
* }
276+
* }
277+
* </pre>
278+
*
279+
* A more advanced example demonstrating using attribute exchange and providing a
280+
* custom AuthenticationUserDetailsService that will make any user that authenticates
281+
* a valid user.
282+
*
283+
* <pre>
284+
* &#064;Configuration
285+
* &#064;EnableWebSecurity
286+
* public class OpenIDLoginConfig extends WebSecurityConfigurerAdapter {
287+
*
288+
* &#064;Override
289+
* protected void configure(HttpSecurity http) throws Exception {
290+
* http.authorizeRequests(authorizeRequests ->
291+
* authorizeRequests
292+
* .antMatchers(&quot;/**&quot;).hasRole(&quot;USER&quot;)
293+
* )
294+
* .openidLogin(openidLogin ->
295+
* openidLogin
296+
* .loginPage(&quot;/login&quot;)
297+
* .permitAll()
298+
* .authenticationUserDetailsService(
299+
* new AutoProvisioningUserDetailsService())
300+
* .attributeExchange(googleExchange ->
301+
* googleExchange
302+
* .identifierPattern(&quot;https://www.google.com/.*&quot;)
303+
* .attribute(emailAttribute ->
304+
* emailAttribute
305+
* .name(&quot;email&quot;)
306+
* .type(&quot;https://axschema.org/contact/email&quot;)
307+
* .required(true)
308+
* )
309+
* .attribute(firstnameAttribute ->
310+
* firstnameAttribute
311+
* .name(&quot;firstname&quot;)
312+
* .type(&quot;https://axschema.org/namePerson/first&quot;)
313+
* .required(true)
314+
* )
315+
* .attribute(lastnameAttribute ->
316+
* lastnameAttribute
317+
* .name(&quot;lastname&quot;)
318+
* .type(&quot;https://axschema.org/namePerson/last&quot;)
319+
* .required(true)
320+
* )
321+
* )
322+
* .attributeExchange(yahooExchange ->
323+
* yahooExchange
324+
* .identifierPattern(&quot;.*yahoo.com.*&quot;)
325+
* .attribute(emailAttribute ->
326+
* emailAttribute
327+
* .name(&quot;email&quot;)
328+
* .type(&quot;https://schema.openid.net/contact/email&quot;)
329+
* .required(true)
330+
* )
331+
* .attribute(fullnameAttribute ->
332+
* fullnameAttribute
333+
* .name(&quot;fullname&quot;)
334+
* .type(&quot;https://axschema.org/namePerson&quot;)
335+
* .required(true)
336+
* )
337+
* )
338+
* );
339+
* }
340+
* }
341+
*
342+
* public class AutoProvisioningUserDetailsService implements
343+
* AuthenticationUserDetailsService&lt;OpenIDAuthenticationToken&gt; {
344+
* public UserDetails loadUserDetails(OpenIDAuthenticationToken token)
345+
* throws UsernameNotFoundException {
346+
* return new User(token.getName(), &quot;NOTUSED&quot;,
347+
* AuthorityUtils.createAuthorityList(&quot;ROLE_USER&quot;));
348+
* }
349+
* }
350+
* </pre>
351+
*
352+
* @see OpenIDLoginConfigurer
353+
*
354+
* @param openidLoginCustomizer the {@link Customizer} to provide more options for
355+
* the {@link OpenIDLoginConfigurer}
356+
* @return the {@link HttpSecurity} for further customizations
357+
* @throws Exception
358+
*/
359+
public HttpSecurity openidLogin(Customizer<OpenIDLoginConfigurer<HttpSecurity>> openidLoginCustomizer) throws Exception {
360+
openidLoginCustomizer.customize(getOrApply(new OpenIDLoginConfigurer<>()));
361+
return HttpSecurity.this;
362+
}
363+
242364
/**
243365
* Adds the Security headers to the response. This is activated by default when using
244366
* {@link WebSecurityConfigurerAdapter}'s default constructor. Accepting the

config/src/main/java/org/springframework/security/config/annotation/web/configurers/openid/OpenIDLoginConfigurer.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.security.authentication.AuthenticationDetailsSource;
2929
import org.springframework.security.authentication.AuthenticationManager;
30+
import org.springframework.security.config.Customizer;
3031
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
3132
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3233
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -148,6 +149,24 @@ public AttributeExchangeConfigurer attributeExchange(String identifierPattern) {
148149
return attributeExchangeConfigurer;
149150
}
150151

152+
/**
153+
* Sets up OpenID attribute exchange for OpenIDs matching the specified pattern.
154+
* The default pattern is &quot;.*&quot;, it can be specified using
155+
* {@link AttributeExchangeConfigurer#identifierPattern(String)}
156+
*
157+
* @param attributeExchangeCustomizer the {@link Customizer} to provide more options for
158+
* the {@link AttributeExchangeConfigurer}
159+
* @return a {@link OpenIDLoginConfigurer} for further customizations
160+
* @throws Exception
161+
*/
162+
public OpenIDLoginConfigurer<H> attributeExchange(Customizer<AttributeExchangeConfigurer> attributeExchangeCustomizer)
163+
throws Exception {
164+
AttributeExchangeConfigurer attributeExchangeConfigurer = new AttributeExchangeConfigurer(".*");
165+
attributeExchangeCustomizer.customize(attributeExchangeConfigurer);
166+
this.attributeExchangeConfigurers.add(attributeExchangeConfigurer);
167+
return this;
168+
}
169+
151170
/**
152171
* Allows specifying the {@link OpenIDConsumer} to be used. The default is using an
153172
* {@link OpenID4JavaConsumer}.
@@ -373,7 +392,7 @@ private void initDefaultLoginFilter(H http) {
373392
* @author Rob Winch
374393
*/
375394
public final class AttributeExchangeConfigurer {
376-
private final String identifier;
395+
private String identifier;
377396
private List<OpenIDAttribute> attributes = new ArrayList<>();
378397
private List<AttributeConfigurer> attributeConfigurers = new ArrayList<>();
379398

@@ -395,6 +414,19 @@ public OpenIDLoginConfigurer<H> and() {
395414
return OpenIDLoginConfigurer.this;
396415
}
397416

417+
/**
418+
* Sets the regular expression for matching on OpenID's (i.e.
419+
* "https://www.google.com/.*", ".*yahoo.com.*", etc)
420+
*
421+
* @param identifierPattern the regular expression for matching on OpenID's
422+
* @return the {@link AttributeExchangeConfigurer} for further customization of
423+
* attribute exchange
424+
*/
425+
public AttributeExchangeConfigurer identifierPattern(String identifierPattern) {
426+
this.identifier = identifierPattern;
427+
return this;
428+
}
429+
398430
/**
399431
* Adds an {@link OpenIDAttribute} to be obtained for the configured OpenID
400432
* pattern.
@@ -419,6 +451,22 @@ public AttributeConfigurer attribute(String name) {
419451
return attributeConfigurer;
420452
}
421453

454+
/**
455+
* Adds an {@link OpenIDAttribute} named &quot;default-attribute&quot;.
456+
* The name can by updated using {@link AttributeConfigurer#name(String)}.
457+
*
458+
* @param attributeCustomizer the {@link Customizer} to provide more options for
459+
* the {@link AttributeConfigurer}
460+
* @return a {@link AttributeExchangeConfigurer} for further customizations
461+
* @throws Exception
462+
*/
463+
public AttributeExchangeConfigurer attribute(Customizer<AttributeConfigurer> attributeCustomizer) throws Exception {
464+
AttributeConfigurer attributeConfigurer = new AttributeConfigurer();
465+
attributeCustomizer.customize(attributeConfigurer);
466+
this.attributeConfigurers.add(attributeConfigurer);
467+
return this;
468+
}
469+
422470
/**
423471
* Gets the {@link OpenIDAttribute}'s for the configured OpenID pattern
424472
* @return
@@ -443,6 +491,16 @@ public final class AttributeConfigurer {
443491
private boolean required = false;
444492
private String type;
445493

494+
/**
495+
* Creates a new instance named "default-attribute".
496+
* The name can by updated using {@link #name(String)}.
497+
*
498+
* @see AttributeExchangeConfigurer#attribute(String)
499+
*/
500+
private AttributeConfigurer() {
501+
this.name = "default-attribute";
502+
}
503+
446504
/**
447505
* Creates a new instance
448506
* @param name the name of the attribute
@@ -486,6 +544,16 @@ public AttributeConfigurer type(String type) {
486544
return this;
487545
}
488546

547+
/**
548+
* The OpenID attribute name.
549+
* @param name
550+
* @return the {@link AttributeConfigurer} for further customizations
551+
*/
552+
public AttributeConfigurer name(String name) {
553+
this.name = name;
554+
return this;
555+
}
556+
489557
/**
490558
* Gets the {@link AttributeExchangeConfigurer} for further customization of
491559
* the attributes

0 commit comments

Comments
 (0)