有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用tomcat领域特性连接到mySql数据库并在JavaRESTAPI中管理身份验证和授权

我正试图使用ApacheTomcat领域feture,如下所述:https://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html

在我的mySql中有两个表:角色和用户。我也有他们之间的正确关系

在我的JavaRESTAPI(eclipse中的一个动态web项目)中,我将这几行代码添加到web中。xml:

<security-constraint>
        <web-resource-collection>
            <web-resource-name>rest</web-resource-name>
            <url-pattern>/rest/*</url-pattern>  
        </web-resource-collection>
        <auth-constraint>
            <role-name>manager</role-name>
        </auth-constraint>
 </security-constraint>
   <login-config>
    <auth-method>BASIC</auth-method>
  </login-config>

我决定使用JDBCRealm方法来支持sql服务器的领域,所以我遵循了一些示例,并将这些行添加到服务器中。位于mypathtoapache/conf文件夹中的xml文件:

<Realm  className="org.apache.catalina.realm.JDBCRealm"
             driverName="com.mysql.jdbc.Driver"
          connectionURL="jdbc:mysql://localhost:3306/TradingSstemDB"
         connectionName="realm_access" connectionPassword="realmpass"
              userTable="users" userNameCol="user_name" userCredCol="password"
          userRoleTable="users_roles" roleNameCol="role_name" />

我还复制了mysql-connector-java-5.1.40-bin。jar到mypathtoapache/lib

现在,我启动了java服务器并尝试访问http://localhost:8080/TradingSystemServer/rest/test,它只返回一条OK消息。 比“需要授权”弹出窗口显示,并要求我插入用户名和密码。然而,即使我放置了有效的凭据,它仍然不起作用。相反,它会不断提示我弹出

有什么不对劲吗?我如何调试这个领域特性


共 (0) 个答案