OR
別のオプションがあります: の代わりに提供されたロール セットを使用するロール フィルターのカスタム実装ですAND
。
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
/**
* Allows access if current user has at least one role of the specified list.
*
* Basically, it's the same as {@link RolesAuthorizationFilter} but using {@literal OR} instead
* of {@literal AND} on the specified roles.
*
* @see RolesAuthorizationFilter
* @author Andy Belsky
*/
public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter {
@Override
public boolean isAccessAllowed(ServletRequest request, ServletResponse response,
Object mappedValue) throws IOException {
final Subject subject = getSubject(request, response);
final String[] rolesArray = (String[]) mappedValue;
if (rolesArray == null || rolesArray.length == 0) {
//no roles specified, so nothing to check - allow access.
return true;
}
for (String roleName : rolesArray) {
if (subject.hasRole(roleName)) {
return true;
}
}
return false;
}
}
での使い方shiro.ini
はこんな感じです。
[main]
...
anyofroles = com.your.package.AnyOfRolesAuthorizationFilter
[urls]
...
/path/to/some/url = anyofroles["role1,role2"]