-
Notifications
You must be signed in to change notification settings - Fork 8.8k
optimize: add jwt authentication for RegisterXXRequests #6317
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
Open
ggbocoder
wants to merge
21
commits into
apache:2.x
Choose a base branch
from
ggbocoder:jwt1
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
50c77dc
add signature
ggbocoder b36f5b2
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder e15537d
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder 85398ec
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder 94c95cd
add jwt authentication
ggbocoder 8e1d8c0
Merge branch '2.x' into jwt1
ggbocoder e4e5e32
io.seata -> org.apache.seata
ggbocoder fd80bb5
fix test
ggbocoder 77d6a50
fix test
ggbocoder ea506ae
fix test
ggbocoder e83ac49
fix test
ggbocoder 3d55930
fix test
ggbocoder e70d44d
fix style
ggbocoder c5cc3d1
fix style
ggbocoder 5b6e9a2
fix style
ggbocoder 6a356f4
put the constants in ConfigurationKeys
ggbocoder 1c60203
add default configs
ggbocoder ee365e9
opt
ggbocoder 744c395
opt
ggbocoder 2e0b742
opt
ggbocoder 8e9002c
opt
ggbocoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
core/src/main/java/org/apache/seata/core/auth/JwtAuthManager.java
This file contains hidden or 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,133 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.seata.core.auth; | ||
|
||
|
||
|
||
import org.apache.seata.common.util.StringUtils; | ||
import org.apache.seata.config.ConfigurationFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.apache.seata.common.ConfigurationKeys.EXTRA_DATA_KV_CHAR; | ||
import static org.apache.seata.common.ConfigurationKeys.EXTRA_DATA_SPLIT_CHAR; | ||
|
||
|
||
public class JwtAuthManager { | ||
private String accessToken; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
public final static String PRO_USERNAME = "username"; | ||
|
||
public final static String PRO_PASSWORD = "password"; | ||
|
||
public final static String PRO_TOKEN = "token"; | ||
|
||
private static JwtAuthManager instance; | ||
|
||
private JwtAuthManager() { | ||
} | ||
|
||
public static JwtAuthManager getInstance() { | ||
if (instance == null) { | ||
instance = new JwtAuthManager(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void init() { | ||
username = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("security." + PRO_USERNAME); | ||
xingfudeshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
password = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig("security." + PRO_PASSWORD); | ||
} | ||
|
||
public String getToken() { | ||
return accessToken; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public void refreshToken(String newToken) { | ||
accessToken = newToken; | ||
} | ||
|
||
public void setAccessToken(String token) { | ||
accessToken = token; | ||
} | ||
|
||
public static HashMap<String, String> convertToHashMap(String inputString) { | ||
HashMap<String, String> resultMap = new HashMap<>(); | ||
if (StringUtils.isBlank(inputString)) { | ||
return resultMap; | ||
} | ||
String[] keyValuePairs = inputString.split(EXTRA_DATA_SPLIT_CHAR); | ||
for (String pair : keyValuePairs) { | ||
String[] keyValue = pair.trim().split(EXTRA_DATA_KV_CHAR); | ||
if (keyValue.length == 2) { | ||
resultMap.put(keyValue[0].trim(), keyValue[1].trim()); | ||
} | ||
} | ||
return resultMap; | ||
} | ||
|
||
public static String convertToString(HashMap<String, String> inputMap) { | ||
if (inputMap == null || inputMap.isEmpty()) { | ||
return ""; | ||
} | ||
StringBuilder resultString = new StringBuilder(); | ||
for (Map.Entry<String, String> entry : inputMap.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
String pair = key + EXTRA_DATA_KV_CHAR + value + EXTRA_DATA_SPLIT_CHAR; | ||
resultString.append(pair); | ||
} | ||
if (resultString.length() > 0) { | ||
resultString.deleteCharAt(resultString.length() - 1); | ||
} | ||
return resultString.toString(); | ||
} | ||
public static String refreshAuthData(String extraData) { | ||
HashMap<String,String> extraDataMap = convertToHashMap(extraData); | ||
extraDataMap.remove(PRO_TOKEN); | ||
if(null != getInstance().getToken()){ | ||
extraDataMap.put(PRO_TOKEN,getInstance().getToken()); | ||
}else if(null!= getInstance().getUsername() && null != getInstance().getPassword()){ | ||
extraDataMap.put(PRO_USERNAME,getInstance().getUsername()); | ||
extraDataMap.put(PRO_PASSWORD,getInstance().getPassword()); | ||
} | ||
return convertToString(extraDataMap); | ||
} | ||
|
||
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.