Java spring security - 拦截不同登录的子域名url?

And*_*ney 7 java security subdomain spring

我有一个安装了弹簧安全的应用程序并且运行良好 - 它目前用完了www.exampledomain.com.

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

唯一的问题是,对于这个新的应用程序,用户需要登录.在春季,通过网站截取网址非常容易 <intercept-url pattern="/Admin/*" access="ROLE_GENERAL"/>

但是当你想拦截一​​个子域进行登录时你会怎么做?例如,以下对我不起作用:

<intercept-url pattern="http://newapp.exampledomain.com/*" access="ROLE_GENERAL"/>
Run Code Online (Sandbox Code Playgroud)

有关如何解决这个问题的任何想法?

小智 2

一种选择是编写您自己的 AccessDecisionVoter,它扩展RoleVoter并添加基于主机名的附加检查。像这样的东西:

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后连接你的选民:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<intercept-url pattern="/Admin/*" access="ROLE_GENERAL" domain="free.acme.com" />
Run Code Online (Sandbox Code Playgroud)