Skip to content

Use substring instead of replaceFirst in OAuth2AuthorizationConsent #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Please add the Apache License header to all new classes, for example:

```java
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -67,7 +67,7 @@ Use git rebase –interactive, git add –patch and other tools to "squash" mult

== Format commit messages

. Keep the subject line to 50 characters or less if possible.
. Keep the subject line to 50 characters or fewer if possible.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this as it's not related to the proposed replaceFirst() change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgrandja I will submit a new PR for this section. Do you think this is a good idea?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of "less" is proper grammar so we will leave as-is

. Do not end the subject line with a period.
. In the body of the commit message, explain how things worked before this commit, what has changed, and how things work now.
. Include Fixes gh-<issue-number> at the end if this fixes a GitHub issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Set<String> getScopes() {
Set<String> authorities = new HashSet<>();
for (GrantedAuthority authority : getAuthorities()) {
if (authority.getAuthority().startsWith(AUTHORITIES_SCOPE_PREFIX)) {
authorities.add(authority.getAuthority().replaceFirst(AUTHORITIES_SCOPE_PREFIX, ""));
authorities.add(authority.getAuthority().substring(AUTHORITIES_SCOPE_PREFIX.length()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I conducted a simple test and achieved a 40 times performance improvement.

How did you conduct the test?

Copy link
Contributor Author

@heartape heartape May 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all the testing information:

public static void main(String[] args) {
    String scope = "SCOPE_";
    String email = "SCOPE_email";
    int count = 10000000;

    long start1 = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        String s = email.replaceFirst(scope, "");
    }
    long end1 = System.currentTimeMillis();
    System.out.println("replaceFirst : " + (end1 - start1));

    long start2 = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        String s = email.substring(scope.length());
    }
    long end2 = System.currentTimeMillis();
    System.out.println("substring : " + (end2 - start2));
    System.out.println("count : " + count);
}
windows(8c16t 32G) idea 
java version "17.0.3.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment (build 17.0.3.1+2-LTS-6)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.3.1+2-LTS-6, mixed mode, sharing)

result:
replaceFirst : 2383
substring : 69
count : 10000000

###############################################################

centos7 vm(2c 2G)  command line 
java version "17.0.6" 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

result:
replaceFirst : 2856
substring : 107
count : 10000000

}
}
return authorities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ public Authentication convert(HttpServletRequest request) {
principal = ANONYMOUS_AUTHENTICATION;
}

String sessionId = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this as it's not related to the proposed replaceFirst() change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

HttpSession session = request.getSession(false);
if (session != null) {
sessionId = session.getId();
}

// client_id (OPTIONAL)
String clientId = parameters.getFirst(OAuth2ParameterNames.CLIENT_ID);
if (StringUtils.hasText(clientId) &&
Expand All @@ -92,6 +86,12 @@ public Authentication convert(HttpServletRequest request) {
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.STATE);
}

String sessionId = null;
HttpSession session = request.getSession(false);
if (session != null) {
sessionId = session.getId();
}

return new OidcLogoutAuthenticationToken(idTokenHint, principal,
sessionId, clientId, postLogoutRedirectUri, state);
}
Expand Down