如何在Spring MVC中重定向到子域

时间:2015-11-26 06:58:39

标签: spring-mvc

默认情况下,所有已登录的用户都将被定向到

http://example.com/landing

如何将用户重定向到基于公司名称的网址,例如

http://mcd.example.com/landing 

http://kfc.example.com/landing

这些subdomains仍然指向与example.com指向的IP相同的IP。

如何在身份验证后将用户重定向到其子域?

我启用了log4j,我注意到重定向后我的身份验证令牌不会停留。

更新日志

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)

1 个答案:

答案 0 :(得分:1)

你可以用两种方式做到这一点。

请根据需要更改RequestMethod.GET

@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");

}

希望这有帮助。