Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC和shiro中处理不同URL集身份验证的多个领域_Java_Spring Mvc_Authentication_Web.xml_Shiro - Fatal编程技术网

Java 在SpringMVC和shiro中处理不同URL集身份验证的多个领域

Java 在SpringMVC和shiro中处理不同URL集身份验证的多个领域,java,spring-mvc,authentication,web.xml,shiro,Java,Spring Mvc,Authentication,Web.xml,Shiro,我正在努力为我的情况找到一个最佳实践解决方案。我的webapp中有两组不同的URL,需要针对两组不同的用户群进行安全保护/身份验证 例如,/foo/*URL只能由基于用户名/pwd令牌的特定用户集访问。另一组/bar/*URL只能由一组用户(存储在与第一组不同的数据库表中)通过username/pwd令牌机制访问 我想了解如何在web.xml中构造shiro过滤器,以及在applicationContext.xml中构造相应的shiro过滤器bean定义。我应该使用两个不同的shiro过滤器(每

我正在努力为我的情况找到一个最佳实践解决方案。我的webapp中有两组不同的URL,需要针对两组不同的用户群进行安全保护/身份验证

例如,/foo/*URL只能由基于用户名/pwd令牌的特定用户集访问。另一组/bar/*URL只能由一组用户(存储在与第一组不同的数据库表中)通过username/pwd令牌机制访问

我想了解如何在web.xml中构造shiro过滤器,以及在applicationContext.xml中构造相应的shiro过滤器bean定义。我应该使用两个不同的shiro过滤器(每个url模式一个),映射到上下文文件中的两个不同bean,其中每个bean都有自己的安全管理器,该安全管理器配置有自己的领域(每个领域负责根据相应的用户基表进行身份验证)

以下是两个链接:

但是我的应用程序中出现了错误。这里我粘贴了一些示例代码

ApplicationContext-shiro.xml

谢谢,
安基特

我现在已经解决了这个问题

在applicationContext.xml文件中,自定义验证器类的顺序错误。我首先编写了验证器属性,然后定义了领域

正在成功验证

    <!-- Single realm app (realm configured next, below). If you have multiple 
        realms, use the 'realms' property instead. -->
    <property name="realms">
        <util:list>
            <ref bean="adminAuthRealm" />
            <ref bean="vendorAuthRealm" />              
        </util:list>
    </property>

    <!-- <property name="authenticator">
        <bean class="com.yatra.mp.security.MultiLoginAuthenticator"/>
    </property> -->

    <property name="cacheManager" ref="cacheManager" />
    <!-- Uncomment this next property if you want heterogenous session access 
        or clusterable/distributable sessions. The default value is 'http' which 
        uses the Servlet container's HttpSession as the underlying Session implementation. -->
</bean>`<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager" />
    <property name="unauthorizedUrl" value="/permissionDenied.do" />

    <!-- The 'filters' property is usually not necessary unless performing 
        an override, which we want to do here (make authc point to a PassthruAuthenticationFilter 
        instead of the default FormAuthenticationFilter: -->

    <property name="filters">
        <util:map>
            <entry key="adminAuthc" value-ref="adminAuthc" />
            <entry key="vendorAuthc" value-ref="vendorAuthc" />
            <entry key="adminUser" value-ref="adminUser" />
        </util:map>
    </property>
    <property name="filterChainDefinitions">
        <value>
            /admin/welcome.do = anon
            /vendor/welcome.do = anon
            /vendor/signup.do = anon
            /admin/signup.do = anon
            /assets/** = anon
            /admin/** = adminAuthc
            /vendor/** = vendorAuthc
        </value>
    </property>
</bean>
<bean id="adminAuthc"
    class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter">
    <property name="loginUrl" value="/admin/login.do" />
    <property name="successUrl" value="/admin/home.do" />
</bean>
<bean id="vendorAuthc"
    class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter">
    <property name="loginUrl" value="/vendor/login.do" />
    <property name="successUrl" value="/vendor/home.do" />
</bean>
<bean id="adminUser" class="org.apache.shiro.web.filter.authc.UserFilter">
    <property name="loginUrl" value="/admin/login.do" />
</bean>

<bean id="vendorUser" class="org.apache.shiro.web.filter.authc.UserFilter">
    <property name="loginUrl" value="/vendor/login.do" />
</bean>`
public class MultiLoginAuthenticator extends ModularRealmAuthenticator {

@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
    assertRealmsConfigured();   <------- Here I am getting null getRealms(). So I am getting exceptions
    MultiLoginAuthenticationToken mlat = null;
    Realm loginRealm = null;

    if (!(authenticationToken instanceof MultiLoginAuthenticationToken)) {
        throw new AuthenticationException("Unrecognized token , not a typeof MultiLoginAuthenticationToken ");
    } else {
        mlat = (MultiLoginAuthenticationToken) authenticationToken;
        loginRealm = lookupRealm(mlat.getRealmName());
    }

    return doSingleRealmAuthentication(loginRealm, mlat);

}

protected Realm lookupRealm(String realmName) throws AuthenticationException {
    Collection<Realm> realms = getRealms();
    for (Realm realm : realms) {
        if (realm.getName().equalsIgnoreCase(realmName)) {
            return realm;
        }
    }
    throw new AuthenticationException("No realm configured for Client " + realmName);
}
@Override
public boolean supports(AuthenticationToken token) {
    if (token instanceof MultiLoginAuthenticationToken) {
        return ((MultiLoginAuthenticationToken)token).getRealmName().equalsIgnoreCase("VendorAuthRealm");
    }
    return false;
}

@Override
protected AuthorizationInfo doGetAuthorizationInfo(
        PrincipalCollection principals) {
    logger.debug("authorization info.....");
    logger.debug("realm size is : {}", principals.fromRealm(getName()).size());
    logger.debug("realm name is : {}", principals.fromRealm(getName()));

    int userId = (Integer) principals.fromRealm(getName()).iterator().next();

    VendorUser vendorUser = vendorUserService.getVendorUser(userId);
    if(vendorUser != null) {
        logger.debug("vendor user first name is : {}", vendorUser.getFirstName());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        if(vendorUser.getVendorProducts() != null){
            logger.debug("vendor users products and rate types...");
            info.addRoles(VendorYatraUtil.getProductSet(vendorUser.getVendorProducts()));
            info.addStringPermissions(VendorYatraUtil.getCarRateTypeSet(vendorUser.getVendorCarRateTypes()));
        }
        return info;
    }
    return null;
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(
        AuthenticationToken authcToken) throws AuthenticationException {
    MultiLoginAuthenticationToken token = (MultiLoginAuthenticationToken) authcToken;
    logger.debug("username is : {}", token.getUsername());

    if(vendorUserService.findVendorUser(token.getUsername()) != null){
        VendorUser vendorUser = vendorUserService.findVendorUser(token.getUsername()); 
        if (vendorUser != null){
            return new SimpleAuthenticationInfo(vendorUser.getId(),
                    vendorUser.getPassword(), getName());
        }
    }

    return null;
}