【问题标题】:Grails - Acegi, how to redirect user to a different domain during authentication?Grails - Acegi,如何在身份验证期间将用户重定向到不同的域?
【发布时间】:2012-03-07 15:46:47
【问题描述】:

我继承了一个使用 Acegi 0.5.3 插件的 Grails 应用程序。

可以通过两个完全不同的 URL(例如 app.domainone.com 和 app.domaintwo.com)访问该应用程序。域名映射到两个不同的用户社区。现在,我的任务是限制用户仅访问与他们相关的域。目前用户可以访问任何域并登录到应用程序。

我对 Acegi 的工作原理有一些了解,但不能说我完全了解。所以想问我如何才能做到这一点。

在理想情况下,当用户尝试登录时,我想重定向(如果需要)到他们的“相关”域并使用他们给定的凭据自动登录。但是,作为一种临时解决方案,即使是简单的重定向到相关登录页面也足够了。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    这是我的 CustomAuthenticationProcessingFilter。那里可能有更好的解决方案,但这有助于我了解我对 Grails 和 Spring Security 的了解。

        class CustomAuthenticationProcessingFilter extends GrailsAuthenticationProcessingFilter implements
        InitializingBean {
    
            //def authenticationManager
            @Override
            public int getOrder() {
                return FilterChainOrder.AUTHENTICATION_PROCESSING_FILTER
            }
    
            @Override
            void doFilterHttp(HttpServletRequest request,
            HttpServletResponse response, FilterChain chain) throws IOException,
            ServletException {
                if (SecurityContextHolder.getContext().getAuthentication() == null) {
                    def loginUrl = "${request.getRequestURL().toString() - request.getRequestURI().toString()}${request.contextPath}"
                    def username = request.getParameter("j_username")
                    def password = request.getParameter("j_password")
    
                    if ( loginUrl && username && password) {
                        def user = User.findByEmailOrCompanyEmail(username,username)
                        if(user) {
                            def query = """select c from Community c, UserCommunity uc
                                            where c.id = uc.comm.id
                                            and uc.user.id = :userId"""
                            def comm = Community.executeQuery(query,[userId:user.id])
                            comm = comm?(comm?.get(0)):null
                            if(loginUrl!=comm?.url) {
                                println "Trying to login using the wrong URL"
                                response.sendRedirect(comm.url+'/login/auth')
                                return
                            }
    
                        }
                    }
                }
    //Resume the normal flow
                super.doFilterHttp(request, response, chain)
            }
    
            @Override
            public void afterPropertiesSet() throws Exception {
                // TODO Auto-generated method stub
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      相关资源
      最近更新 更多