本文整理汇总了Java中org.apache.shiro.authz.AuthorizationException类的典型用法代码示例。如果您正苦于以下问题:Java AuthorizationException类的具体用法?Java AuthorizationException怎么用?Java AuthorizationException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AuthorizationException类属于org.apache.shiro.authz包,在下文中一共展示了AuthorizationException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGetAuthorizationInfo
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principalCollection) {
if (principalCollection == null) {
throw new AuthorizationException("Principal is not null!");
}
Shiro shiro = (Shiro) principalCollection.getPrimaryPrincipal();
User entity = new User();
entity.setId(shiro.getId());
entity = (User) service.iUserService.select(entity);
if (null == entity) {
throw new UnknownAccountException("No account found for user ["
+ shiro.getId() + "]");
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
return info;
}
示例2: doGetAuthorizationInfo
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//null usernames are invalid
if (principals == null) {
throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
}
String username = (String) getAvailablePrincipal(principals);
Set<String> roleNames = new HashSet<>();
roleNames.add(this.userService.findByUsername(username).getRole().getName());
AuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
/**
* If you want to do Permission Based authorization, you can grab the Permissions List associated to your user:
* For example:
* Set<String> permissions = new HashSet<>();
* permissions.add(this.userService.findByUsername(username).getRole().getPermissions());
* ((SimpleAuthorizationInfo)info).setStringPermissions(permissions);
*/
return info;
}
示例3: createSession
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
/**
* @since 1.0
*/
protected Session createSession(SessionContext sessionContext) throws AuthorizationException {
if (!WebUtils.isHttp(sessionContext)) {
String msg = "SessionContext must be an HTTP compatible implementation.";
throw new IllegalArgumentException(msg);
}
HttpServletRequest request = WebUtils.getHttpRequest(sessionContext);
HttpSession httpSession = request.getSession();
//SHIRO-240: DO NOT use the 'globalSessionTimeout' value here on the acquired session.
//see: https://issues.apache.org/jira/browse/SHIRO-240
String host = getHost(sessionContext);
return createSession(httpSession, host);
}
示例4: assertAuthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
/**
* Ensures that the calling <code>Subject</code> has the Annotation's specified roles, and if not, throws an
* <code>AuthorizingException</code> indicating that access is denied.
*
* @param a the RequiresRoles annotation to use to check for one or more roles
* @throws org.apache.shiro.authz.AuthorizationException
* if the calling <code>Subject</code> does not have the role(s) necessary to
* proceed.
*/
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresRoles)) return;
RequiresRoles rrAnnotation = (RequiresRoles) a;
String[] roles = rrAnnotation.value();
if (roles.length == 1) {
getSubject().checkRole(roles[0]);
return;
}
if (Logical.AND.equals(rrAnnotation.logical())) {
getSubject().checkRoles(Arrays.asList(roles));
return;
}
if (Logical.OR.equals(rrAnnotation.logical())) {
// Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
boolean hasAtLeastOneRole = false;
for (String role : roles) if (getSubject().hasRole(role)) hasAtLeastOneRole = true;
// Cause the exception if none of the role match, note that the exception message will be a bit misleading
if (!hasAtLeastOneRole) getSubject().checkRole(roles[0]);
}
}
示例5: assertAuthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
/**
* Ensures that the calling <code>Subject</code> has the Annotation's specified permissions, and if not, throws an
* <code>AuthorizingException</code> indicating access is denied.
*
* @param a the RequiresPermission annotation being inspected to check for one or more permissions
* @throws org.apache.shiro.authz.AuthorizationException
* if the calling <code>Subject</code> does not have the permission(s) necessary to
* continue access or execution.
*/
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresPermissions)) return;
RequiresPermissions rpAnnotation = (RequiresPermissions) a;
String[] perms = getAnnotationValue(a);
Subject subject = getSubject();
if (perms.length == 1) {
subject.checkPermission(perms[0]);
return;
}
if (Logical.AND.equals(rpAnnotation.logical())) {
getSubject().checkPermissions(perms);
return;
}
if (Logical.OR.equals(rpAnnotation.logical())) {
// Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
boolean hasAtLeastOnePermission = false;
for (String permission : perms) if (getSubject().isPermitted(permission)) hasAtLeastOnePermission = true;
// Cause the exception if none of the role match, note that the exception message will be a bit misleading
if (!hasAtLeastOnePermission) getSubject().checkPermission(perms[0]);
}
}
示例6: assertAuthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Override
public void assertAuthorized() throws AuthorizationException {
//if (!(annotation instanceof RequiresRoles)) return;
RequiresRoles rrAnnotation = (RequiresRoles) annotation;
String[] roles = rrAnnotation.value();
if (roles.length == 1) {
getSubject().checkRole(roles[0]);
return;
}
if (Logical.AND.equals(rrAnnotation.logical())) {
getSubject().checkRoles(Arrays.asList(roles));
return;
}
if (Logical.OR.equals(rrAnnotation.logical())) {
// Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
boolean hasAtLeastOneRole = false;
for (String role : roles) if (getSubject().hasRole(role)) hasAtLeastOneRole = true;
// Cause the exception if none of the role match, note that the exception message will be a bit misleading
if (!hasAtLeastOneRole) getSubject().checkRole(roles[0]);
}
}
示例7: testHandle_unauthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Test(expected = AuthorizationException.class)
public void testHandle_unauthorized() throws IOException {
when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(RawFormat.NAME), eq(BreadActions.EDIT), any()))
.thenReturn(false);
ComponentUpload component = new ComponentUpload();
component.getFields().put("directory", "org/apache/maven");
AssetUpload asset = new AssetUpload();
asset.getFields().put("filename", "foo.jar");
asset.setPayload(jarPayload);
component.getAssetUploads().add(asset);
asset = new AssetUpload();
asset.getFields().put("filename", "bar.jar");
asset.setPayload(sourcesPayload);
component.getAssetUploads().add(asset);
underTest.handle(repository, component);
}
示例8: testHandle_unauthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Test(expected = AuthorizationException.class)
public void testHandle_unauthorized() throws IOException {
when(contentPermissionChecker.isPermitted(eq(REPO_NAME), eq(Maven2Format.NAME), eq(BreadActions.EDIT), any()))
.thenReturn(false);
ComponentUpload componentUpload = new ComponentUpload();
componentUpload.getFields().put("groupId", "org.apache.maven");
componentUpload.getFields().put("artifactId", "tomcat");
componentUpload.getFields().put("version", "5.0.28");
AssetUpload assetUpload = new AssetUpload();
assetUpload.getFields().put("extension", "jar");
assetUpload.setPayload(jarPayload);
componentUpload.getAssetUploads().add(assetUpload);
underTest.handle(repository, componentUpload);
}
示例9: convert
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Override
protected Response convert(final AuthorizationException exception, final String id) {
HttpServletRequest httpRequest = httpRequestProvider.get();
if (httpRequest.getAttribute(ANONYMOUS_LOGIN) != null) {
// user is authenticated
String scheme = (String) httpRequest.getAttribute(AUTH_SCHEME_KEY);
String realm = (String) httpRequest.getAttribute(AUTH_REALM_KEY);
return Response.status(Status.UNAUTHORIZED)
.header(AUTHENTICATE_HEADER, String.format("%s realm=\"%s\"", scheme, realm))
.build();
}
return Response.status(Status.FORBIDDEN).build();
}
示例10: deleteComponent
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@Override
public void deleteComponent(final Repository repository, final Component component) {
checkNotNull(repository);
checkNotNull(component);
String repositoryFormat = repository.getFormat().toString();
VariableResolverAdapter variableResolverAdapter = variableResolverAdapterManager.get(repositoryFormat);
StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get();
try {
storageTx.begin();
for (Asset asset : storageTx.browseAssets(component)) {
if (!canDeleteAssetInRepository(repository, repositoryFormat, variableResolverAdapter, asset)) {
throw new AuthorizationException();
}
}
}
finally {
storageTx.close();
}
getComponentMaintenanceFacet(repository).deleteComponent(component.getEntityMetadata().getId());
}
示例11: doGetAuthorizationInfo
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//null usernames are invalid
if (principals == null) {
throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
}
String username = (String) getAvailablePrincipal(principals);
java.util.Set<String> roles = redisManager.smembers(user_roles_KeyPrefix + username);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
if (permissionsLookupEnabled) {
java.util.List<java.lang.String> permissionsList = redisManager.hmget(roles_permissions_Key, roles.toArray(new String[0]));
Set<String> permissionsSet = new HashSet<String>(permissionsList.size());
permissionsSet.addAll(permissionsList);
info.setStringPermissions(permissionsSet);
}
return info;
}
示例12: find
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
public Iterable<ObjBase> find(Map<String, Object> query, int hitmax){
query = addSubSchemas(query);
List<ObjBase> result = connection.get(query, hitmax);
//TODO: also add converted instances
//filter results for permission
List<ObjBase> filteredResult = new ArrayList<>();
for (ObjBase obj : result){
try{
checkPriv(obj.getId(), "view");
filteredResult.add(obj);
} catch (AuthorizationException e){
}
}
return filteredResult;
}
示例13: doGetAuthorizationInfo
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
/**
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
if (principalCollection == null) {
throw new AuthorizationException(
"The principal collections can't be null");
}
final String username =
(String) this.getAvailablePrincipal(principalCollection);
final List<GroupPermission> groupPermissions =
this.accountService.loadUserPermissions(username);
final Set<String> authorizationKeys = groupPermissions
.parallelStream()
.map(GroupPermission::getAuthorizationKey)
.collect(Collectors.toSet());
return new SimpleAuthorizationInfo(authorizationKeys);
}
示例14: assertAuthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
public void assertAuthorized(final Object resource, final Method handler) throws AuthorizationException {
super.assertAuthorized(new MethodInvocation() {
@Override
public Object proceed() throws Throwable {
return null;
}
@Override
public Method getMethod() {
return handler;
}
@Override
public Object[] getArguments() {
return new Object[0];
}
@Override
public Object getThis() {
return resource;
}
});
}
示例15: assertAuthorized
import org.apache.shiro.authz.AuthorizationException; //导入依赖的package包/类
public void assertAuthorized(final ExecutionContext context) throws AuthorizationException {
super.assertAuthorized(new MethodInvocation() {
@Override
public Object proceed() throws Throwable {
return null;
}
@Override
public Method getMethod() {
return context.getHandler();
}
@Override
public Object[] getArguments() {
return new Object[0];
}
@Override
public Object getThis() {
return context.getActionBean();
}
});
}