Apache Shiro SQL 验证用户时出错
Apache Shiro SQL error while authenticating user
我正在尝试连接到数据库,以便使用 Apache Shiro 对用户进行身份验证。我有一个 servlet 调用 java class 来执行此任务。现在它只是在成功验证后更改一个字符串。我尝试了许多不同的连接方法:数据池、jtds、Microsoft Sql Server JDBC,都给我错误:
SQL error while authenticating user [user1]
我的日志文件也显示此错误:
Warning: RAR5058: Error while Resizing pool SQLS1-TestBilling. Exception : Connection could not be allocated because: The TCP/IP connection to the host SQLS1, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
我可以很好地使用 servlet 中的 TestBilling 连接池,所以我认为这不是池的问题,但似乎无法使用 ini 文件声明来使用 SQL .有没有我遗漏、忘记做或做错的事情?
相关文件如下:
GetAuthServlet.java:
package com.phmc.shiro5web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetAuthServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Auth</title>");
out.println("</head>");
out.println("<body>");
AuthenticationClass au = new AuthenticationClass();
String b = "";
b = au.isAuthenticated("mmarino", "test");
out.println(b);
out.println("</body>");
out.println("</html>");
}
AuthenticationClass.java:
package com.phmc.shiro5web;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuthenticationClass {
private static final transient Logger log = LoggerFactory.getLogger(Authentication.class);
String isAuthenticated(String username, String password){
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
String b = "fail";
log.info("Test");
try{
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("mmarino", "test") ;
token.setRememberMe(true);
try {
currentUser.login(token);
b = "Success";
}catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
}
}
}catch(Exception e){
b = e.toString();
}
)
return b;
}
}
Shiro.ini:
[main]
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.resourceName = jdbc/TestBilling
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource = $ds
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = SELECT theuser FROM UserList
securityManager.realm = $jdbcRealm
我也试过:
jdbcRealm.authenticationQuery = SELECT theuser FROM UserList where passwordList = ?
在 Shiro.ini 文件中倒数第二行。
尝试
# DataSource config
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType = javax.sql.DataSource
ds.resourceName = jdbc/TestBilling
似乎还有一个我没有发现的额外错误导致 log4j 中断,它阻止了其他显示基本 SQL 错误的错误。
我正在尝试连接到数据库,以便使用 Apache Shiro 对用户进行身份验证。我有一个 servlet 调用 java class 来执行此任务。现在它只是在成功验证后更改一个字符串。我尝试了许多不同的连接方法:数据池、jtds、Microsoft Sql Server JDBC,都给我错误:
SQL error while authenticating user [user1]
我的日志文件也显示此错误:
Warning: RAR5058: Error while Resizing pool SQLS1-TestBilling. Exception : Connection could not be allocated because: The TCP/IP connection to the host SQLS1, port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
我可以很好地使用 servlet 中的 TestBilling 连接池,所以我认为这不是池的问题,但似乎无法使用 ini 文件声明来使用 SQL .有没有我遗漏、忘记做或做错的事情?
相关文件如下:
GetAuthServlet.java:
package com.phmc.shiro5web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetAuthServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Auth</title>");
out.println("</head>");
out.println("<body>");
AuthenticationClass au = new AuthenticationClass();
String b = "";
b = au.isAuthenticated("mmarino", "test");
out.println(b);
out.println("</body>");
out.println("</html>");
}
AuthenticationClass.java:
package com.phmc.shiro5web;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AuthenticationClass {
private static final transient Logger log = LoggerFactory.getLogger(Authentication.class);
String isAuthenticated(String username, String password){
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
String b = "fail";
log.info("Test");
try{
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("mmarino", "test") ;
token.setRememberMe(true);
try {
currentUser.login(token);
b = "Success";
}catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
}
}
}catch(Exception e){
b = e.toString();
}
)
return b;
}
}
Shiro.ini:
[main]
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.resourceName = jdbc/TestBilling
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource = $ds
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = SELECT theuser FROM UserList
securityManager.realm = $jdbcRealm
我也试过: jdbcRealm.authenticationQuery = SELECT theuser FROM UserList where passwordList = ?
在 Shiro.ini 文件中倒数第二行。
尝试
# DataSource config
ds = org.apache.shiro.jndi.JndiObjectFactory
ds.requiredType = javax.sql.DataSource
ds.resourceName = jdbc/TestBilling
似乎还有一个我没有发现的额外错误导致 log4j 中断,它阻止了其他显示基本 SQL 错误的错误。