Search in sources :

Example 51 with Value

use of org.springframework.beans.factory.annotation.Value in project uPortal by Jasig.

the class IdTokenFactory method createUserInfo.

public String createUserInfo(String username, Set<String> claimsToInclude, Set<String> groupsToInclude) {
    final Date now = new Date();
    final Date expires = new Date(now.getTime() + (timeoutSeconds * 1000L));
    final JwtBuilder builder = Jwts.builder().setIssuer(issuer).setSubject(username).setAudience(issuer).setExpiration(expires).setIssuedAt(now);
    final IPersonAttributes person = personAttributeDao.getPerson(username);
    // Attribute mappings
    mappings.stream().filter(mapping -> includeClaim(mapping.getClaimName(), claimsToInclude)).forEach(item -> {
        final Object value = person.getAttributeValue(item.getAttributeName());
        if (value != null) {
            builder.claim(item.getClaimName(), item.getDataTypeConverter().convert(value));
        }
    });
    // Groups
    final List<String> groups = new ArrayList<>();
    final IGroupMember groupMember = GroupService.getGroupMember(username, IPerson.class);
    if (groupMember != null) {
        final Set<IEntityGroup> ancestors = groupMember.getAncestorGroups();
        for (IEntityGroup g : ancestors) {
            if (includeGroup(g, groupsToInclude)) {
                groups.add(g.getName());
            }
        }
    }
    if (!groups.isEmpty()) {
        /*
             * If a Claim is not returned, that Claim Name SHOULD be omitted from the JSON object
             * representing the Claims; it SHOULD NOT be present with a null or empty string value.
             */
        builder.claim("groups", groups);
    }
    // Default custom claims defined by uPortal.properties
    customClaims.stream().filter(claimName -> includeClaim(claimName, claimsToInclude)).map(attributeName -> new CustomClaim(attributeName, person.getAttributeValues(attributeName))).filter(claim -> claim.getClaimValue() != null).forEach(claim -> builder.claim(claim.getClaimName(), claim.getClaimValue()));
    final String rslt = builder.signWith(algorithmFactory.getAlgorithm(), signatureKey).compact();
    logger.debug("Produced the following JWT for username='{}':  {}", username, rslt);
    return rslt;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) IEntityGroup(org.apereo.portal.groups.IEntityGroup) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Headers(org.apereo.portal.soffit.Headers) StringUtils(org.apache.commons.lang3.StringUtils) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) Claims(io.jsonwebtoken.Claims) HashSet(java.util.HashSet) JwtSignatureAlgorithmFactory(org.apereo.portal.soffit.service.JwtSignatureAlgorithmFactory) Value(org.springframework.beans.factory.annotation.Value) BigDecimal(java.math.BigDecimal) Jws(io.jsonwebtoken.Jws) HttpServletRequest(javax.servlet.http.HttpServletRequest) GroupService(org.apereo.portal.services.GroupService) JwtEncryptor(org.apereo.portal.soffit.service.JwtEncryptor) JwtBuilder(io.jsonwebtoken.JwtBuilder) IPerson(org.apereo.portal.security.IPerson) AbstractJwtService(org.apereo.portal.soffit.service.AbstractJwtService) IPersonAttributes(org.apereo.services.persondir.IPersonAttributes) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) Jwts(io.jsonwebtoken.Jwts) PostConstruct(javax.annotation.PostConstruct) IGroupMember(org.apereo.portal.groups.IGroupMember) Collections(java.util.Collections) IEntityGroup(org.apereo.portal.groups.IEntityGroup) IGroupMember(org.apereo.portal.groups.IGroupMember) IPersonAttributes(org.apereo.services.persondir.IPersonAttributes) ArrayList(java.util.ArrayList) JwtBuilder(io.jsonwebtoken.JwtBuilder) Date(java.util.Date)

Example 52 with Value

use of org.springframework.beans.factory.annotation.Value in project leopard by tanhaichao.

the class SysconfigBeanPostProcessor method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    // System.err.println("postProcessBeforeInitialization:" + beanName);
    Class<?> clazz = bean.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        Value annotation = field.getAnnotation(Value.class);
        if (annotation == null) {
            continue;
        }
        field.setAccessible(true);
        Object value = resolveValue(annotation, field);
        if (value == null) {
            continue;
        }
        try {
            field.set(bean, value);
            FieldInfo fieldInfo = new FieldInfo();
            fieldInfo.setBean(bean);
            fieldInfo.setField(field);
            fieldList.add(fieldInfo);
        } catch (IllegalAccessException e) {
            throw new BeanInstantiationException(clazz, e.getMessage(), e);
        }
    }
    return bean;
}
Also used : Field(java.lang.reflect.Field) Value(org.springframework.beans.factory.annotation.Value) BeanInstantiationException(org.springframework.beans.BeanInstantiationException)

Example 53 with Value

use of org.springframework.beans.factory.annotation.Value in project leopard by tanhaichao.

