我正在尝试构建一个云解决方案,它将在子域上充当完全不同的应用程序。我现在面临的问题是在注册和登录期间处理这个子域。我们使用spring表单登录进行登录。每个注册的用户都应被视为仅该特定子域的用户,并且不应允许他从其他子域登录。所有子域都是动态创建的子域。
例如,在“ogre.mydomain.com”中拥有帐户的名为“shrek”的用户不应被允许从“human.mydomain.com”登录,除非从“human.mydomain.com”注册。 com”具有相同的名称或不同的名称。
我尝试过的
我尝试将子域与用户关联,并通过在登录期间将子域附加到用户名来验证用户。
这种方法的问题是每次我在 Controller 中请求主体时,我都会将域作为用户名的一部分。
我只是想知道是否有更好的方法来做到这一点。
最佳答案
您应该做的是为每个子域指定一个角色,而不是在用户名中使用子域。
当用户在 ogre.mydomain.com
中注册名为 shrek
时,该用户将自动获得名为 ROLE_OGRE_USER
的角色,并具有遵循 Spring 安全限制。
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 ("ogre".equals(subdomain)) {
if(authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_OGRE_USER"))) {
return ACCESS_GRANTED;
} else {
return ACCESS_DENIED;
}
}
else {
return 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="MyVoter" />
</list>
</property>
阅读此内容 SO post
关于java - spring security认证添加子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471839/