Spring Boot - how to automatically redirect user from www domain to non-www domain? - spring

I have classic Spring Boot application on my VM.
A user can go to https://www.fakeurl.com which is pointing to my VM , or, a user can go to https://fakeurl.com which is pointing to my VM.
The problem is that this is bad for SEO. When a user goes to https://www.fakeurl.com (with www) I need to automatically redirect him to non-www domain https://fakeurl.com
How can I do this in SpringBoot?

Related

How can I setup keycloak and Spring to use HTTPS when redirecting to login page

I'm trying to reach a Spring Boot API hosted on a server with my Angular application, but I have some CORS issues.
CORS Error in the console
Redirection Flow
I tried several things, I added my URLs in both keycloak and Spring, and I think I found the origin of the problem but can't seems to resolve it.
I believe the problem comes from the fact that keycloak redirects to http://user-service.cubetech-app.fr/sso/login instead of the HTTPS version of that url. I read that when the browser detect a redirection on a different hostname it change the Origin header to null, and I think this is what creates the CORS error.
I don't know how to force HTTPS on the internal redirection.
For further informations, my spring backend and my keycloak server are hosted on my VPS, in a docker, and behind a traefik reverse-proxy that enables HTTPS with Let's Encrypt.
Check the webOrigins value of the client you use in Keycloak.
Realm settings-> clients -> {your client}

Subdomain reroute to domain

I have created a web app using Laravel 8 and is hosted on Server A using domain name 'myapp.com'. I have applied a wildcard subdomain eg: 'myweb1.myapp.com' and 'myweb2.myapp.com' for one of the app features.
I then purchased a domain 'myweb1.com' from Server B which I intends to reroute it to 'myweb1.myapp.com'. How do I achieve this?
So, when 'myweb1.myapp.com' is entered the URL will reroute to 'myweb1.com'. And 'myweb1.myapp.com/product' will be 'myweb1.com/product'.
Do I need to change the nameservers for 'myweb1.com'? Do I need to change .htaccess for it? What if I only purchased a domain without a hosting which I can't edit .htaccess? Can I achieve this? And are there any specific term for I want to do?
you can do it with reverse proxy.
if your web server is nginx: https://stackoverflow.com/a/14352958/5987259
if your web server is apache: https://stackoverflow.com/a/52286695/5987259

Amazon Route53 redirection

I have a domain example.com in AWS and have got one load balancer and one ec2 instance. Trying to setup https with certificate from AWS.
Route53 setup and A record setup is done as follows for domain to load balancer.
domain name - *.example.com
Name:-
example.com.
Type:-
- Ip4
Alias Target :- dns name of load balancer.
My web is a spring based web and it redirect user to landing page if user access www.example.com --->>> www.example.com/landing.
After all the setup, if i access like the below url, it is working fine.
https://www.example.com/landing
But if i access without www,
https://example.com/landing --> It leads to browser security page.(Your connection is not private
Attackers might be trying to steal your information from ...)
Our certificate is based on the domain name *.example.com.
If I simply access, https://example.com/ ---> It gets redirected to http://example.com/landing -- > Spring app redirects like this with http.
http://www.test.com/landing leads to certificate trust error. (https://example.com/landing).
Getting site can not be reached error if i simply access, http://www.example.com/
I am looking for URL redirection to https if user access using http or without www or without any scheme. Not sure if i have to configure anything in Route53.
Also, Do i need to configure in my app to redirect to https?
Your current certificate should not work for the root, i.e. https://test.com
Either purchase an alternate name for https://test.com or use only www.test.com as your website. You do not need to change anything in Route53 for now. You probably do need to add redirection in your webserver/app to use HTTPS-only, unless you want to leave it to the users.

cPanel Tomcat7 with domain fails to redirect spring security url j_spring_security_check

I am using cpanel WHM 11.44 (supports tomcat 7.0.42) on my Centos VPS. I have creating a domain (www.testdomain.com) using WHM and install servlet on it.
Inside /home/testdomain/ jsptest.jsp is created and successfully loaded that page. On that I have uploaded my spring mvc application with spring security(MyApps.war). My application MyApps.war is extracted inside /home/testdomain.
As per cpanel tomcat7 documentation, I have added JkMount /MyApps/* ajp13 on cp_jkmount.conf file.
https://documentation.cpanel.net/display/EA/Introduction+to+Tomcat
Now I can access my tomcat application using www.testdomain.com/MyApps. That is successfully loaded and redirected to www.testdomain.com/MyApps/index.jsp. (login page)
After login I can see www.testdomain.com/MyApps/j_spring_security_check with error message.
(Not Found : The requested URL /MyApps/j_spring_security_check was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Apache Server at www.testdomain.com Port 80).
Actually MyApps is working fine on my local computer.
I know cPanel Tomcat 7 is shared on each domains. So What I am missing on shared tomcat with spring security. Please help me move forward.

Replication test failed, site Moved Temporarily (Dispatcher Flush Agent)

I have a CQ Dispatcher configured behind a SSO(single sign on), so when I access the dispatcher URL it will first redirect me to the SSO login page and then after entering the credentials it moves to the dispatcher(My home page).
There isn't a problem in accessing the dispatcher URL
Problem Statement
Problem is with flushing the old content in the cache and getting in the new content onto the cache.
Editing the flush agent
Under the transport tab in the URI section when I give in the value dispatcherURL/dispatcher/invalidate.cache and test the connection it says site moved to the particular sso login page.
What I did
I tried giving in the SSO credentials under the transport tab with
URI as dispatcherURL/dispatcher/invalidate.cache
I tried giving in the SSO credentials under the transport tab with
URI as
SSOURL/dispatcher/invalidate.cache [It is redirected URL]
Please share your knowledge around this.
I think the best option here is to create a new virtualhost in Apache, available without SSO. It should be configured in such manner that only flush agent (and no external clients) can access it. If Apache is installed on the same host as CQ, the virtualhost can be bound to the localhost interface. Otherwise, it should only allow connections from the CQ5 host.
The virtualhost should include the dispatcher configuration, copied from your regular virtualhost.
If you have virtualhost like this, you can configure the flush agent without need to deal with authorization.

Resources