Tomcat Realm:无法为我的Web应用程序设置领域

时间:2016-09-27 03:20:06

标签: tomcat tomcat8 jdbcrealm

我正在尝试为我的测试Web应用程序设置Tomcat JDBC Realm。我遵循标准文档https://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#JDBCRealm

我的server.xml如下:

<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>

我没有对我的申请进行任何更改。 我还需要做些什么改变才能让它发挥作用?

######已编辑

我在应用程序中尝试了context.xml,但也没有用。

的src /主/ web应用/ 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>

我的webapp / WEB-INF / web.xml如下:

<?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>

我有一件运动衫RESTful资源:

@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!";
    }
}

还有一个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 链路

0 个答案:

没有答案