the class SysconfigBeanPostProcessor method get.

@Override
public SysconfigVO get() {
    SysconfigVO sysconfigVO = new SysconfigVO();
    List<FieldVO> fieldVOList = new ArrayList<FieldVO>();
    sysconfigVO.setFieldList(fieldVOList);
    for (FieldInfo fieldInfo : fieldList) {
        Field field = fieldInfo.getField();
        Value annotation = field.getAnnotation(Value.class);
        Object value;
        try {
            value = field.get(fieldInfo.getBean());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        String sysconfigId = annotation.value().replace("${", "").replace("}", "");
        FieldVO fieldVO = new FieldVO();
        fieldVO.setSysconfigId(sysconfigId);
        fieldVO.setValue(value);
        fieldVOList.add(fieldVO);
    }
    sysconfigVO.setLmodify(lmodify);
    return sysconfigVO;
}
Also used : FieldVO(io.leopard.sysconfig.viewer.FieldVO) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) SysconfigVO(io.leopard.sysconfig.viewer.SysconfigVO)

Example 54 with Value

use of org.springframework.beans.factory.annotation.Value in project CzechIdMng by bcvsolutions.

the class ClusteredEhCacheConfiguration method ehCacheManager.

/**
 * Defines clustered {@link CacheManager} using Terracotta server.
 *
 * @param terracotaUrl a list of IP addresses with ports (IP_ADDR:PORT)
 * @param terracotaResourceName name of server resource to connect
 * @param terracotaResourcePoolName name od server resource pool name
 * @param terracotaResourcePoolSize size of server resource pool in MB
 * @param idMCacheConfigurations a list of {@link IdMCacheConfiguration} defined in container
 * @return CacheManager with distributed capabilities
 */
@Bean
@Qualifier("jCacheManager")
@ConditionalOnProperty(value = TERRACOTA_URL_PROPERTY)
@ConditionalOnMissingBean
public CacheManager ehCacheManager(@Value("${" + TERRACOTA_URL_PROPERTY + "}") String terracotaUrl, @Value("${" + TERRACOTA_RESOURCE_NAME_PROPERTY + "}") String terracotaResourceName, @Value("${" + TERRACOTA_RESOURCE_POOL_NAME_PROPERTY + "}") String terracotaResourcePoolName, @Value("${" + TERRACOTA_RESOURCE_POOL_SIZE_PROPERTY + "}") int terracotaResourcePoolSize, @Autowired List<IdMCacheConfiguration> idMCacheConfigurations) {
    CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = CacheManagerBuilder.newCacheManagerBuilder().with(ClusteringServiceConfigurationBuilder.cluster(parseServerAddresses(terracotaUrl), "default").autoCreate(server -> server.defaultServerResource(terracotaResourceName).resourcePool(terracotaResourcePoolName, terracotaResourcePoolSize, MemoryUnit.MB, terracotaResourceName))).withSerializer(CacheObjectWrapper.class, CacheWrapperSerializer.class).withSerializer(SerializableCacheObjectWrapper.class, SerializableCacheWrapperSerializer.class);
    PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true);
    // create caches using IdMCacheConfiguration instances
    if (!CollectionUtils.isEmpty(idMCacheConfigurations)) {
        for (IdMCacheConfiguration config : idMCacheConfigurations) {
            cacheManager.createCache(config.getCacheName(), toConcreteConfiguration(config, terracotaResourcePoolName));
        }
    }
    // get CacheManager (Jcache) with above updated configuration
    final EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
    return ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), cacheManager.getRuntimeConfiguration());
}
Also used : Arrays(java.util.Arrays) ExpiryPolicyBuilder(org.ehcache.config.builders.ExpiryPolicyBuilder) ResourcePoolsBuilder(org.ehcache.config.builders.ResourcePoolsBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheConfiguration(org.ehcache.config.CacheConfiguration) Value(org.springframework.beans.factory.annotation.Value) ClusteringServiceConfigurationBuilder(org.ehcache.clustered.client.config.builders.ClusteringServiceConfigurationBuilder) EhcacheCachingProvider(org.ehcache.jsr107.EhcacheCachingProvider) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Order(org.springframework.core.annotation.Order) MemoryUnit(org.ehcache.config.units.MemoryUnit) SerializableCacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.SerializableCacheObjectWrapper) CacheManagerBuilder(org.ehcache.config.builders.CacheManagerBuilder) Caching(javax.cache.Caching) IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration) PersistentCacheManager(org.ehcache.PersistentCacheManager) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ClusteredResourcePoolBuilder(org.ehcache.clustered.client.config.builders.ClusteredResourcePoolBuilder) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) CacheConfigurationBuilder(org.ehcache.config.builders.CacheConfigurationBuilder) CollectionUtils(org.springframework.util.CollectionUtils) CacheManager(javax.cache.CacheManager) Bean(org.springframework.context.annotation.Bean) CacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.CacheObjectWrapper) SerializableCacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.SerializableCacheObjectWrapper) CacheObjectWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.CacheObjectWrapper) PersistentCacheManager(org.ehcache.PersistentCacheManager) IdMCacheConfiguration(eu.bcvsolutions.idm.core.api.config.cache.IdMCacheConfiguration) EhcacheCachingProvider(org.ehcache.jsr107.EhcacheCachingProvider) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 55 with Value

