Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

BASIC web authentication issue

Hi,

i wrote my web application with web.xml as below

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
<welcome-file-list>
    <welcome-file>simple.jsp</welcome-file>
</welcome-file-list>
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>MyResources</web-resource-name>
			<url-pattern>/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>myuserrole</role-name>
		</auth-constraint>
	</security-constraint>
	<login-config>
		<auth-method>BASIC</auth-method>
		<realm-name>FILE</realm-name>
	<!-- 	<form-login-config>
			<form-login-page>/login.jsp</form-login-page>
			<form-error-page>/error.jsp</form-error-page>
		</form-login-config> -->
	</login-config>
</web-app>

Open in new window


i added role and username and password to tomcat-users.xml as below

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><tomcat-users version="1.0" xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>-->
    <role rolename="myuserrole"/>
    <user username="myuser" password="mypassword" roles="myuserrole"/>
    

</tomcat-users>

Open in new window



when i run my application on tmcat 8.5 it is directly displaying simple.jsp content

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Simple JSP</title>
</head>
<body>
	<b>Secured JSP</b>
</body>
</html>

Open in new window


as below
Simple JSP


 without asking me to enter username and password

how to fix this so that it asks me to enter username and password once i run application on server
when i give username and password as myuser and mypassword then only it should show content of simple.jsp


how to change this to form based authentication
any good examples, resources , links around this with source code?
please advise
simpleJsp.png
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

1) How do you build and deploy the web application?
2) How do you access the simple.jsp?

sorry i could not see this earlier.

I basically just right clicked on the project in eclipse then selected"run as server" selected tomcat 8 there then automatically it is opening the jsp content without opening the popup asking my username and password which is what i expected?
Avatar of gudii9

ASKER

please advise
Avatar of gudii9

ASKER

any simple end to end application on this? please advise
Maybe Eclipse's embedded browser does not support it. Can you try exporting the .war and deploying it on a standalone Tomcat instance?
Avatar of gudii9

ASKER

Maybe Eclipse's embedded browser does not support it. Can you try exporting the .war and deploying it on a standalone Tomcat instance?


you mean eclipse embedded tomcat server as below?

Maybe Eclipse's embedded tomcat does not support it. Can you try exporting the .war and deploying it on a standalone Tomcat instance?
i was not clear on how browser related with standalone tomcat? please advise
Avatar of gudii9

ASKER

please advise
Avatar of gudii9

ASKER

tried below link

https://www.concretepage.com/java-ee/jsp-servlet/form-based-authentication-in-jsp-using-tomcat

i am getting below error

ud_mdb_activemq\JSPLoginDemo>gradle clean build
'gradle' is not recognized as an internal or external command,
operable program or batch file.

please advise
Avatar of gudii9

ASKER

when i deployed war to the webapps location of tomcat getting errors
http://localhost:8080/security-form-based/protected/index.jsp

HTTP Status 404 – Not Found
Type Status Report

Message /security-form-based/protected/index.jsp

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

please advise