为什么我不能使用Realm和JDBC登录?

时间:2018-10-23 19:17:54

标签: java jdbc jdbcrealm

嗨,你能帮我吗...我想使用Realm使用表单身份验证方法,但是当我设置所有内容时,我无法进入... 它让我发疯了...:(

这是我的xml代码:

<login-config>
<auth-method>FORM</auth-method>
<realm-name>org.apache.catalina.realm.JDBCRealm</realm-name>
<form-login-config>
    <form-login-page>/Login.jsp</form-login-page>
    <form-error-page>/Erooooring.jsp</form-error-page>
</form-login-config>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>gav</web-resource-name>
        <url-pattern>/teacher/*</url-pattern>
    </web-resource-collection>

    <auth-constraint>
        <role-name>teacher</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>khar</web-resource-name>
        <url-pattern>/student/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>student</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <role-name>teacher</role-name>
</security-role>

<security-role>
    <role-name>student</role-name>
</security-role>

这是我的登录html代码:

<body>
<div class="login">
<h1>Login</h1>
<form action="Login" method="post" onsubmit="return AccessValidator()">
<input type="text" name="userid" id="id" placeholder="Username" size="20">
  <label style="color: red;background: black;" for="id" id="label1"></label>
<input type="password" name="userpassword" placeholder="Password" id="password" size="20">
  <label style="color: red;background: black;" for="password" id="label2"></label>

  <button type="submit" class="btn btn-primary btn-block btn-large" onclick="AccessValidator()">Let me in.</button>
</form>
</div>
<% if (request.getAttribute("error")==null) ;
else { out.print("<h1 style=\"margin-left: 35%;color: #670d10\">Invalid    Username OR Password</h1>"); }
%>

<script language="JavaScript" src="Validator.js"></script>
</body>

这是我的servlet代码:

@WebServlet("/Login")
public class Login extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse    response) throws IOException, ServletException {


    String id = request.getParameter("userid");
    String password = request.getParameter("userpassword");
    System.out.println(id);
    System.out.println(password);

    System.out.println(request.getRemoteUser());
    try {
        System.out.println("inside try ");
        request.login(id,password);
        System.out.println("Loged in");

        GenericPrincipal genericPrincipal = (GenericPrincipal)    request.getUserPrincipal();
        String rolename = genericPrincipal.getRoles()[0];
        System.out.println(rolename);


     } catch (Exception E) {

        System.out.println(E);

      }
  }
 }

这是我的Realm代码在server里面,在tomcat config里面:

<Realm className="org.apache.catalina.realm.LockOutRealm">
    <!-- This Realm uses the UserDatabase configured in the global JNDI
         resources under the key "UserDatabase".  Any edits
         that are performed against this UserDatabase are immediately
         available for use by the Realm.  -->
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>


   <Realm
     className="org.apache.catalina.realm.JDBCRealm" 
     driverName="oracle.jdbc.driver.OracleDriver"
     connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
       connectionName="khanloo"
       connectionPassword="myjava123"
       userTable="newusers"
       userNameCo1="username"
       userCredCo1="password"
       userRoleTable="newroles"
       roleNameCo1="rolename"
   />

   </Realm>

在我的数据库中,我有2个表 1是具有用户名和密码的新用户 2是具有用户名和角色名的新人

我在表1中有用户名saeed,密码khanloo 在表2中,用户名saeed和角色名教师

但是当我在登录时询问khanloo时,它不起作用,这是我的输出:

saeed
khanloo
null
inside try 
javax.servlet.ServletException: Login failed

这是stacktrace:

at org.apache.catalina.connector.Request.login(Request.java:2699)
at org.apache.catalina.connector.RequestFacade.login(RequestFacade.java:1087)
at projeSabteNamUni.Login.service(Login.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

很明显,request.login中出现异常,但是我已将所有设置正确,,任何人都可以看到问题出在哪里吗?在xml或服务器文件中?

0 个答案:

没有答案