Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a NetBeans web project on Tomcat 8.0.15, MySql (with Jconnector), Servlet/Jsp. I need a secure login!!!

There is a login page. On successful login, if it is admin, servlet redirects to the admin page, if username is of a standardUser, the servlet redirects to the main page.

For now nothing works. The page is not redirected by servlet, on submit button press the error.html appears and shows response.getStatus() is 0!!! I tested(without login) servlet for username/password check against the DB and it works.

so Here is my code files: login.jsp:

XML
<form id="loginForm" method="POST" action="j_security_check">
                        <input name="j_username" id="j_username" type="email" value="test.admin@test.com"></input>
                        <input name="j_password" id="j_password" type="password" value="test"></input>
                        <input id="loginSubmit" type="submit" value="Login"></input></form>


web.xml:
XML
<servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>ProjectP.Auth</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/j_servlet_check</url-pattern>
    </servlet-mapping>
<session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>MyDatabase</realm-name>
        <form-login-config>
            <form-login-page>/Login.jsp</form-login-page>
            <form-error-page>/Error.html</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description>Web Site Administrator</description>
        <role-name>admin</role-name>
    </security-role>
    <security-role>
        <description>Stansard user</description>
        <role-name>user</role-name>
    </security-role>
    <security-constraint>
        <web-resource-collection>
           <web-resource-name>ProjectP</web-resource-name>
           <url-pattern>/adminpages/*</url-pattern>
           <url-pattern>/userpages/*</url-pattern>
           <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
           <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <web-resource-collection>
           <web-resource-name>ProjectP</web-resource-name>
           <url-pattern>/userpages/*</url-pattern>
           <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
           <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>MyDatabase</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

Context.xml:
XML
<Context antiJARLocking="true" path="/ProjectP" allowCasualMultipartParsing="true" >
    <Realm className="org.apache.catalina.realm.JDBCRealm" driverName="com.mysql.jdbc.Driver"
               name="MyDatabase" type="javax.sql.DataSource"
               connectionURL="jdbc:mysql://localhost:3306/mydatabase"
                   connectionName="root" connectionPassword="sa"
                   userTable="administrators" userNameCol="username" userCredCol="password"
                   userRoleTable="userroles" roleNameCol="role"
                   auth="Container" maxTotal="100" maxIdle="30" maxWaitMillis="10000"/>
</Context>

LoginServlet.java:
Java
@WebServlet("/j_servlet_check/*")
@MultipartConfig
public class LoginServlet extends HttpServlet {
protected void doPost(HttpS .......
response.setContentType("text/html;charset=UTF-8"); //text/plain
        String username, password;
        try {
            username = request.getParameter("j_username");
            password = request.getParameter("j_password");
if (checkLoginUser(username, password)) { //it works for sure, tested...

response.sendRedirect("./index.html"); // should it work?

HttpSession session=request.getSession();
session.setAttribute( "username", username );

request.getRequestDispatcher("../admin/Administration.jsp").forward(request, response); // mayme this should work!?


Is context.xml - realm needed?
So, how to create a secure login?
Posted

1 solution

In context.xml

HTML
<context antijarlocking="true" path="/myDBname" allowcasualmultipartparsing="true">
	<realm classname="org.apache.catalina.realm.CombinedRealm">
		<realm classname="org.apache.catalina.realm.JDBCRealm" drivername="com.mysql.jdbc.Driver">
			   name="myDBname" type="javax.sql.DataSource"
			   connectionURL="jdbc:mysql://localhost:3306/mydbname" 
			   connectionName="root" connectionPassword="sasa" 
			   userTable="administrators" userNameCol="Email" userCredCol="Password"
			   userRoleTable="administrators" roleNameCol="LoginRole"
			   auth="Container" maxTotal="100" maxIdle="30" maxWaitMillis="10000"/>
	</realm>               
</realm></context>


web.xml
HTML
<resource-ref>
	<description>DB Connection</description>
	<res-ref-name>myDBname</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
</resource-ref>
<login-config>
	<auth-method>FORM</auth-method>
	<realm-name>myDBname</realm-name>
	<form-login-config>
		<form-login-page>/pages/Login.jsp</form-login-page>
		<form-error-page>/pages/Error.jsp</form-error-page>
	</form-login-config>
</login-config>
<security-role>
	<description>Administrator</description>
	<role-name>admin</role-name>
</security-role>
<security-constraint>
	<web-resource-collection>
	   <web-resource-name>myDBname</web-resource-name>
	   <url-pattern>/pages/admin/*</url-pattern>
	   <http-method>POST</http-method>
	   <http-method>GET</http-method>
	   <http-method>PUT</http-method>
	</web-resource-collection>
	<auth-constraint>
	   <role-name>admin</role-name>
	</auth-constraint>
</security-constraint>
<welcome-file-list>
	<welcome-file>pages/home.jsp</welcome-file>
</welcome-file-list>


Copied mysql-connector-java-5.1.23-bin.jar into C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.15\lib

I also use <%= request.getContextPath() %> in order to connect resources to pages

HTML
<link href="<%= request.getContextPath() %>/resources/styles/main.css" rel="stylesheet" type="text/css" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900