使用WildFly自定义Web应用程序身份验证

时间:2016-10-07 08:15:46

标签: java wildfly-9 undertow

我需要为在WildFly 9服务器上运行的Java EE应用程序创建自定义Web应用程序身份验证。我在the undertow website documentation找到了很少有关如何操作的信息。

到目前为止,我所做的是创建一个io.undertow.servlet.ServletExtension实现:

package mypackage;

import java.util.Map;
import javax.servlet.ServletContext;
import org.jboss.as.domain.http.server.security.AnonymousMechanism;
import io.undertow.security.api.AuthenticationMechanism;
import io.undertow.security.api.AuthenticationMechanismFactory;
import io.undertow.server.handlers.form.FormParserFactory;
import io.undertow.servlet.ServletExtension;
import io.undertow.servlet.api.DeploymentInfo;

public class MyServletExtension implements ServletExtension {

    @Override
    public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
        deploymentInfo.addAuthenticationMechanism("MY-AUTH", new AuthenticationMechanismFactory() {
            @Override
            public AuthenticationMechanism create(String mechanismName, FormParserFactory formParserFactory, Map<String, String> properties) {
                return new AnonymousMechanism();
            }
        });
    }
}

然后我在src / main / resources / META-INF / services /中添加了一个名为io.undertow.servlet.ServletExtension的文件,内容为:mypackage.MyServletExtension

最后,我修改了我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <display-name>my-app</display-name>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>restricted resources</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>

    <security-role>
        <role-name>*</role-name>
    </security-role>

    <login-config>
        <auth-method>MY-AUTH</auth-method>
    </login-config>

</web-app>

在我的pom.xml文件中,我有这些依赖项:

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-core</artifactId>
    <version>1.2.9.Final</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-servlet</artifactId>
    <version>1.2.9.Final</version>
    <scope>provided</scope>
</dependency>

但是在服务器启动时我有这个错误:

09:40:20,192 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 27) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./my-app-web: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./my-app-web: java.lang.LinkageError: Failed to link mypackage/MyServletExtension (Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_60]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_60]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_60]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_60]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]
    at org.jboss.threads.JBossThread.run(JBossThread.java:320) [jboss-threads-2.2.0.Final.jar:2.2.0.Final]
Caused by: java.lang.LinkageError: Failed to link mypackage/MyServletExtension (Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader)
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:437) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ModuleClassLoader.loadClassLocal(ModuleClassLoader.java:269) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ModuleClassLoader$1.loadClassLocal(ModuleClassLoader.java:77) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.Module.loadModuleClass(Module.java:560) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:197) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130) [jboss-modules.jar:1.4.3.Final]
    at java.lang.Class.forName0(Native Method) [rt.jar:1.8.0_60]
    at java.lang.Class.forName(Class.java:348) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:370) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480) [rt.jar:1.8.0_60]
    at io.undertow.servlet.core.DeploymentManagerImpl.handleExtensions(DeploymentManagerImpl.java:243)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:150)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    ... 6 more
Caused by: java.lang.NoClassDefFoundError: io/undertow/servlet/ServletExtension
    at java.lang.ClassLoader.defineClass1(Native Method) [rt.jar:1.8.0_60]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760) [rt.jar:1.8.0_60]
    at org.jboss.modules.ModuleClassLoader.doDefineOrLoadClass(ModuleClassLoader.java:353) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ModuleClassLoader.defineClass(ModuleClassLoader.java:432) [jboss-modules.jar:1.4.3.Final]
    ... 23 more
Caused by: java.lang.ClassNotFoundException: io.undertow.servlet.ServletExtension from [Module "deployment.my-app-0.0.1-SNAPSHOT.ear.my-app-ejb.jar:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385) [jboss-modules.jar:1.4.3.Final]
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130) [jboss-modules.jar:1.4.3.Final]
    ... 27 more

当我将依赖关系的范围从提供更改为编译时,我有另一个例外:

10:08:01,387 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 29) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./my-app-web: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./my-app-web: java.util.ServiceConfigurationError: io.undertow.servlet.ServletExtension: Provider mypackage.MyServletExtension not a subtype
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_60]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_60]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_60]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_60]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]
    at org.jboss.threads.JBossThread.run(JBossThread.java:320) [jboss-threads-2.2.0.Final.jar:2.2.0.Final]
Caused by: java.util.ServiceConfigurationError: io.undertow.servlet.ServletExtension: Provider mypackage.MyServletExtension not a subtype
    at java.util.ServiceLoader.fail(ServiceLoader.java:239) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404) [rt.jar:1.8.0_60]
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480) [rt.jar:1.8.0_60]
    at io.undertow.servlet.core.DeploymentManagerImpl.handleExtensions(DeploymentManagerImpl.java:243)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:150)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
    ... 6 more

有人可以告诉我配置有什么问题吗?

1 个答案:

答案 0 :(得分:1)

您已将其打包在一个ejb-jar中,默认情况下它不会依赖于下载。

您需要添加

Dependencies: io.undertow.servlet io.undertow.core

到META-INF / MANIFEST.MF(或使用jboss-deployment-structure.xml或将其打包在默认情况下获取这些依赖关系的war中。)