Java spring安全性-拦截不同登录的子域url?

Java spring安全性-拦截不同登录的子域url?,java,spring,security,subdomain,Java,Spring,Security,Subdomain,我有一个安装了spring security的应用程序,并且运行良好——它目前在www.exampledomain.com上运行 现在我想扩展运行在子域之外的应用程序。例如newapp.exampledomain.com 唯一的问题是,对于这个新应用程序,用户需要登录。在春季,通过 但是,当您想要截取子域进行登录时,您会怎么做?例如,以下内容对我不起作用: <intercept-url pattern="http://newapp.exampledomain.com/*" access="

我有一个安装了spring security的应用程序,并且运行良好——它目前在
www.exampledomain.com
上运行

现在我想扩展运行在子域之外的应用程序。例如
newapp.exampledomain.com

唯一的问题是,对于这个新应用程序,用户需要登录。在春季,通过

但是,当您想要截取子域进行登录时,您会怎么做?例如,以下内容对我不起作用:

<intercept-url pattern="http://newapp.exampledomain.com/*" access="ROLE_GENERAL"/>


有没有想过如何解决这个问题?

一个选择是编写自己的AccessDecisionVoter,它根据主机名扩展并添加额外的检查。大概是这样的:

public class MyVoter extends RoleVoter {
  public int vote(Authentication authentication,
                java.lang.Object object,
                java.util.Collection<ConfigAttribute> attributes) {
    FilterInvocation filterInvocation = (FilterInvocation) object;
    HttpRequest request = filterInvocation.getHttpRequest();
    // get subdomain from request
    String subdomain = getSubdomain(request);
    if ("free".equals(subdomain)) {
      return ACCESS_GRANTED;
    }
    else {
      super.vote(authentication, object, attributes);
    }
  }
}
公共类MyVoter扩展RoleVoter{
公共整数投票(身份验证,
java.lang.Object对象,
java.util.Collection属性){
FilterInvocation FilterInvocation=(FilterInvocation)对象;
HttpRequest请求=filterInvocation.getHttpRequest();
//从请求中获取子域
String subdomain=getSubdomain(请求);
如果(“自由”。等于(子域)){
返回已授予的访问权限;
}
否则{
超级投票(身份验证、对象、属性);
}
}
}
然后连接您的投票人:

<security:http auto-config="true" 
               use-expressions="true" 
               access-decision-manager-ref="accessDecisionManager">
...
</security:http>

<bean id="accessDecisionManager"
      class="org.springframework.security.access.vote.UnanimousBased">
    <property name="decisionVoters">
        <list>
            <bean class="com.acme.MyVoter" />
        </list>
    </property>
</bean>

...
如果您想更进一步,还可以编写自己的,这样可以删除投票者中的硬编码主机名检查,并执行以下操作:

<intercept-url pattern="/Admin/*" access="ROLE_GENERAL" domain="free.acme.com" />

在会话cookie中,域应显式设置为exampledomain.com

应用服务器负责会话cookie创建(JSESSIONID),但不负责Spring安全性

你所要做的就是通知你的应用服务器,你想在cookie中始终拥有相同的域

添加到web.xml中:

   <session-config>
        <cookie-config>
            <domain>exampledomain.com</domain>
        </cookie-config>
    </session-config>

exampledomain.com

您在子域中使用的是同一个webapp,而不是不同的webapp?您可以覆盖
FilterSecurityInterceptor
来添加子域检查,但闻起来像是黑客,不是真正的解决方案。将您的问题投票给开箱即用的解决方案。另外,您能否更具体地说明web应用程序/web容器子域的配置。Yeh对不同的子域使用相同的应用程序,不同的子域对用户表示不同的功能,但相同的服务器处理它(出于成本效益的原因)。对于web应用容器子域配置,有一个过滤器用于查找调用的子域,然后将请求转发到该域功能的相关操作。想象一下,所有人都可以访问的free.domain.com和pro.domain.com,它们的外观和感觉都比pro.domain.com好得多,添加了功能。在任何spring安全版本中,intercept-url标记都不会获得名为“domain”的属性。使用投票者可能是正确的,但这种解决方案永远不会起作用。