拦截不同登录的子域url?
我有一个应用程序安装了弹簧安全,并且运行良好 - 它目前已经用完了www.exampledomain.com
。
我现在想要扩展应用程序运行一个子域。 例如newapp.exampledomain.com
。
唯一的问题是用户需要登录这个新的应用程序。在春天,通过<intercept-url pattern="/Admin/*" access="ROLE_GENERAL"/>
拦截url非常容易。
但是当你想拦截一个子域名登录时,你会做什么? 例如以下不适合我:
<intercept-url pattern="http://newapp.exampledomain.com/*" access="ROLE_GENERAL"/>
有关如何解决这个问题的任何想法?
一种选择是编写自己的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);
}
}
}
然后连线你的选民:
<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 Security。
您所要做的就是通知您的应用服务器,您希望始终在Cookie中拥有相同的域。
添加到你的web.xml中:
<session-config>
<cookie-config>
<domain>exampledomain.com</domain>
</cookie-config>
</session-config>
链接地址: http://www.djcxy.com/p/10603.html