Maven tomcat-plugin cannot find custom realm

170 views Asked by At

I am beginner for custom realm. I am trying to create a demo using Maven and CustomRealm.

When i run the maven project, i got ClassNotFoundException for custom relam class.

Below is code snippet.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.realm</groupId>
    <artifactId>RealmDemo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>RealmDemo Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>7.0.47</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>RealmDemo</finalName>
    </build>
</project>

Custom Realm Class

public class TestRealm extends RealmBase {

    @Override
    protected String getName() {
        System.out.println("Get Name");
        return null;
    }
    @Override
    protected String getPassword(String arg0) {
        System.out.println("Get Password for "+arg0);
        return null;
    }
    @Override
    protected Principal getPrincipal(String arg0) {
        System.out.println("Get Principal for "+arg0);
        return null;
    }
}

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>RealmDemo</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>TestRealm</servlet-name>
        <servlet-class>com.test.TestRealm</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestRealm</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
    <security-role>
        <role-name>admin</role-name>
    </security-role>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>
                Entire Application
            </web-resource-name>
            <url-pattern>/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>  
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method> 
        <form-login-config>
            <form-login-page>/login/login.html</form-login-page>
            <form-error-page>/login/error.html</form-error-page>
        </form-login-config>
    </login-config>
</web-app>

I have a context.xml file into META-INF folder which has realm config as below

/META-INF/context.xml

<Context>
    <Realm className="com.test.TestRealm" />
</Context>

When i run this project using mvn clean install tomcat:run i get below exception

Caused by: java.lang.ClassNotFoundException: com.test.TestRealm

I am searching for the solution over google since last 2 days but didn't get any proper solution for this.

Please guide.

Thanks

0

There are 0 answers