Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For microservices + OIDC, cache the requests to the OIDC server
Without this cache, each microservice call does a cache to the "user-info" endpoint on the OIDC server, which will flood the OIDC server
- Loading branch information
Showing
4 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../server/templates/src/main/java/package/security/oauth2/_CachedUserInfoTokenServices.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<%# | ||
Copyright 2013-2017 the original author or authors from the JHipster project. | ||
|
||
This file is part of the JHipster project, see https://jhipster.github.io/ | ||
for more information. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
-%> | ||
package <%=packageName%>.security.oauth2; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
public class CachedUserInfoTokenServices extends UserInfoTokenServices { | ||
private final Logger log = LoggerFactory.getLogger(UserInfoTokenServices.class); | ||
public CachedUserInfoTokenServices(String userInfoEndpointUrl, String clientId) { | ||
super(userInfoEndpointUrl, clientId); | ||
} | ||
@Override | ||
@Cacheable("oAuth2Authentication") | ||
public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException { | ||
log.debug("Getting user information from OpenID Connect server"); | ||
return super.loadAuthentication(accessToken); | ||
} | ||
} |
fba5187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mraible @danielpetisme I'm sorry I'm going fast here, I need this for one of my clients - but I'd love to have your review here, this solves a very important performance issue, as normally microservices hit the OIDC server all the time to get the "user info"
fba5187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Should it apply to monoliths too or they're OK because of sessions?
fba5187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
fba5187
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes only for microservices as they are stateless - you don't have this issue with gateways and monoliths.
Now I messed up because this needs a cache, so this only works if you have an Hibernate L2 cache, which is stupid... I will correct this today and create a new ticket to refactor caching.