use of org.springframework.beans.factory.annotation.Value in project ArachneCentralAPI by OHDSI.

the class AntivirusServiceImpl method processRequest.

@EventListener
@Async(value = "antivirusScanExecutor")
public void processRequest(AntivirusJobEvent event) {
    final AntivirusJob antivirusJob = event.getAntivirusJob();
    final AntivirusJobFileType fileType = antivirusJob.getAntivirusJobFileType();
    final Long fileId = antivirusJob.getFileId();
    logger.debug(PROCESSING_SCAN_REQUEST, fileId, fileType);
    String description = null;
    AntivirusStatus status;
    try (InputStream content = antivirusJob.getContent()) {
        clamavClientAvailabilityCheck();
        final ScanResult scan = retryTemplate.execute((RetryCallback<ScanResult, Exception>) retryContext -> {
            logger.debug(PROCESSING_SCAN_ATTEMPT, fileId, fileType);
            return scan(content);
        });
        if (scan instanceof ScanResult.OK) {
            status = AntivirusStatus.OK;
        } else {
            status = AntivirusStatus.INFECTED;
            description = scan.toString();
        }
    } catch (Exception e) {
        logger.error("Error scanning file: {}", e.getMessage());
        if (e instanceof ClamavException) {
            final Throwable cause = e.getCause();
            description = cause.getMessage();
        } else {
            description = e.getMessage();
        }
        status = AntivirusStatus.NOT_SCANNED;
    }
    logger.debug(PROCESSING_SCAN_RESULT, fileId, fileType, status);
    publishResponse(fileType, fileId, status, description);
}
Also used : CommunicationException(xyz.capybara.clamav.CommunicationException) Async(org.springframework.scheduling.annotation.Async) LoggerFactory(org.slf4j.LoggerFactory) AntivirusJobResponse(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) ScanResult(xyz.capybara.clamav.commands.scan.result.ScanResult) AntivirusStatus(com.odysseusinc.arachne.portal.model.AntivirusStatus) AntivirusJobStudyFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobStudyFileResponseEvent) Value(org.springframework.beans.factory.annotation.Value) AntivirusJobEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobEvent) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ClamavException(xyz.capybara.clamav.ClamavException) AntivirusJobPaperProtocolFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobPaperProtocolFileResponseEvent) Logger(org.slf4j.Logger) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) AntivirusJobAnalysisFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobAnalysisFileResponseEvent) EventListener(org.springframework.context.event.EventListener) AntivirusJobPaperPaperFileResponseEvent(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobPaperPaperFileResponseEvent) AntivirusJobResponseEventBase(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobResponseEventBase) ClamavClient(xyz.capybara.clamav.ClamavClient) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) RetryCallback(org.springframework.retry.RetryCallback) RetryTemplate(org.springframework.retry.support.RetryTemplate) InputStream(java.io.InputStream) AntivirusJob(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJob) ScanResult(xyz.capybara.clamav.commands.scan.result.ScanResult) AntivirusJobFileType(com.odysseusinc.arachne.portal.service.impl.antivirus.events.AntivirusJobFileType) InputStream(java.io.InputStream) AntivirusStatus(com.odysseusinc.arachne.portal.model.AntivirusStatus) CommunicationException(xyz.capybara.clamav.CommunicationException) ClamavException(xyz.capybara.clamav.ClamavException) ClamavException(xyz.capybara.clamav.ClamavException) Async(org.springframework.scheduling.annotation.Async) EventListener(org.springframework.context.event.EventListener)

Aggregations

Value (org.springframework.beans.factory.annotation.Value)77 Autowired (org.springframework.beans.factory.annotation.Autowired)36 Collectors (java.util.stream.Collectors)34 IOException (java.io.IOException)30 List (java.util.List)29 Logger (org.slf4j.Logger)23 LoggerFactory (org.slf4j.LoggerFactory)23 PathVariable (org.springframework.web.bind.annotation.PathVariable)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ArrayList (java.util.ArrayList)21 Map (java.util.Map)21 RequestParam (org.springframework.web.bind.annotation.RequestParam)21 Optional (java.util.Optional)19 PostConstruct (javax.annotation.PostConstruct)19 RestController (org.springframework.web.bind.annotation.RestController)19 Set (java.util.Set)18 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)18 HttpStatus (org.springframework.http.HttpStatus)17 ApiOperation (io.swagger.annotations.ApiOperation)16 HttpServletResponse (javax.servlet.http.HttpServletResponse)16