Tomcat Realm: Not able to set realm for my web application

*爱你&永不变心* 提交于 2019-12-25 08:13:25

问题


I am trying to set Tomcat JDBC Realm for my Testing web application. I am following the standard documentation, https://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#JDBCRealm.

My server.xml is as following:

<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

        <!-- Default Realm -->
        <Realm className="org.apache.catalina.realm.LockOutRealm">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
        </Realm>

        <!-- Mine implementation -->
        <Realm className="org.apache.catalina.realm.JDBCRealm"
               driverName="com.mysql.jdbc.Driver"
               connectionURL="jdbc:mysql://localhost/dbSecurity?user=root&amp;password=root"
               userTable="users"
               userNameCol="user_name"
               userCredCol="user_pass"
               userRoleTable="user_roles"
               roleNameCol="role_name"/>

        <Host name="localhost"  appBase="webapps"
              unpackWARs="true" autoDeploy="true">
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        </Host>
    </Engine>
</Service>

I didn't made any changes in my application. What other changes I need to do to get it working?

###### EDITED

I tried context.xml in application, that didn't work either.

src/main/webapp/META-INF/context.xml

<Context>
    <Resource
            name="jdbc/dbSecurity"
            auth="Container"
            type="javax.sql.DataSource"
            removeAbandoned="true"
            removeAbandonedTimeout="15"
            maxActive="5"
            maxIdle="5"
            maxWait="7000"
            username="root"
            password="root"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost/dbSecurity?autoReconnectForPools=true&amp;characterEncoding=UTF-8"
            validationQuery="SELECT '1';"
            testOnBorrow="true"/>
    <Realm className="org.apache.catalina.realm.JDBCRealm"
           driverName="com.mysql.jdbc.Driver"
           connectionName="root"
           connectionPassword="root"
           connectionURL="jdbc:mysql://localhost/dbSecurity?autoReconnectForPools=true&amp;characterEncoding=UTF-8"
           userTable="users"
           userNameCol="user_name"
           userCredCol="user_pass"
           userRoleTable="user_roles"
           roleNameCol="role_name"/>
</Context>

My webapp/WEB-INF/web.xml is as given:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.restroshop</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

I have a jersey RESTful resource :

@Path("myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

And an index.jsp

<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/myresource">Jersey resource</a>
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
    for more information on Jersey!
<div ng-app="app">
    <div ng-controller="appCtrl">{{greet}}</div>
    <div ng-controller="orderCtrl">{{greet}}</div>
</div>
<script src="app/bower_components/angular/angular.js"></script>
<script src="app/app.module.js"></script>
<script src="app/app.controller.js"></script>
<script src="app/Orders/orders.module.js"></script>
<script src="app/Orders/orders.controller.js"></script>
</body>
</html>

GitHub Repository link

来源:https://stackoverflow.com/questions/39715453/tomcat-realm-not-able-to-set-realm-for-my-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!