This question already has answers here:
Set CORS header in Tomcat
(4 answers)
Closed 7 years ago.
I am trying to consume some web services which are cross domain. When I disable chrome's web-security it is working fine. I want it to work without this so I have tried adding cross-domain.xml and still it didnt work. When i searched more, came to know about CORS enabling in tomcat.
from http://www.w3.org/wiki/CORS_Enabled
For Apache
Apache can be configured to expose this header using mod_headers. This is enabled by default in Apache, however you may want to ensure it's enabled in your deployment by running the following command:
a2enmod headers
To expose the header, you can add the following line inside , , and sections, or within an .htaccess file.
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Can anyone please let me know where to add these configurations in TOMCAT and in which files exactly. I am using tomcat from eclipse.
Appreciate any help.
CORS support in Tomcat is provided via a filter. You need to add this filter to your web.xml file and configure it to match your requirements. Full details on the configuration options available can be found in the Tomcat Documentation.
Check this answer: Set CORS header in Tomcat
Note that you need Tomcat 7.0.41 or higher.
To know where the current instance of Tomcat is located try this:
System.out.println(System.getProperty("catalina.base"));
You'll see the path in the console view.
Then look for /conf/web.xml on that folder, open it and add the lines of the above link.
Just to add a bit of extra info over the right solution. Be aware that you'll need this class org.apache.catalina.filters.CorsFilter. So in order to have it, if your tomcat is not 7.0.41 or higher, download 'tomcat-catalina.7.0.41.jar' or higher ( you can do it from http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina ) and put it in the 'lib' folder inside Tomcat installation folders.
I actually used 7.0.42
Hope it helps!
Related
I am using Apache Tomcat 9 server as a Maven dependency in my project. It is working fine and now I need to add a health endpoint so that it will return 200 OK if everything is running fine.
I came to know about HealthCheckValve (https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Health_Check_Valve) option in Tomcat 9 which is helpful. But I am not been able to figure out where to add this and the process of configuring this valve. As I know if server is standalone we can configure in Server.xml but as the Tomcat Server is a maven dependency I don't know how and where I should configure this.
Can somebody please help me in configuring health endpoint in Apache Tomcat 9 (as a maven dependency) ?
See the documentation, then add the HealthCheckerValve to server.xml. Valves go into either the Engine, Host or Context element. In the server.xml packaged with Tomcat you can find comments that should direct you to the right location.
When embedding a version of Tomcat, you won't have this file available, and so you need to assemble instances of these containers programmatically.
Check the launcher application in this example: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html
While I could not find methods like addValve() I found an init() method that you could use to provide a server.xml which will be read by Tomcat.
I saw the documentation of all valves available in Tomcat 9.0.x.
In order to find the solution of this specific task, I tried looking for configuration of other valves such as Remote Address Valve in embedded tomcat.
I found a solution by user967710 after searching a lot.
I did the following to add a Health Check Valve to my Tomcat 9.0.64 :
Tomcat tomcat = new Tomcat();
tomcat.getEngine().setName(UUID.randomUUID().toString());
tomcat.setPort(context.port);
tomcat.setHostname(context.hostname);
tomcat.getHost().setAppBase(".");
Valve valve = new HealthCheckValve();
tomcat.getHost().getPipeline().addValve(valve);
It doesn't matter how you configure the Tomcat for your project i.e from line 1 ~ 5 but actually last 2 lines i.e 6 and 7 are important where you are adding the valve.
The health endpoint can be accessible on host:port/health.
For e.g if it is hosted at http://localhost:4000 then the health endpoint would be http://localhost:4000/health
This endpoint will return 200 OK with a simple JSON response stating the Tomcat server status i.e "UP" if everything is up and running.
I am trying to configure an external Apache front-end with a different URL to the backend tomcat app. I take tomcat's manager app as an example.
I would like users to access my page like externally like:
https://myhost.com/tomcat-manager
Internally, it gets redirected to
http://localhost:8080/manager
I tried the following configuration in apache2 with mod_proxy and mod_rewrite:
RewriteEngine on
SSLProxyEngine on
RewriteRule "/manager/(.*)$" https://myhost.com/tomcat-manager/$1 [P]
ProxyPass /tomcat-manager http://localhost:8080/manager
ProxyPassReverse /tomcat-manager http://localhost:8080/manager
It mostly works, but I dislike that the tomcat manager webapp outputs its links as "/manager" instead of "/tomcat-manager", forcing me to add the above mod_rewrite rule. Ideally, I would like the end-user to see only browser URLs of the pattern:
https://myhost.com/tomcat-manager/(whatever)
and never the following:
https://myhost.com/manager/(whatever)
Any suggestion to configure Apache without modifying the underlying webapp? Thanks!
I found the answer to my question. It is Apache's mod_proxy_html. I enabled the corresponding modules and fixed Ubuntu's mod_proxy_html missing configuration as stated here: https://serverfault.com/questions/684905/proxyhtmlurlmap-not-working-in-apache2-4
Then, I added the following configuration code:
ProxyHTMLEnable On
ProxyHTMLURLMap http://localhost:8080/manager/ /tomcat-manager/
ProxyHTMLURLMap /manager/ /tomcat-manager/
SetOutputFilter proxy-html
RequestHeader unset Accept-Encoding
And that's it. Now, I can access http://myhost.com/tomcat-manager and the html code within is also rewritten!
I want to convert spring application into angular js. I am using some external css and js.
css is included properly but not js.
when I use
<script src="bower_components/angular/angular.min.js"></script>
It is considering angular.min.jsp and throwing error not found.
How to include external js into my app.
It seems as some misconfiguration on the server side.
I would suppose that you are using Apache 2.4 or Tomcat 7
In Apache 2.4 take a look at https://httpd.apache.org/docs/2.4/rewrite/remapping.html
If you were running the application in the ubuntu then default location to look at would be /etc/apache2 and usually this is going to be found in /etc/apache2/sites/available/000-default.conf
In Tomcat 7 take a look at
https://tomcat.apache.org/tomcat-8.0-doc/rewrite.html
Under Ubuntu with the default installation of Tomcat, this information will be found either in the file /var/lib/tomcat7/conf/server.xml or under the directory your application is deployed in META-INF/context.xml or under /var/lib/tomcat7/Catalina in one of the subdirectory. This depends a lot on the settings you have.
If neither of these is a case then take a look at the filters applied to the application as some of them can screw the answer. For them take a look at WEB-INF/web.xml or if you are using the XML-less configuration then try Some of the I believe SpringWebApplication classes.
If you still use the Spring to serve the backend then it is also possible that you have incorrectly set up ViewResolver, which returns .jsp as appendix instead of .js
I have a webapplication that I am running in Felix osgi container. I am using jetty as the implementation for the extHttpService. Currently it is writing the cookies to the '/' root path. I would like to change this as it is causing conflicts with other web applications. Looking at jetty documentation it appears I need to set the following property.
org.mortbay.jetty.servlet.SessionPath
However, I am unable to find a way to set this using the ExtHttpService via osgi. I have tried creating a jetty.xml file, adding this to the config.properties, and setting it as a property in the call to register my servlet.
Does anyone know how to set this?
thanks,
I actually ended up patching the source for my current implementation, but on the mailing lists here, a patch has been submitted that should allow this to be configurable.
I was searching in Google and found that Apache can be configured via mod_access directives in the httpd.conf file to block a web site from a particular IP.
Is there anything equivalent in Tomcat?
I am not sure I understand what are the corresponding configuration files.
Thanks
Try the Remote Address Filter. http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html
See the Request filters section of this doc. Doing this via tomcat configuration is pretty static, you need to restart to edit configuration. If you need something dynamic, it's probably best to implement a custom servlet filter.