我有如下地图
Map<String, String> values = new HashMap<String, String>();
values.put("aa", "20");
values.put("bb", "30");
values.put("cc", "20");
values.put("dd", "45");
values.put("ee", "35");
values.put("ff", "35");
values.put("gg", "20");
Run Code Online (Sandbox Code Playgroud)
我想以格式创建新地图 Map<String,List<String>> ,示例输出将为
"20" -> ["aa","cc","gg"]
"30" -> ["bb"]
"35" -> ["ee","ff"]
"45" -> ["dd"]
Run Code Online (Sandbox Code Playgroud)
我可以通过迭代实体来做
Map<String, List<String>> output = new HashMap<String,List<String>>();
for(Map.Entry<String, String> entry : values.entrySet()) {
if(output.containsKey(entry.getValue())){
output.get(entry.getValue()).add(entry.getKey());
}else{
List<String> list = new ArrayList<String>();
list.add(entry.getKey());
output.put(entry.getValue(),list);
}
}
Run Code Online (Sandbox Code Playgroud)
使用流可以做得更好吗?
Keycloak 策略执行器无法与示例 Sprint 启动应用程序配合使用。
我正在使用 Keycloak 版本 6.0.1 并尝试集成示例 Sprint 启动应用程序(Sprint 启动版本 2.1.3)。我的目标是在 Keycloak 中设置策略和权限,并在示例 Spring boot 应用程序中使用 Keycloak 策略强制执行器,以便使用 Keycloak 中定义的适当权限自动执行所有授权决策,并且示例应用程序中不需要任何代码。
我的示例 spring boot 应用程序仅从内存列表中打印用户列表:
public class JPAUserResource {
@Autowired
private UserRepository userRepo;
@GetMapping(path = "/jpausers")
public List<JPAUser> retrieveAllUsers() {
return userRepo.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 application.properties 文件具有以下内容:
server.port=38080
spring.jpa.show-sql=true
spring.h2.console.enabled=true
logging.level.org.springframework.security=DEBUG
logging.level.org.keycloak.adapters.authorization=DEBUG
#Keycloak Configuration
keycloak.auth-server-url=http://192.168.154.190:18180/auth
keycloak.realm=master
keycloak.resource=login-app
keycloak.principal-attribute=preferred_username
keycloak.credentials.secret=195925d6-b258-407d-a65d-f1fd12d7a876
keycloak.policy-enforcer-config.enforcement-mode=enforcing
keycloak.realm-key=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjyYRe6LxBxO9hVtr4ScsMCBp3aPE9qbJLptPIMQCZR6JhVhOxA1kxhRmVYHXR5pdwiQWU8MriRhAY1JGniG6GNS1+BL+JaUiaGxov4rpD2SIMdrs8YjjSoD3Z8wvsMAopzWG48i9T/ppNaqKTkDZHbHAXOYJn+lymQ4EqpQrJ1Uh+SUA8XcLvWUQ12ty9BieujudWhnAgQ4zxyJY3I8sZwjaRIxndzSlyPJo45lWzXkpqcl92eU/Max7LRM4WKqsUvu86DgqlXbJcz8T+GUeF30ONQDSLX9rwNIT4ZiCVMT7x6YfKXZW6jxC0UiXxZuT23xk8A9iCP4rC9xo1NfGTwIDAQAB
keycloak.policy-enforcer-config.paths[0].path=/jpausers
keycloak.policy-enforcer-config.paths[0].methods[0].method=GET
Run Code Online (Sandbox Code Playgroud)
我的Keycloak授权设置如下:
{
"allowRemoteResourceManagement": true,
"policyEnforcementMode": "ENFORCING",
"resources": [
{
"name": "Default Resource",
"type": "urn:login-app:resources:default",
"ownerManagedAccess": false, …Run Code Online (Sandbox Code Playgroud) 以下是我的用例:我需要向访问令牌添加声明,以便我可以在对我的资源进行策略评估期间使用它。我的策略是基于 javascript 的策略,它只能访问登录用户的保留和自定义属性。我使用以下 api 来推送声明:
curl -X POST \
http://localhost:8082/auth/realms/cms-non-prod/protocol/openid-connect/token \
-H 'Authorization: Bearer eyJhbGciOiJSXXXXXXXXXXXXXXXX' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Postman-Token: ac020c2b-9efb-4817-81ea-61895c8775a7' \
-d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Auma-ticket&claim_token=ewoiaW5zdGl0dXRpb25JZCI6WyJEQ0IiXQp9& claim_token_format=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt&client_id=indra-bff \
&client_Secret=5760582d-74ff-496c-a6c2-2530ddde6408&audience=indra-bff'
Run Code Online (Sandbox Code Playgroud)
它添加了声明,但它添加到授权--> 权限--> 资源。如果我有基于 JS 的政策,我该如何阅读。任何有关此的指示都会有所帮助。下面是我点击上面的 url 时得到的令牌:
{
"jti": "4c00f1a4-8038-4c45-820d-23a9c9ab6d42",
"exp": 1580733917,
"nbf": 0,
"iat": 1580730317,
"iss": "http://localhost:8082/auth/realms/cms-non-prod",
"aud": "indra-bff",
"sub": "9ab2fc80-3a5c-426d-ae78-56de01d214df",
"typ": "Bearer",
"azp": "indra-bff",
"auth_time": 0,
"session_state": "2ab35757-d09d-4d52-946b-f519a1338abf",
"acr": "1",
"realm_access": {
"roles": [
"PR_DCB_RECON_ASSOCIATE",
"PR_YBL_RECON_ASSOCIATE",
"offline_access",
"uma_authorization",
"PR_DCB_RECON_MGR"
]
},
"resource_access": {
"indra-bff": { …Run Code Online (Sandbox Code Playgroud) 我使用ISBNdB来获取有关书籍的信息.响应类型是application/octet-stream.我得到的样本json响应如下所示
{
"index_searched" : "isbn",
"data" : [
{
"publisher_id" : "john_wiley_sons_inc",
"publisher_name" : "John Wiley & Sons, Inc",
"title_latin" : "Java programming interviews exposed",
"language" : "eng",
"summary" : "",
"physical_description_text" : "1 online resource (xvi, 368 pages) :",
"author_data" : [
{
"name" : "Markham, Noel",
"id" : "markham_noel"
},
{
"id" : "greg_milette",
"name" : "Greg Milette"
}
],
"title_long" : "Java programming interviews exposed",
"urls_text" : "",
"publisher_text" : "New York; John Wiley & …Run Code Online (Sandbox Code Playgroud) 我们正在使用 Java 代码将用户导入 KeyCloak,并且我们正在使用keycloak-admin-clientAPI。
尝试如下设置密码,但它没有为用户设置/保留密码。
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("password");
userRepresentation.setCredentials(Arrays.asList(credential));
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来设置用户凭据
keycloak ×3
java ×2
spring-boot ×2
claims ×1
jackson ×1
java-stream ×1
json ×1
jwt ×1
spring-mvc ×1