SEC-2984: Add @WithMockUser authorities property
This commit is contained in:
@@ -70,14 +70,33 @@ public @interface WithMockUser {
|
||||
String username() default "";
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The roles to use. The default is "USER". A {@link GrantedAuthority} will be created
|
||||
* for each value within roles. Each value in roles will automatically be prefixed
|
||||
* with "ROLE_". For example, the default will result in "ROLE_USER" being used.
|
||||
* </p>
|
||||
* <p>
|
||||
* If {@link #authorities()} is specified this property cannot be changed from the default.
|
||||
* </p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String[] roles() default { "USER" };
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The authorities to use. A {@link GrantedAuthority} will be created for each value.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* If this property is specified then {@link #roles()} is not used. This differs from
|
||||
* {@link #roles()} in that it does not prefix the values passed in automatically.
|
||||
* </p>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String[] authorities() default {};
|
||||
|
||||
/**
|
||||
* The password to be used. The default is "password".
|
||||
* @return
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.security.test.context.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
@@ -44,16 +45,26 @@ final class WithMockUserSecurityContextFactory implements
|
||||
throw new IllegalArgumentException(withUser
|
||||
+ " cannot have null username on both username and value properites");
|
||||
}
|
||||
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||
for (String role : withUser.roles()) {
|
||||
if (role.startsWith("ROLE_")) {
|
||||
throw new IllegalArgumentException("roles cannot start with ROLE_ Got "
|
||||
+ role);
|
||||
}
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
|
||||
|
||||
List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
|
||||
for (String authority : withUser.authorities()) {
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority(authority));
|
||||
}
|
||||
|
||||
if(grantedAuthorities.isEmpty()) {
|
||||
for (String role : withUser.roles()) {
|
||||
if (role.startsWith("ROLE_")) {
|
||||
throw new IllegalArgumentException("roles cannot start with ROLE_ Got "
|
||||
+ role);
|
||||
}
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role));
|
||||
}
|
||||
} else if(!(withUser.roles().length == 1 && "USER".equals(withUser.roles()[0]))) {
|
||||
throw new IllegalStateException("You cannot define roles attribute "+ Arrays.asList(withUser.roles())+" with authorities attribute "+ Arrays.asList(withUser.authorities()));
|
||||
}
|
||||
|
||||
User principal = new User(username, withUser.password(), true, true, true, true,
|
||||
authorities);
|
||||
grantedAuthorities);
|
||||
Authentication authentication = new UsernamePasswordAuthenticationToken(
|
||||
principal, principal.getPassword(), principal.getAuthorities());
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
|
||||
Reference in New Issue
Block a user