java.security.AccessControlException:访问被拒绝(“java.util.PropertyPermission”“com.mysql.cj.disableAbandonedConnectionCleanup”“读取”)

问题描述 投票:0回答:2

我尝试使用 JDBC 连接到我的 Web 项目中的数据库,但它抛出了

错误:

java.security.AccessControlException: access denied ("java.util.PropertyPermission" "com.mysql.cj.disableAbandonedConnectionCleanup" "read")
    at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:488)
    at java.base/java.security.AccessController.checkPermission(AccessController.java:1071)
    at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:411)
    at java.base/java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1146)
    at java.base/java.lang.System.getProperty(System.java:966)
    at java.base/java.lang.Boolean.getBoolean(Boolean.java:283)
    at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:57)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:421)
    at java.base/java.lang.Class.forName(Class.java:412)
    at com.mysql.cj.jdbc.NonRegisteringDriver.<clinit>(NonRegisteringDriver.java:77)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:421)
    at java.base/java.lang.Class.forName(Class.java:412)
    at com.server.Register.doPost(Register.java:29)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.apache.catalina.security.SecurityUtil.lambda$execute$0(SecurityUtil.java:223)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
    at java.base/javax.security.auth.Subject.doAsPrivileged(Subject.java:670)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:251)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:143)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.lambda$doFilter$0(ApplicationFilterChain.java:124)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:571)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:123)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.apache.catalina.security.SecurityUtil.lambda$execute$0(SecurityUtil.java:223)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
    at java.base/javax.security.auth.Subject.doAsPrivileged(Subject.java:670)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:251)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:203)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.ApplicationFilterChain.lambda$doFilter$0(ApplicationFilterChain.java:124)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:571)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:123)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:663)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:389)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1741)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1190)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
    at java.base/java.lang.Thread.run(Thread.java:1583)

我尝试使用 chatgpt 解决此异常,我使用了 chatgpt 中的代码片段,但没有用。

System.setProperty("com.mysql.cj.disableAbandonedConnectionCleanup", "true");

使用:

  • JDK 21
  • Tomcat v10.1 服务器
package com.server;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@WebServlet("/Register")
public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String sql = "insert into login_credentials values(?,?,?)";
        String url = "jdbc:mysql://localhost:3306/personal_finance";
        String user_name = "root";
        String dpassword = "Pavan@999";
        String email = request.getParameter("email");
        String user_id = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(email + " " + user_id + " " + password);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            Connection con = DriverManager.getConnection(url,user_name,dpassword);
            PreparedStatement smt = con.prepareStatement(sql);
            smt.setString(1, user_id);
            smt.setString(2, email);
            smt.setString(3, password);
            int i = smt.executeUpdate();
            System.out.print(i + " " + "records inserted");
            con.close();
            response.sendRedirect("Login.jsp");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

有人可以告诉我解决这个问题吗?

java tomcat jdbc mysql-connector securitymanager
2个回答
2
投票

常规 Google 会让您访问 https://www.cnblogs.com/wtsgtc/p/14295068.html,这或多或少会引导您在 JRE 的

java.policy
文件中枚举该权限 - 假设没有不需要这种修改的 MySQL 驱动程序版本。


1
投票

问题在于您编写的代码存在连接泄漏。发生异常时,代码可能不会关闭连接。您可能错过了检查请求参数中的

null
值。要正确关闭连接,您应该使用
try-with-resources
自动关闭连接。您可以在此处阅读有关此内容的更多信息每次需要时创建和关闭数据库连接有什么影响吗?

如果您在 JVM 最初使用

disableAbandonedConnectionCleanup
开关启动时设置了系统属性,您也可以禁用
-D
线程。此错误在此描述 Bug #96870

© www.soinside.com 2019 - 2024. All rights reserved.