1

By default all users who have logged in will be directed to

http://example.com/landing

How can I redirect users to URL that's based on their company name e.g.

http://mcd.example.com/landing 

OR

http://kfc.example.com/landing

These subdomains still pointing to the same IP as where example.com points to.

How can I redirect users after authentication to their subdomains ?

I enabled the log4j and I noticed that my authentication token does not stay after redirection.

Update Log

DEBUG: org.springframework.security.web.DefaultRedirectStrategy - Redirecting to '/chaffic/redirectPage'
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext stored to HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@629fb6b: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@629fb6b: Principal: com.myapp.auth.AuthenticationUserDetails@817f0539; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: 42A6C4938AC026C3ACB8F9DB1148A19A; Granted Authorities: MEMBER'
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/rest/game/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/rest/app/**'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 1 of 13 in additional filter chain; firing Filter: 'ChannelProcessingFilter'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/login'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/authentication'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/forgot'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/site_**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/apk/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/admin/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Request '/redirectpage' matched by universal pattern '/**'
DEBUG: org.springframework.security.web.access.channel.ChannelProcessingFilter - Request: FilterInvocation: URL: /redirectPage; ConfigAttributes: [REQUIRES_SECURE_CHANNEL]
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 2 of 13 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@629fb6b: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@629fb6b: Principal: com.myapp.auth.AuthenticationUserDetails@817f0539; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: 42A6C4938AC026C3ACB8F9DB1148A19A; Granted Authorities: MEMBER'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 3 of 13 in additional filter chain; firing Filter: 'ConcurrentSessionFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 4 of 13 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 5 of 13 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 6 of 13 in additional filter chain; firing Filter: 'BrowserAuthenticationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 7 of 13 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 8 of 13 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 9 of 13 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.AnonymousAuthenticationFilter - SecurityContextHolder not populated with anonymous token, as it already contained: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken@629fb6b: Principal: com.myapp.auth.AuthenticationUserDetails@817f0539; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: 42A6C4938AC026C3ACB8F9DB1148A19A; Granted Authorities: MEMBER'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 10 of 13 in additional filter chain; firing Filter: 'SessionManagementFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 11 of 13 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 12 of 13 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/login'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/authentication'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/forgot'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/site_**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/apk/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/redirectpage'; against '/admin/**'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /redirectPage; Attributes: [hasAnyRole('MEMBER','ADMIN')]
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@629fb6b: Principal: com.myapp.auth.AuthenticationUserDetails@817f0539; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 127.0.0.1; SessionId: 42A6C4938AC026C3ACB8F9DB1148A19A; Granted Authorities: MEMBER
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@6ec797e, returned: 1
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Authorization successful
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - RunAsManager did not change Authentication object
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage at position 13 of 13 in additional filter chain; firing Filter: 'SwitchUserFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /redirectPage reached end of additional filter chain; proceeding with original chain
DEBUG: org.springframework.web.servlet.i18n.CookieLocaleResolver - Parsed cookie value [en_US] into locale 'en_US'
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/chaffic/redirectPage]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /redirectPage
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView com.myapp.controller.LandingPageController.redirectPage(org.springframework.ui.Model,javax.servlet.http.HttpServletRequest)]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'landingPageController'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/chaffic/redirectPage] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:https://company.chaffic.com:8443/chaffic/landing'; URL [https://company.chaffic.com:8443/chaffic/landing]] in DispatcherServlet with name 'mvc-dispatcher'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/rest/game/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/rest/app/**'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 1 of 13 in additional filter chain; firing Filter: 'ChannelProcessingFilter'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/login'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/authentication'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/forgot'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/site_**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/apk/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/admin/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Request '/landing' matched by universal pattern '/**'
DEBUG: org.springframework.security.web.access.channel.ChannelProcessingFilter - Request: FilterInvocation: URL: /landing; ConfigAttributes: [REQUIRES_SECURE_CHANNEL]
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 2 of 13 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - HttpSession returned null object for SPRING_SECURITY_CONTEXT
DEBUG: org.springframework.security.web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@267a6199. A new one will be created.
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 3 of 13 in additional filter chain; firing Filter: 'ConcurrentSessionFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 4 of 13 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 5 of 13 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 6 of 13 in additional filter chain; firing Filter: 'BrowserAuthenticationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 7 of 13 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - pathInfo: both null (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - queryString: both null (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - requestURI: arg1=/chaffic/landing; arg2=/chaffic/landing (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - serverPort: arg1=8443; arg2=8443 (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - requestURL: arg1=https://company.chaffic.com:8443/chaffic/landing; arg2=https://company.chaffic.com:8443/chaffic/landing (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - scheme: arg1=https; arg2=https (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - serverName: arg1=company.chaffic.com; arg2=company.chaffic.com (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - contextPath: arg1=/chaffic; arg2=/chaffic (property equals)
DEBUG: org.springframework.security.web.savedrequest.DefaultSavedRequest - servletPath: arg1=/landing; arg2=/landing (property equals)
DEBUG: org.springframework.security.web.savedrequest.HttpSessionRequestCache - Removing DefaultSavedRequest from session if present
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 8 of 13 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 9 of 13 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
DEBUG: org.springframework.security.web.authentication.AnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@6fa8dbd0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffdaa08: RemoteIpAddress: 127.0.0.1; SessionId: 9F9470A7CBD2D51E8F7DFB9EFB34F5A2; Granted Authorities: ROLE_ANONYMOUS'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 10 of 13 in additional filter chain; firing Filter: 'SessionManagementFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 11 of 13 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
DEBUG: org.springframework.security.web.FilterChainProxy - /landing at position 12 of 13 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/login'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/authentication'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/forgot'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/resources/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/site_**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/apk/**'
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/landing'; against '/admin/**'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /landing; Attributes: [hasAnyRole('MEMBER','ADMIN')]
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6fa8dbd0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffdaa08: RemoteIpAddress: 127.0.0.1; SessionId: 9F9470A7CBD2D51E8F7DFB9EFB34F5A2; Granted Authorities: ROLE_ANONYMOUS
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@6ec797e, returned: -1
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)
abiieez
  • 2,983
  • 13
  • 44
  • 101

1 Answers1

1

You could do this with 2 ways.

Please change the RequestMethod.GET as you wish.

@RequestMapping(value = "/comeHereToRedirec", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
    httpServletResponse.setHeader("Location", "http://mcd.example.com/landing ");
}


@RequestMapping(value = "/comeHereToRedirec", method = RequestMethod.GET)
public ModelAndView method() {
        return new ModelAndView("redirect:" + "http://mcd.example.com/landing");

}

Hope this helps.

tharindu_DG
  • 7,300
  • 4
  • 48
  • 56
  • I am getting redirected to the login page. Any idea why ? – abiieez Nov 26 '15 at 07:42
  • @abiieez : Are you sure that the authentication is happening correctly? What should happen if the authentication fails? – tharindu_DG Nov 26 '15 at 07:46
  • Yes, If I keyed in wrong credentials I will get the correct invalid credentials message. Now I keyed in the correct credentials, however seems like I lose my authentication token as sooon as I redirect to the subdomain. – abiieez Nov 26 '15 at 07:50
  • 1
    @abiieez: Yes, that does happen when the session id is in a cookie. Here is how you can overcome this : http://stackoverflow.com/questions/18492576/share-cookie-between-subdomain-and-domain – tharindu_DG Nov 26 '15 at 07:54