首页 > 解决方案 > Tomcat DataSourceRealm 未显示任何日志记录

问题描述

我正在使用 Jersey3 和 Tomcat10 在 Java 中实现 REST-API。为了为特定角色提供/限制对某些端点的访问,我想使用DataSourceRealm.

我的配置context.xml是这样的:

<Resource type="javax.sql.DataSource"
              name="jdbc/myDB"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@//localhost:1521/orcl"
              username="xx"
              password="xx"/>

<Realm className="org.apache.catalina.realm.DataSourceRealm" debug="99"
           dataSourceName="jdbc/myDB" localDataSource="true"
           userTable="USERS" userNameCol="USERNAME" userCredCol="PASSWORD"
           userRoleTable="USER_ROLES" roleNameCol="ROLENAME"/>

也在我的dataSourcecontext.xml 中定义,可以使用 JNDI 访问。

在我的web.xml我定义了以下安全约束:

<security-constraint>
        <web-resource-collection>
            <web-resource-name>Some name</web-resource-name>
            <url-pattern>/api/data</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>Administrator</role-name>
        </auth-constraint>
</security-constraint>

所以每个GET带有模式/api/data的请求都需要角色Administrator

USER_ROLES有一个带有USERNAME=admin和的条目ROLENAME=Administrator

另外,我添加了这些行:

<login-config>
        <auth-method>BASIC</auth-method>
    </login-config>

    <security-role>
        <role-name>Administrator</role-name>
    </security-role>

为了做显而易见的事情。

相应的端点定义如下:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/data")
    @RolesAllowed({"Administrator"})
    public Response getData(...)

将这些行添加到我的日志配置中:

org.apache.catalina.realm.level = ALL
org.apache.catalina.realm.useParentHandlers = true
org.apache.catalina.authenticator.level = ALL
org.apache.catalina.authenticator.useParentHandlers = true

在带有身份验证标头的 GET 中调用 http://localhost:8080/myapp/api/data Basic YWRtaW46YWRtaW4=--> admin:admin

到目前为止,限制访问是可行的,我的DataSourceRealm似乎已被考虑在内。尽管没有人在进行身份验证时的日志。

为了查看来自 DataSourceRealm 的日志,我需要添加什么?任何帮助/输入将不胜感激。

标签: javaauthenticationtomcatloggingjersey

解决方案


推荐阅读