добавил автодобавление пользователя и методы в Imdg
This commit is contained in:
parent
873a437a82
commit
602c194db7
8 changed files with 183 additions and 2 deletions
|
|
@ -49,6 +49,10 @@
|
|||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ru.spcex.clearing</groupId>
|
||||
<artifactId>classes</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import org.springframework.util.LinkedMultiValueMap;
|
|||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.spcex.clearing.backendapi.service.UserAutoCreateService;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
|
@ -39,13 +40,15 @@ public class KeycloakRestTemplateAuthenticationProvider implements Authenticatio
|
|||
private final HttpServletRequest currentHttpRequest;
|
||||
private final HttpServletResponse currentHttpResponse;
|
||||
private final RestTemplate restTemplate;
|
||||
private final UserAutoCreateService userAutoCreation;
|
||||
|
||||
public KeycloakRestTemplateAuthenticationProvider(KeycloakConfigResolver resolver, HttpServletRequest currentHttpRequest, HttpServletResponse currentHttpResponse,
|
||||
@Qualifier("clearing-rest") RestTemplate restTemplate) {
|
||||
@Qualifier("clearing-rest") RestTemplate restTemplate, UserAutoCreateService userAutoCreation) {
|
||||
this.resolver = resolver;
|
||||
this.currentHttpRequest = currentHttpRequest;
|
||||
this.currentHttpResponse = currentHttpResponse;
|
||||
this.restTemplate = restTemplate;
|
||||
this.userAutoCreation = userAutoCreation;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -83,11 +86,11 @@ public class KeycloakRestTemplateAuthenticationProvider implements Authenticatio
|
|||
RefreshableKeycloakSecurityContext skSession = new RefreshableKeycloakSecurityContext(deployment, null, accessTokenString, accessToken, null, null, refreshTokenString);
|
||||
String principalName = AdapterUtils.getPrincipalName(deployment, accessToken);
|
||||
final KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = new KeycloakPrincipal<>(principalName, skSession);
|
||||
|
||||
final Set<String> roles = AdapterUtils.getRolesFromSecurityContext(skSession);
|
||||
final KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, skSession);
|
||||
KeycloakAuthenticationToken keycloakAuthenticationToken = new KeycloakAuthenticationToken(account, false, realmRoles);
|
||||
keycloakAuthenticationToken.setAuthenticated(true);
|
||||
checkRightsAndCreate(principalName, roles);
|
||||
return keycloakAuthenticationToken;
|
||||
|
||||
} catch (VerificationException vex) {
|
||||
|
|
@ -97,6 +100,13 @@ public class KeycloakRestTemplateAuthenticationProvider implements Authenticatio
|
|||
}
|
||||
}
|
||||
|
||||
private void checkRightsAndCreate(String username, Set<String> roles) {
|
||||
//todo remove hardcode
|
||||
if (roles.contains("admin") || roles.contains("default-roles-master")) {
|
||||
userAutoCreation.createUserIfNeeded(username);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> type) {
|
||||
return UsernamePasswordAuthenticationToken.class.equals(type);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
package ru.spcex.clearing.backendapi.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.clearing.classes.statics.data.user.ClearingUser;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class UserAutoCreateService {
|
||||
|
||||
private final Imdg<ClearingUser> userMap;
|
||||
|
||||
@Autowired
|
||||
public UserAutoCreateService(ImdgProvider imdgProvider) {
|
||||
this.userMap = imdgProvider.getImdg(IMDGDistributedNames.Map_ClearingUser, ClearingUser.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* при необходимости переделать на отдельный поток
|
||||
* todo transactional?
|
||||
*/
|
||||
public void createUserIfNeeded(String userName) {
|
||||
ClearingUser username = userMap.getSingleObjectByFieldValues(Map.of("username", userName));
|
||||
if (username == null) {
|
||||
username = new ClearingUser();
|
||||
username.setUsername(userName);
|
||||
username.setCreated(Instant.now());
|
||||
userMap.insert(username);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,15 @@ import ru.clearing.classes.objects.BusinessObject;
|
|||
*/
|
||||
public class ClearingUser extends BusinessObject {
|
||||
private static final long serialVersionUID = ConstSerializable.serialVersionUID;
|
||||
private String username;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
// private String idpUuid;
|
||||
//
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package ru.spcex.clearing.imdg.object;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.clearing.classes.statics.data.user.ClearingUser;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.imdg.base.AutoconfiguredMap;
|
||||
import ru.spcex.clearing.imdg.base.SimpleObjectMapStore;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
//todo rewrite
|
||||
@Component
|
||||
public class ClearingUserMapStore extends SimpleObjectMapStore<ClearingUser> implements AutoconfiguredMap<ClearingUser> {
|
||||
|
||||
public ClearingUserMapStore(JdbcTemplate jdbcTemplate) {
|
||||
super(jdbcTemplate);
|
||||
//super(jdbcTemplate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableName() {
|
||||
|
||||
return "COMPANY";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getFields() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void store(Map<Long, ClearingUser> map) {}
|
||||
|
||||
@Override
|
||||
public String getMapName() {
|
||||
return IMDGDistributedNames.Map_ClearingUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getIndexingField() {
|
||||
return new String[] {"username"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClearingUser load(Long key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, ClearingUser> loadAll(Collection<Long> keys) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Long> loadAllKeys() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,9 +2,12 @@ package ru.spcex.platform.imdg.iml.hazelcast.adapter;
|
|||
|
||||
import com.hazelcast.core.IMap;
|
||||
import com.hazelcast.core.IdGenerator;
|
||||
import com.hazelcast.query.Predicate;
|
||||
import com.hazelcast.query.Predicates;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
|
||||
import java.util.*;
|
||||
public class ImdgHazelcast<T extends SpcexObjectBase> implements Imdg<T> {
|
||||
private IdGenerator idGenerator;
|
||||
|
||||
|
|
@ -46,4 +49,52 @@ public class ImdgHazelcast<T extends SpcexObjectBase> implements Imdg<T> {
|
|||
public T getSingleObjectByID(Long paramLong) {
|
||||
return map.get(paramLong);
|
||||
}
|
||||
|
||||
/**
|
||||
* возвращает объект удовлетворяющий условиям
|
||||
* не проверяет, если удовлетворяющих условиям > 1
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T getSingleObjectByFieldValues(Map<String, ? extends Comparable<?>> conditions) {
|
||||
Predicate<Long, ?>[] predicates = new Predicate[conditions.size()];
|
||||
final int[] i = {0};
|
||||
conditions.forEach((key, value) -> {
|
||||
predicates[i[0]] = Predicates.equal(key, value);
|
||||
i[0]++;
|
||||
});
|
||||
Predicate<Long, T> or = Predicates.or(predicates);
|
||||
Set<Map.Entry<Long, T>> found = map.entrySet(or);
|
||||
Iterator<Map.Entry<Long, T>> allFoundByCondition = found.iterator();
|
||||
if (allFoundByCondition.hasNext()) {
|
||||
return allFoundByCondition.next().getValue();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* возвращает коллекцию объектов, удовлетворяющий условиям на поля
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Collection<T> getCollectionObjectsByFieldValues(Map<String, ? extends Comparable<?>> conditions) {
|
||||
Predicate<Long, ?>[] predicates = new Predicate[conditions.size()];
|
||||
final int[] i = {0};
|
||||
conditions.forEach((key, value) -> {
|
||||
predicates[i[0]] = Predicates.equal(key, value);
|
||||
i[0]++;
|
||||
});
|
||||
Predicate<Long, T> or = Predicates.or(predicates);
|
||||
Set<Long> ids = map.keySet(or);
|
||||
Iterator<Long> idIterator = ids.iterator();
|
||||
Collection<T> searchResult = new ArrayList<>();
|
||||
while(idIterator.hasNext()) {
|
||||
T element = map.get(idIterator.next());
|
||||
if (element != null) {
|
||||
searchResult.add(element);
|
||||
}
|
||||
}
|
||||
return searchResult;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ public final class IMDGDistributedNames {
|
|||
public static final String Map_SDf08 = "Map_SDf08";
|
||||
public static final String Map_SDf02 = "Map_SDf02";
|
||||
public static final String Map_SDf01 = "Map_SDf01";
|
||||
public static final String Map_ClearingUser = "Map_ClearingUser";
|
||||
|
||||
// public static final String Map_InterestStatusDictionary = "Map_InterestStatusDictionary"; future
|
||||
public static final String Map_AllowedDictionary = "Map_AllowedDictionary";
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ru.spcex.platform.imdg.api;
|
|||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Imdg<T extends SpcexObjectBase> {
|
||||
default void insert(T paramT) {
|
||||
|
|
@ -29,6 +30,14 @@ public interface Imdg<T extends SpcexObjectBase> {
|
|||
throw new UnsupportedOperationException("not implemented getSingleObjectBySQL");
|
||||
}
|
||||
|
||||
default T getSingleObjectByFieldValues(Map<String, ? extends Comparable<?>> conditions) {
|
||||
throw new UnsupportedOperationException("not implemented getSingleObjectByFieldValues");
|
||||
}
|
||||
|
||||
default Collection<T> getCollectionObjectsByFieldValues(Map<String, ? extends Comparable<?>> conditions) {
|
||||
throw new UnsupportedOperationException("not implemented getCollectionObjectsByFieldValues");
|
||||
}
|
||||
|
||||
default T getSingleObjectByID(Long paramLong) {
|
||||
throw new UnsupportedOperationException("not implemented getSingleObjectByID");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue