Class ApiSecurityConfig
java.lang.Object
com.irurueta.authserver.authorization.api.web.ApiSecurityConfig
Security filter chain of the REST API under
/api/**: stateless, bearer tokens issued by this server and
validated by a decoder of its own (never the framework-wide one, which also serves the UserInfo endpoint). That
decoder only accepts a token that this server signed, whose issuer is this server, whose timestamps are valid,
whose aud lists the API audience (authserver.api.audience, default auth-server) and that
does not carry the purpose claim of the account tokens (an e-mail verification JWT is never a bearer
token). Authentication and access failures are answered as described by RFC 6750
(WWW-Authenticate: Bearer ...).-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringPath pattern of the administration API.static final StringScope that grants access to the administration API; a role alone never does.static final StringProperty that holds the audience a token must carry to call the API.static final StringPath pattern of the REST API.private static final StringPrefix of the authorities derived from roles.private static final StringName of the claim that holds the roles. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverterCreates the converter that maps thescopeclaim toSCOPE_*authorities and therolesclaim toROLE_*authorities.(package private) static org.springframework.security.oauth2.jwt.JwtDecoderapiJwtDecoder(com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience) Creates the decoder of the API's bearer tokens: signature against the published keys, issuer, timestamps, audience, and nopurposeclaim.org.springframework.security.web.SecurityFilterChainapiSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http, com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience) Creates the chain of the REST API.private static org.springframework.security.oauth2.core.OAuth2TokenValidatorResultrejectPurposeBoundToken(org.springframework.security.oauth2.jwt.Jwt jwt) Rejects a purpose-bound account token (such as an e-mail verification JWT): it is signed by this server but never meant as a bearer token.private static Collection<org.springframework.security.core.GrantedAuthority> roleAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt) Maps the roles of a token to authorities, adding theROLE_prefix when it is missing.
-
Field Details
-
API_PATTERN
Path pattern of the REST API.- See Also:
-
ADMIN_PATTERN
Path pattern of the administration API.- See Also:
-
ADMIN_SCOPE_AUTHORITY
Scope that grants access to the administration API; a role alone never does.- See Also:
-
API_AUDIENCE_PROPERTY
Property that holds the audience a token must carry to call the API.- See Also:
-
ROLE_PREFIX
Prefix of the authorities derived from roles.- See Also:
-
ROLES_CLAIM
Name of the claim that holds the roles.- See Also:
-
-
Constructor Details
-
ApiSecurityConfig
public ApiSecurityConfig()
-
-
Method Details
-
apiSecurityFilterChain
@Bean @Order(0) public org.springframework.security.web.SecurityFilterChain apiSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http, com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, @Value("${spring.security.oauth2.authorizationserver.issuer}") String issuer, @Value("${authserver.api.audience:auth-server}") String audience) throws Exception Creates the chain of the REST API.- Parameters:
http- security builder.jwkSource- the published keys of this server.issuer- issuer of this server (spring.security.oauth2.authorizationserver.issuer).audience- audience the API requires (authserver.api.audience).- Returns:
- the API filter chain.
- Throws:
Exception- if the chain cannot be built.
-
apiJwtDecoder
static org.springframework.security.oauth2.jwt.JwtDecoder apiJwtDecoder(com.nimbusds.jose.jwk.source.JWKSource<com.nimbusds.jose.proc.SecurityContext> jwkSource, String issuer, String audience) Creates the decoder of the API's bearer tokens: signature against the published keys, issuer, timestamps, audience, and nopurposeclaim.- Parameters:
jwkSource- the published keys of this server.issuer- issuer every token must have.audience- audience every token must list.- Returns:
- the decoder.
-
rejectPurposeBoundToken
private static org.springframework.security.oauth2.core.OAuth2TokenValidatorResult rejectPurposeBoundToken(org.springframework.security.oauth2.jwt.Jwt jwt) Rejects a purpose-bound account token (such as an e-mail verification JWT): it is signed by this server but never meant as a bearer token.- Parameters:
jwt- the token.- Returns:
- a failure when the token carries the
purposeclaim, a success otherwise.
-
apiJwtAuthenticationConverter
org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter apiJwtAuthenticationConverter()Creates the converter that maps thescopeclaim toSCOPE_*authorities and therolesclaim toROLE_*authorities.- Returns:
- the converter.
-
roleAuthorities
private static Collection<org.springframework.security.core.GrantedAuthority> roleAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt) Maps the roles of a token to authorities, adding theROLE_prefix when it is missing.- Parameters:
jwt- token to read.- Returns:
- the role authorities, empty when the token has no roles.
-