如何将SQLPermission
授予Java中的codebase
?我正在尝试使用JDBC连接到数据库(MySql)。
我发现的唯一有用的信息是SQLPermission,即使这还不够清楚。
我是否必须同意Socket
权限?
我使用的政策文件
grant
{
permission java.net.SocketPermission "*", "listen, connect, accept";
permission java.sql.SQLPermission "setLog";
permission java.sql.SQLPermission "callAbort";
permission java.sql.SQLPermission "setSyncFactory";
permission java.sql.SQLPermission "setNetworkTimeout";
};
statck trace:
Exception in thread "Abandoned connection cleanup thread" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "setContextClassLoader")
当我添加
对策略文件的权限java.lang.RuntimePermission "setContextClassLoader";
没有异常但是未创建的Connection和Statement始终为null。在调试后,应用程序在return flag;
之后的try块结束时直接跳转到conn = DriverManager.getConnection(DB_URL,USER,PASS);
当然,在未设置安全管理器或授予AllPermission时,一切都能正常运行。
public boolean validate(String name,String password)
{
Connection conn = null;
Statement stmt = null;
try{
System.out.println("retrieving data");
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Connection successful");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
String userName = rs.getString("userName");
System.out.println(userName+" "+name);
if(name.equals(userName))
{
String pwd=rs.getString("pass");
System.out.println(pwd+" "+password);
if(pwd.equalsIgnoreCase(password))
{
String position = rs.getString("position");
flag=true;
map.put("userName", userName) ;
map.put("password", pwd) ;
map.put("position", position) ;
System.out.println(map);
System.out.println("user authenticated");
}
}
}
rs.close();
stmt.close();
conn.close();
return flag;
}catch(SQLException se)
{
se.printStackTrace();
System.out.println(se.getMessage());
return flag;
}finally
{
try
{
if(stmt!=null)
stmt.close();
}catch(SQLException se2)
{
se2.printStackTrace();
System.out.println(se2.getMessage());
}
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
return flag;
}
答案 0 :(得分:0)
雅确实问题是有权连接到数据库。使用新的策略文件可以正常工作。
grant
{
permission java.net.SocketPermission "*", "accept, connect, listen, resolve";
permission java.util.PropertyPermission "*", "read, write";
permission java.lang.RuntimePermission "*";
};