---
title: "spring-security4 无权限问题解决方案"
author: "hacker507"
date: "2019-04-19T09:25:53Z"
tags: []
categories: ["编程语言"]
slug: "spring-security4"
draft: false
lastmod: "2019-04-19T09:25:53Z"
id: 2440257
url: "https://hacker507.iteye.com/blog/2440257"
---

设置资源后无法进行访问，主要是由于spring security 在判断角色的时候，自动加上了前缀 ORLE_,直接上diamante就懂了；

类：org.springframework.security.access.expression.SecurityExpressionRoot


```java

public final boolean hasRole(String role) {
		return hasAnyRole(role);
	}

	public final boolean hasAnyRole(String... roles) {
		return hasAnyAuthorityName(defaultRolePrefix, roles);
	}

	private boolean hasAnyAuthorityName(String prefix, String... roles) {
		Set<String> roleSet = getAuthoritySet();

		for (String role : roles) {
			String defaultedRole = getRoleWithDefaultPrefix(prefix, role);
			if (roleSet.contains(defaultedRole)) {
				return true;
			}
		}

		return false;
	}
	private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String role) {
		if (role == null) {
			return role;
		}
		if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) {
			return role;
		}
		if (role.startsWith(defaultRolePrefix)) {
			return role;
		}
		return defaultRolePrefix + role;
	}

```