简体   繁体   English

注入在 SecurityRealm-Bean 中不起作用

[英]Injection not working in SecurityRealm-Bean

I'm trying to implement a custom security realm which access a database for user validation.我正在尝试实现自定义安全 realm 访问数据库以进行用户验证。 I want to inject a configured database from the datasource-module.我想从数据源模块注入一个配置的数据库。 It seems that no dependency injections is working as it also failed to inject a ContextService.似乎没有依赖注入起作用,因为它也未能注入 ContextService。 My reduced security realm:我降低的安全性 realm:

import java.security.Principal;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Map;
import java.util.function.Consumer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;

import org.wildfly.extension.elytron.Configurable;
import org.wildfly.security.auth.SupportLevel;
import org.wildfly.security.auth.realm.CacheableSecurityRealm;
import org.wildfly.security.auth.server.RealmIdentity;
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.credential.Credential;
import org.wildfly.security.evidence.Evidence;
import org.wildfly.security.evidence.PasswordGuessEvidence;

@Stateless
public class ExampleRealm implements CacheableSecurityRealm, Configurable
{
    @Resource(name = "java:jboss/datasources/ExampleDS")
    private DataSource ds;

    public ExampleRealm()
    {
        // nothing
    }

    @PostConstruct
    public void init()
    {
        System.out.println("init CDI DemoBean");
    }

    @Override
    public void initialize(final Map<String, String> map)
    {
        System.out.println("init " + ds);
    }

    @Override
    public void registerIdentityChangeListener(final Consumer<Principal> cnsmr)
    {
        // nothing
    }

    @Override
    public SupportLevel getCredentialAcquireSupport(final Class<? extends Credential> credentialType, final String algorithmName,
            final AlgorithmParameterSpec parameterSpec) throws RealmUnavailableException
    {
        return SupportLevel.UNSUPPORTED;
    }

    @Override
    public SupportLevel getEvidenceVerifySupport(final Class<? extends Evidence> evidenceType, final String algorithmName) throws RealmUnavailableException
    {
        return PasswordGuessEvidence.class.isAssignableFrom(evidenceType) ? SupportLevel.POSSIBLY_SUPPORTED : SupportLevel.UNSUPPORTED;
    }

    @Override
    public RealmIdentity getRealmIdentity(final Principal principal) throws RealmUnavailableException
    {
        System.out.println("getIdentity " + ds);

        //omitted

        return RealmIdentity.NON_EXISTENT;
    }
}

I tried different dependencies in the module.xml.我在 module.xml 中尝试了不同的依赖项。 Current module.xml:当前模块.xml:

<?xml version='1.0' encoding='UTF-8'?>

<module xmlns="urn:jboss:module:1.1" name="prototype.webAuth.providers">

    <resources>
        <resource-root path="loginProviders-1.0.0.jar"/>
    </resources>

    <dependencies>
        <module name="org.wildfly.security.elytron"/>
        <module name="org.wildfly.extension.elytron"/>
        <module name="javax.api"/>
        <module name="javax.annotation.api"/>
        <module name="javax.ejb.api"/>
        <module name="javax.resource.api"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.inject.api"/>
        <module name="javax.interceptor.api"/>
        <module name="javax.validation.api"/>
        <module name="org.hibernate.validator"/>
        <module name="javax.xml.stream.api"/>
    </dependencies>
</module>

The datasource is available in the standalone.xml and can be injected and accessed in a servlet which is cointained in a deployed war-file.该数据源在standalone.xml 中可用,并且可以在部署的war 文件中包含的servlet 中注入和访问。 But in the security realm it is null.但在安全 realm 中,它是 null。

The jar is created with maven and packaging ejb. jar 是用 maven 和包装 ejb 创建的。 Any advises?有什么建议吗?

You are trying to define a security realm as an EJB which is not supported.您正在尝试将安全 realm 定义为不受支持的 EJB。 The security realm classes are not deployed as EJBs.安全 realm 类未部署为 EJB。 Also no other custom Elytron components are supported as CDI beans.也不支持其他自定义 Elytron 组件作为 CDI bean。

If you want to define components that are CDI based you can maybe use a standard Jakarta EE security APIs that allow you to define an authentication mechanism and identity store as CDI beans.如果您想定义基于 CDI 的组件,您可以使用标准的 Jakarta EE 安全 API,它允许您将身份验证机制和身份存储定义为 CDI bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM