views:

23

answers:

1

dears

does anyone knows if tomcat is able to password protect file (like apache .htaccess )? i mean when user request a file from tomcat webapp its prompt a dialogue to enter user-name and password and made this using configuration.

or protect the file depend on its IP address .

hope someone can help me ?

regads

+2  A: 

you can set basic authentication in tomcat.

Add your user to tomcat-users.xml. Something like :

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="myname" password="mypassword" roles="tomcat"/>
  <user username="test" password="test"/>
</tomcat-users>

And Add configuration to your apps web.xml. like:

<!-- Define a Security Constraint on this Application -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Entire Application</web-resource-name>
      <url-pattern>/references/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>your-role</role-name>
    </auth-constraint>
  </security-constraint>

  <!-- Define the Login Configuration for this Application -->
  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Application</realm-name>
  </login-config>

  <!-- Security roles referenced by this web application -->
  <security-role>
    <description>
      The role that is required to log in to the Manager Application
    </description>
    <role-name>your-role</role-name>
  </security-role>

links to understand more:

http://www.avajava.com/tutorials/lessons/how-do-i-use-basic-authentication-with-tomcat.html

YoK