Java tutorial
/******************************************************************************* * Copyright 2008(c) The OBiBa Consortium. All rights reserved. * * This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package org.obiba.opal.core.runtime.security; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAccount; import org.apache.shiro.authc.credential.AllowAllCredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.stereotype.Component; @Component public class BackgroundJobRealm extends AuthorizingRealm { public BackgroundJobRealm() { setCredentialsMatcher(new AllowAllCredentialsMatcher()); } @Override public boolean supports(AuthenticationToken token) { return token instanceof BackgroundJobServiceAuthToken; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // BackgroundJobServiceAuthToken jobToken = (BackgroundJobServiceAuthToken) token; return new SimpleAccount(new SystemPrincipal(), null, getName()); } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); if (principals.oneByType(SystemPrincipal.class) != null) { info.addStringPermission("*"); } return info; } private static class SystemPrincipal { @Override public String toString() { return "opal/system"; } } }