mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 09:12:14 +00:00
Change default authority for oauth2Login()
Previously, the default authority was ROLE_USER when using oauth2Login() for both OAuth2 and OIDC providers. * Default authority for OAuth2UserAuthority is now OAUTH2_USER * Default authority for OidcUserAuthority is now OIDC_USER Documentation has been updated to include this implementation detail. Closes gh-7856
This commit is contained in:
parent
7527fd811c
commit
181ee7410b
@ -193,7 +193,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(1);
|
assertThat(authentication.getAuthorities()).hasSize(1);
|
||||||
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
||||||
.hasToString("ROLE_USER");
|
.hasToString("OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -208,7 +208,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(1);
|
assertThat(authentication.getAuthorities()).hasSize(1);
|
||||||
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
||||||
.hasToString("ROLE_USER");
|
.hasToString("OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
// gh-6009
|
// gh-6009
|
||||||
@ -246,7 +246,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
Authentication authentication = this.securityContextRepository
|
Authentication authentication = this.securityContextRepository
|
||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(2);
|
assertThat(authentication.getAuthorities()).hasSize(2);
|
||||||
assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER");
|
assertThat(authentication.getAuthorities()).first().hasToString("OAUTH2_USER");
|
||||||
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,7 +266,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
Authentication authentication = this.securityContextRepository
|
Authentication authentication = this.securityContextRepository
|
||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(2);
|
assertThat(authentication.getAuthorities()).hasSize(2);
|
||||||
assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER");
|
assertThat(authentication.getAuthorities()).first().hasToString("OAUTH2_USER");
|
||||||
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,7 +286,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
Authentication authentication = this.securityContextRepository
|
Authentication authentication = this.securityContextRepository
|
||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(2);
|
assertThat(authentication.getAuthorities()).hasSize(2);
|
||||||
assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER");
|
assertThat(authentication.getAuthorities()).first().hasToString("OAUTH2_USER");
|
||||||
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,7 +309,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(1);
|
assertThat(authentication.getAuthorities()).hasSize(1);
|
||||||
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
assertThat(authentication.getAuthorities()).first().isInstanceOf(OAuth2UserAuthority.class)
|
||||||
.hasToString("ROLE_USER");
|
.hasToString("OAUTH2_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
// gh-5521
|
// gh-5521
|
||||||
@ -518,7 +518,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(1);
|
assertThat(authentication.getAuthorities()).hasSize(1);
|
||||||
assertThat(authentication.getAuthorities()).first().isInstanceOf(OidcUserAuthority.class)
|
assertThat(authentication.getAuthorities()).first().isInstanceOf(OidcUserAuthority.class)
|
||||||
.hasToString("ROLE_USER");
|
.hasToString("OIDC_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -538,7 +538,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(1);
|
assertThat(authentication.getAuthorities()).hasSize(1);
|
||||||
assertThat(authentication.getAuthorities()).first().isInstanceOf(OidcUserAuthority.class)
|
assertThat(authentication.getAuthorities()).first().isInstanceOf(OidcUserAuthority.class)
|
||||||
.hasToString("ROLE_USER");
|
.hasToString("OIDC_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -557,7 +557,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
Authentication authentication = this.securityContextRepository
|
Authentication authentication = this.securityContextRepository
|
||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(2);
|
assertThat(authentication.getAuthorities()).hasSize(2);
|
||||||
assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER");
|
assertThat(authentication.getAuthorities()).first().hasToString("OIDC_USER");
|
||||||
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER");
|
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,7 +577,7 @@ public class OAuth2LoginConfigurerTests {
|
|||||||
Authentication authentication = this.securityContextRepository
|
Authentication authentication = this.securityContextRepository
|
||||||
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
.loadContext(new HttpRequestResponseHolder(this.request, this.response)).getAuthentication();
|
||||||
assertThat(authentication.getAuthorities()).hasSize(2);
|
assertThat(authentication.getAuthorities()).hasSize(2);
|
||||||
assertThat(authentication.getAuthorities()).first().hasToString("ROLE_USER");
|
assertThat(authentication.getAuthorities()).first().hasToString("OIDC_USER");
|
||||||
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER");
|
assertThat(authentication.getAuthorities()).last().hasToString("ROLE_OIDC_USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,7 +302,8 @@ The UserInfo Endpoint includes a number of configuration options, as described i
|
|||||||
[[webflux-oauth2-login-advanced-map-authorities]]
|
[[webflux-oauth2-login-advanced-map-authorities]]
|
||||||
=== Mapping User Authorities
|
=== Mapping User Authorities
|
||||||
|
|
||||||
After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) may be mapped to a new set of `GrantedAuthority` instances, which will be supplied to `OAuth2AuthenticationToken` when completing the authentication.
|
After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) contains a list of granted authorities populated from `OAuth2UserRequest.getAccessToken().getScopes()` and prefixed with `SCOPE_`.
|
||||||
|
These granted authorities may be mapped to a new set of `GrantedAuthority` instances, which will be supplied to `OAuth2AuthenticationToken` when completing the authentication.
|
||||||
|
|
||||||
[TIP]
|
[TIP]
|
||||||
`OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
|
`OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
|
||||||
@ -316,6 +317,8 @@ There are a couple of options to choose from when mapping user authorities:
|
|||||||
[[webflux-oauth2-login-advanced-map-authorities-grantedauthoritiesmapper]]
|
[[webflux-oauth2-login-advanced-map-authorities-grantedauthoritiesmapper]]
|
||||||
==== Using a GrantedAuthoritiesMapper
|
==== Using a GrantedAuthoritiesMapper
|
||||||
|
|
||||||
|
The `GrantedAuthoritiesMapper` is given a list of granted authorities which contains a special authority of type `OAuth2UserAuthority` and the authority string `OAUTH2_USER` (or `OidcUserAuthority` and the authority string `OIDC_USER`).
|
||||||
|
|
||||||
Register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the configuration, as shown in the following example:
|
Register a `GrantedAuthoritiesMapper` `@Bean` to have it automatically applied to the configuration, as shown in the following example:
|
||||||
|
|
||||||
.Granted Authorities Mapper Configuration
|
.Granted Authorities Mapper Configuration
|
||||||
|
@ -425,7 +425,8 @@ The UserInfo Endpoint includes a number of configuration options, as described i
|
|||||||
[[oauth2login-advanced-map-authorities]]
|
[[oauth2login-advanced-map-authorities]]
|
||||||
=== Mapping User Authorities
|
=== Mapping User Authorities
|
||||||
|
|
||||||
After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) can be mapped to a new set of `GrantedAuthority` instances, which are supplied to `OAuth2AuthenticationToken` when completing the authentication.
|
After the user successfully authenticates with the OAuth 2.0 Provider, the `OAuth2User.getAuthorities()` (or `OidcUser.getAuthorities()`) contains a list of granted authorities populated from `OAuth2UserRequest.getAccessToken().getScopes()` and prefixed with `SCOPE_`.
|
||||||
|
These granted authorities can be mapped to a new set of `GrantedAuthority` instances, which are supplied to `OAuth2AuthenticationToken` when completing the authentication.
|
||||||
|
|
||||||
[TIP]
|
[TIP]
|
||||||
`OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
|
`OAuth2AuthenticationToken.getAuthorities()` is used for authorizing requests, such as in `hasRole('USER')` or `hasRole('ADMIN')`.
|
||||||
@ -439,6 +440,8 @@ There are a couple of options to choose from when mapping user authorities:
|
|||||||
[[oauth2login-advanced-map-authorities-grantedauthoritiesmapper]]
|
[[oauth2login-advanced-map-authorities-grantedauthoritiesmapper]]
|
||||||
==== Using a GrantedAuthoritiesMapper
|
==== Using a GrantedAuthoritiesMapper
|
||||||
|
|
||||||
|
The `GrantedAuthoritiesMapper` is given a list of granted authorities which contains a special authority of type `OAuth2UserAuthority` and the authority string `OAUTH2_USER` (or `OidcUserAuthority` and the authority string `OIDC_USER`).
|
||||||
|
|
||||||
Provide an implementation of `GrantedAuthoritiesMapper` and configure it, as follows:
|
Provide an implementation of `GrantedAuthoritiesMapper` and configure it, as follows:
|
||||||
|
|
||||||
.Granted Authorities Mapper Configuration
|
.Granted Authorities Mapper Configuration
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -198,6 +198,9 @@ public class OidcReactiveOAuth2UserServiceTests {
|
|||||||
assertThat(user.getAuthorities()).hasSize(1);
|
assertThat(user.getAuthorities()).hasSize(1);
|
||||||
Iterator<? extends GrantedAuthority> authorities = user.getAuthorities().iterator();
|
Iterator<? extends GrantedAuthority> authorities = user.getAuthorities().iterator();
|
||||||
assertThat(authorities.next()).isInstanceOf(OAuth2UserAuthority.class);
|
assertThat(authorities.next()).isInstanceOf(OAuth2UserAuthority.class);
|
||||||
|
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next();
|
||||||
|
assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER");
|
||||||
|
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
private OidcUserRequest userRequest() {
|
private OidcUserRequest userRequest() {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -246,7 +246,7 @@ public class OidcUserServiceTests {
|
|||||||
assertThat(user.getAuthorities().size()).isEqualTo(3);
|
assertThat(user.getAuthorities().size()).isEqualTo(3);
|
||||||
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OidcUserAuthority.class);
|
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OidcUserAuthority.class);
|
||||||
OidcUserAuthority userAuthority = (OidcUserAuthority) user.getAuthorities().iterator().next();
|
OidcUserAuthority userAuthority = (OidcUserAuthority) user.getAuthorities().iterator().next();
|
||||||
assertThat(userAuthority.getAuthority()).isEqualTo("ROLE_USER");
|
assertThat(userAuthority.getAuthority()).isEqualTo("OIDC_USER");
|
||||||
assertThat(userAuthority.getIdToken()).isEqualTo(user.getIdToken());
|
assertThat(userAuthority.getIdToken()).isEqualTo(user.getIdToken());
|
||||||
assertThat(userAuthority.getUserInfo()).isEqualTo(user.getUserInfo());
|
assertThat(userAuthority.getUserInfo()).isEqualTo(user.getUserInfo());
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -154,7 +154,7 @@ public class DefaultOAuth2UserServiceTests {
|
|||||||
assertThat(user.getAuthorities().size()).isEqualTo(1);
|
assertThat(user.getAuthorities().size()).isEqualTo(1);
|
||||||
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
|
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
|
||||||
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next();
|
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next();
|
||||||
assertThat(userAuthority.getAuthority()).isEqualTo("ROLE_USER");
|
assertThat(userAuthority.getAuthority()).isEqualTo("OAUTH2_USER");
|
||||||
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -141,7 +141,7 @@ public class DefaultReactiveOAuth2UserServiceTests {
|
|||||||
assertThat(user.getAuthorities().size()).isEqualTo(1);
|
assertThat(user.getAuthorities().size()).isEqualTo(1);
|
||||||
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
|
assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OAuth2UserAuthority.class);
|
||||||
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next();
|
OAuth2UserAuthority userAuthority = (OAuth2UserAuthority) user.getAuthorities().iterator().next();
|
||||||
assertThat(userAuthority.getAuthority()).isEqualTo("ROLE_USER");
|
assertThat(userAuthority.getAuthority()).isEqualTo("OAUTH2_USER");
|
||||||
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
assertThat(userAuthority.getAttributes()).isEqualTo(user.getAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -48,13 +48,13 @@ public class OidcUserAuthority extends OAuth2UserAuthority {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a {@code OidcUserAuthority} using the provided parameters and defaults
|
* Constructs a {@code OidcUserAuthority} using the provided parameters and defaults
|
||||||
* {@link #getAuthority()} to {@code ROLE_USER}.
|
* {@link #getAuthority()} to {@code OIDC_USER}.
|
||||||
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
|
* @param idToken the {@link OidcIdToken ID Token} containing claims about the user
|
||||||
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
|
* @param userInfo the {@link OidcUserInfo UserInfo} containing claims about the user,
|
||||||
* may be {@code null}
|
* may be {@code null}
|
||||||
*/
|
*/
|
||||||
public OidcUserAuthority(OidcIdToken idToken, OidcUserInfo userInfo) {
|
public OidcUserAuthority(OidcIdToken idToken, OidcUserInfo userInfo) {
|
||||||
this("ROLE_USER", idToken, userInfo);
|
this("OIDC_USER", idToken, userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,11 +43,11 @@ public class OAuth2UserAuthority implements GrantedAuthority {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a {@code OAuth2UserAuthority} using the provided parameters and defaults
|
* Constructs a {@code OAuth2UserAuthority} using the provided parameters and defaults
|
||||||
* {@link #getAuthority()} to {@code ROLE_USER}.
|
* {@link #getAuthority()} to {@code OAUTH2_USER}.
|
||||||
* @param attributes the attributes about the user
|
* @param attributes the attributes about the user
|
||||||
*/
|
*/
|
||||||
public OAuth2UserAuthority(Map<String, Object> attributes) {
|
public OAuth2UserAuthority(Map<String, Object> attributes) {
|
||||||
this("ROLE_USER", attributes);
|
this("OAUTH2_USER", attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user