1. 概述
在本教程中,我們將重點介紹使用 Spring Security 創建自定義安全表達式。
有時,框架中提供的表達式可能不夠表達力。在這種情況下,構建一個語義上更豐富的自定義表達式相對簡單。
我們首先將討論如何創建自定義 PermissionEvaluator,然後創建一個完全自定義的表達式,最後將覆蓋內置的安全表達式。
2. 用户實體
首先,讓我們為創建新的安全表達式奠定基礎。
讓我們看看我們的 用户實體,該實體包含 權限和 組織:
@Entity
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String username;
private String password;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "users_privileges",
joinColumns =
@JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns =
@JoinColumn(name = "privilege_id", referencedColumnName = "id"))
private Set<Privilege> privileges;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "organization_id", referencedColumnName = "id")
private Organization organization;
// standard getters and setters
}以下是我們的簡單 特權:
@Entity
public class Privilege {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String name;
// standard getters and setters
}以及我們的 組織:
@Entity
public class Organization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String name;
// standard setters and getters
}最後,我們將使用一個更簡單的自定義 Principal:
public class MyUserPrincipal implements UserDetails {
private User user;
public MyUserPrincipal(User user) {
this.user = user;
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (Privilege privilege : user.getPrivileges()) {
authorities.add(new SimpleGrantedAuthority(privilege.getName()));
}
return authorities;
}
...
}現在有了這些類都準備就緒,我們將使用我們自定義的 Principal 在一個基本的 UserDetailsService 實現中:
@Service
public class MyUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
return new MyUserPrincipal(user);
}
}正如您所見,這些關係並不複雜——用户擁有一個或多個權限,並且每個用户都屬於一個組織。
3. 數據準備
接下來,讓我們使用簡單的測試數據初始化數據庫:
@Component
public class SetupData {
@Autowired
private UserRepository userRepository;
@Autowired
private PrivilegeRepository privilegeRepository;
@Autowired
private OrganizationRepository organizationRepository;
@PostConstruct
public void init() {
initPrivileges();
initOrganizations();
initUsers();
}
}以下是我們的 初始化方法:
private void initPrivileges() {
Privilege privilege1 = new Privilege("FOO_READ_PRIVILEGE");
privilegeRepository.save(privilege1);
Privilege privilege2 = new Privilege("FOO_WRITE_PRIVILEGE");
privilegeRepository.save(privilege2);
}private void initOrganizations() {
Organization org1 = new Organization("FirstOrg");
organizationRepository.save(org1);
Organization org2 = new Organization("SecondOrg");
organizationRepository.save(org2);
}private void initUsers() {
Privilege privilege1 = privilegeRepository.findByName("FOO_READ_PRIVILEGE");
Privilege privilege2 = privilegeRepository.findByName("FOO_WRITE_PRIVILEGE");
User user1 = new User();
user1.setUsername("john");
user1.setPassword("123");
user1.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1)));
user1.setOrganization(organizationRepository.findByName("FirstOrg"));
userRepository.save(user1);
User user2 = new User();
user2.setUsername("tom");
user2.setPassword("111");
user2.setPrivileges(new HashSet<Privilege>(Arrays.asList(privilege1, privilege2)));
user2.setOrganization(organizationRepository.findByName("SecondOrg"));
userRepository.save(user2);
}請注意:
- 用户“john”僅擁有FOO_READ_權限
- 用户“tom”同時擁有FOO_READ_權限 和 FOO_WRITE_權限
4. 自定義權限評估器
到目前為止,我們已經準備好開始實現我們的新表達式——通過一個新的、自定義的權限評估器。
我們將使用用户的權限來安全我們的方法——但我們不想使用硬編碼的權限名稱,而是希望實現一個更開放、更靈活的實現。
讓我們開始吧。
4.1. 權限評估器 (PermissionEvaluator)
為了創建我們自己的自定義權限評估器,我們需要實現 PermissionEvaluator 接口:
public class CustomPermissionEvaluator implements PermissionEvaluator {
@Override
public boolean hasPermission(
Authentication auth, Object targetDomainObject, Object permission) {
if ((auth == null) || (targetDomainObject == null) || !(permission instanceof String)){
return false;
}
String targetType = targetDomainObject.getClass().getSimpleName().toUpperCase();
return hasPrivilege(auth, targetType, permission.toString().toUpperCase());
}
@Override
public boolean hasPermission(
Authentication auth, Serializable targetId, String targetType, Object permission) {
if ((auth == null) || (targetType == null) || !(permission instanceof String)) {
return false;
}
return hasPrivilege(auth, targetType.toUpperCase(),
permission.toString().toUpperCase());
}
}以下是我們的 hasPrivilege() 方法:
private boolean hasPrivilege(Authentication auth, String targetType, String permission) {
for (GrantedAuthority grantedAuth : auth.getAuthorities()) {
if (grantedAuth.getAuthority().startsWith(targetType) &&
grantedAuth.getAuthority().contains(permission)) {
return true;
}
}
return false;
}我們現在擁有一個新的安全表達式,可以立即使用:hasPermission。
因此,不再使用更硬編碼的版本:
@PostAuthorize("hasAuthority('FOO_READ_PRIVILEGE')")我們可以使用:
@PostAuthorize("hasPermission(returnObject, 'read')")或
@PreAuthorize("hasPermission(#id, 'Foo', 'read')")注意: 指的是方法參數,Foo 指的是目標對象類型。
4.2. 方法安全配置
僅僅定義 CustomPermissionEvaluator 遠遠不夠,我們還需要將其應用於方法安全配置中:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
return expressionHandler;
}
}4.3. 實踐示例
現在,讓我們開始利用新的表達式——在幾個簡單的控制器方法中:
@Controller
public class MainController {
@PostAuthorize("hasPermission(returnObject, 'read')")
@GetMapping("/foos/{id}")
@ResponseBody
public Foo findById(@PathVariable long id) {
return new Foo("Sample");
}
@PreAuthorize("hasPermission(#foo, 'write')")
@PostMapping("/foos")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public Foo create(@RequestBody Foo foo) {
return foo;
}
}我們這就開始——我們現在都已準備就緒,並且在實踐中使用新的表達式。
4.4. 實時測試
現在,讓我們編寫一個簡單的實時測試,通過調用API並確保一切正常工作:
@Test
public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() {
Response response = givenAuth("john", "123").get("http://localhost:8082/foos/1");
assertEquals(200, response.getStatusCode());
assertTrue(response.asString().contains("id"));
}
@Test
public void givenUserWithNoWritePrivilegeAndHasPermission_whenPostFoo_thenForbidden() {
Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE)
.body(new Foo("sample"))
.post("http://localhost:8082/foos");
assertEquals(403, response.getStatusCode());
}
@Test
public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() {
Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE)
.body(new Foo("sample"))
.post("http://localhost:8082/foos");
assertEquals(201, response.getStatusCode());
assertTrue(response.asString().contains("id"));
}以下是我們的 givenAuth() 方法:
private RequestSpecification givenAuth(String username, String password) {
FormAuthConfig formAuthConfig =
new FormAuthConfig("http://localhost:8082/login", "username", "password");
return RestAssured.given().auth().form(username, password, formAuthConfig);
}5. 新的安全表達式
通過之前的解決方案,我們能夠定義和使用 hasPermission 表達式——這在某些情況下非常有用。
然而,我們仍然受到該表達式的名稱和語義的限制。
因此,在本節中,我們將採用完全自定義的方式——實現一個名為 isMember() 的安全表達式,檢查主體是否為組織成員。
5.1. 自定義方法安全表達式
為了創建此新的自定義表達式,我們需要首先實現根節點,所有安全表達式的評估都從這裏開始:
public class CustomMethodSecurityExpressionRoot
extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
public CustomMethodSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}
public boolean isMember(Long OrganizationId) {
User user = ((MyUserPrincipal) this.getPrincipal()).getUser();
return user.getOrganization().getId().longValue() == OrganizationId.longValue();
}
...
}現在我們如何在根音符中提供了這個新的操作;isMember() 用於檢查當前用户是否是指定Organization的成員。
此外,請注意我們如何擴展了SecurityExpressionRoot以包含內置表達式。
5.2. 自定義表達式處理器
接下來,我們需要在表達式處理器中注入我們的 CustomMethodSecurityExpressionRoot:
public class CustomMethodSecurityExpressionHandler
extends DefaultMethodSecurityExpressionHandler {
private AuthenticationTrustResolver trustResolver =
new AuthenticationTrustResolverImpl();
@Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(
Authentication authentication, MethodInvocation invocation) {
CustomMethodSecurityExpressionRoot root =
new CustomMethodSecurityExpressionRoot(authentication);
root.setPermissionEvaluator(getPermissionEvaluator());
root.setTrustResolver(this.trustResolver);
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
}5.3. 方法安全配置
現在,我們需要在方法安全配置中使用我們的 CustomMethodSecurityExpressionHandler:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
CustomMethodSecurityExpressionHandler expressionHandler =
new CustomMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
return expressionHandler;
}
}5.4. 使用新的表達式
以下是一個簡單的示例,使用 isMember() 方法來安全地保護我們的控制器方法:
@PreAuthorize("isMember(#id)")
@GetMapping("/organizations/{id}")
@ResponseBody
public Organization findOrgById(@PathVariable long id) {
return organizationRepository.findOne(id);
}5.5. 實時測試
以下是一個簡單的實時測試,針對用户“john”:
@Test
public void givenUserMemberInOrganization_whenGetOrganization_thenOK() {
Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/1");
assertEquals(200, response.getStatusCode());
assertTrue(response.asString().contains("id"));
}
@Test
public void givenUserMemberNotInOrganization_whenGetOrganization_thenForbidden() {
Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/2");
assertEquals(403, response.getStatusCode());
}6. 禁用內置安全表達式
最後,讓我們看看如何覆蓋內置安全表達式——我們將討論禁用 <em >hasAuthority()</em>。
6.1. 自定義安全表達式根
我們將以類似的方式開始,編寫我們自己的 <em >SecurityExpressionRoot</em>,主要原因是內置方法是 <em >final</em> 的,因此我們無法覆蓋它們:
public class MySecurityExpressionRoot implements MethodSecurityExpressionOperations {
public MySecurityExpressionRoot(Authentication authentication) {
if (authentication == null) {
throw new IllegalArgumentException("Authentication object cannot be null");
}
this.authentication = authentication;
}
@Override
public final boolean hasAuthority(String authority) {
throw new RuntimeException("method hasAuthority() not allowed");
}
...
}定義好這個根節點後,我們需要將其注入到表達式處理器中,然後將該處理器連接到我們的配置中——正如我們在第5節中所做的那樣。
6.2. 示例 – 使用表達式
現在,如果我們想使用 <em hasAuthority()</em> 來安全方法 – 如下所示,當我們嘗試訪問方法時,將會拋出 <em RuntimeException</em>:
@PreAuthorize("hasAuthority('FOO_READ_PRIVILEGE')")
@GetMapping("/foos")
@ResponseBody
public Foo findFooByName(@RequestParam String name) {
return new Foo(name);
}6.3. 實時測試
以下是我們的簡單測試:
@Test
public void givenDisabledSecurityExpression_whenGetFooByName_thenError() {
Response response = givenAuth("john", "123").get("http://localhost:8082/foos?name=sample");
assertEquals(500, response.getStatusCode());
}7. 結論
在本指南中,我們深入探討了如何在 Spring Security 中實現自定義安全表達式的各種方法,如果現有的方法無法滿足需求。