Last active
October 18, 2018 12:24
-
-
Save thetekst/aad80723f11c74206aeac8604b5a4402 to your computer and use it in GitHub Desktop.
Spring Boot Security Role and using @secured for controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@Slf4j | |
@RequiredArgsConstructor | |
@RequestMapping("channel") | |
public class MyController { | |
// Если бы мы в UserPrincipal добавили public final static String ROLE_ADMIN_NAME = ROLE_PREFIX + Role.ADMIN; | |
// или Role.ADMIN.name(), | |
// то при использовании @Secured(UserPrincipal.ROLE_ADMIN_NAME) мы бы получили: Attribute value must be constant. | |
// Т.к. здесь используется вызов функции Role. или Role.ADMIN., а это уже не константа | |
@Secured(Role.ROLE_ADMIN_NAME) | |
@GetMapping("all") | |
public List<Animal> getAll() { | |
return AnimalService.getAll(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before | |
@Getter | |
@AllArgsConstructor | |
@ToString | |
public enum Role1 { | |
ADMIN("Administrator"), OPERATOR("Operator"); | |
private final String title; | |
public static Role getType(final String title) { | |
return Arrays.stream(values()).filter(e -> title.equals(e.title)).findFirst().orElse(null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After | |
@Getter | |
@AllArgsConstructor | |
@ToString | |
public enum Role2 { | |
ADMIN("Administrator"), OPERATOR("Operator"); | |
public final static String ROLE_PREFIX = "ROLE_"; // ADD | |
public final static String ROLE_ADMIN_NAME = ROLE_PREFIX + "ADMIN"; // ADD | |
public final static String ROLE_OPERATOR_NAME = ROLE_PREFIX + "OPERATOR"; // ADD | |
private final String title; | |
public static Role getType(final String title) { | |
return Arrays.stream(values()).filter(e -> title.equals(e.title)).findFirst().orElse(null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@Configuration | |
@EnableWebSecurity | |
@RequiredArgsConstructor | |
@EnableGlobalMethodSecurity(securedEnabled = true) // add for @Secured | |
public class SecurityConfig extends WebSecurityConfigurerAdapter { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before | |
@Data | |
@ToString(exclude = {"password"}) | |
public class UserPrincipal1 implements UserDetails { | |
private final static String ROLE_PREFIX = "ROLE_"; | |
private Long id; | |
private String email; | |
@JsonIgnore | |
private String password; | |
private Boolean disabled; | |
private Map<Workspace, Role> workspaces; | |
@JsonIgnore | |
@Override | |
public Collection<? extends GrantedAuthority> getAuthorities() { | |
final Set<GrantedAuthority> authorities = new HashSet<>(); | |
workspaces.forEach((k, v) -> authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + v.name()))); | |
return authorities; | |
} | |
@JsonIgnore | |
@Override | |
public String getPassword() { | |
return password; | |
} | |
@JsonIgnore | |
@Override | |
public String getUsername() { | |
return email; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isAccountNonExpired() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isAccountNonLocked() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isCredentialsNonExpired() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isEnabled() { | |
return !disabled; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After | |
@Data | |
@ToString(exclude = {"password"}) | |
public class UserPrincipal2 implements UserDetails { | |
// moved to Role private final static String ROLE_PREFIX = "ROLE_"; and change access level modifiers to public | |
private Long id; | |
private String email; | |
@JsonIgnore | |
private String password; | |
private Boolean disabled; | |
private Map<Workspace, Role> workspaces; | |
@JsonIgnore | |
@Override | |
public Collection<? extends GrantedAuthority> getAuthorities() { | |
final Set<GrantedAuthority> authorities = new HashSet<>(); | |
workspaces.forEach((k, v) -> authorities.add(new SimpleGrantedAuthority(Role.ROLE_PREFIX + v.name()))); // change to Role.ROLE_PREFIX | |
return authorities; | |
// если не используется, то return Collections.emptyList(); | |
} | |
@JsonIgnore | |
@Override | |
public String getPassword() { | |
return password; | |
} | |
@JsonIgnore | |
@Override | |
public String getUsername() { | |
return email; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isAccountNonExpired() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isAccountNonLocked() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isCredentialsNonExpired() { | |
return !disabled; | |
} | |
@JsonIgnore | |
@Override | |
public boolean isEnabled() { | |
return !disabled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment