Skip to content

Commit 2bac6f8

Browse files
committed
[BAEL-1411] Format code
1 parent 1b7e695 commit 2bac6f8

7 files changed

Lines changed: 145 additions & 147 deletions

File tree

‎spring-security-core/src/main/java/org/baeldung/methodsecurity/annotation/IsViewer.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
@Target(ElementType.METHOD)
1111
@Retention(RetentionPolicy.RUNTIME)
1212
@PreAuthorize("hasRole('VIEWER')")
13-
public @interface IsViewer
14-
{
13+
public @interface IsViewer {
1514
}

‎spring-security-core/src/main/java/org/baeldung/methodsecurity/entity/CustomUser.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import org.springframework.security.core.userdetails.User;
77

88
@SuppressWarnings("serial")
9-
public class CustomUser extends User{
10-
9+
public class CustomUser extends User {
10+
1111
private String nickName;
1212

1313
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
1414
super(username, password, authorities);
1515
}
16-
17-
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities,String nickName) {
16+
17+
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities, String nickName) {
1818
super(username, password, authorities);
1919
this.nickName = nickName;
2020
}

‎spring-security-core/src/main/java/org/baeldung/methodsecurity/repository/UserRoleRepository.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@
1313

1414
@Service
1515
public class UserRoleRepository {
16-
17-
static Map<String,CustomUser> DB_BASED_USER_MAPPING;
18-
19-
static{
16+
17+
static Map<String, CustomUser> DB_BASED_USER_MAPPING;
18+
19+
static {
2020
DB_BASED_USER_MAPPING = new LinkedHashMap<>();
21-
DB_BASED_USER_MAPPING.put("jane", new CustomUser("jane","1234", getGrantedAuthorities("ROLE_USER","ROLE_VIEWER"),"jane"));
22-
DB_BASED_USER_MAPPING.put("john", new CustomUser("john","1234", getGrantedAuthorities("ROLE_EDITOR","ROLE_ADMIN"),"jane"));
23-
DB_BASED_USER_MAPPING.put("jack", new CustomUser("jack","1234", getGrantedAuthorities("ROLE_USER","ROLE_REVIEWER"),"jane"));
21+
DB_BASED_USER_MAPPING.put("jane", new CustomUser("jane", "1234", getGrantedAuthorities("ROLE_USER", "ROLE_VIEWER"), "jane"));
22+
DB_BASED_USER_MAPPING.put("john", new CustomUser("john", "1234", getGrantedAuthorities("ROLE_EDITOR", "ROLE_ADMIN"), "jane"));
23+
DB_BASED_USER_MAPPING.put("jack", new CustomUser("jack", "1234", getGrantedAuthorities("ROLE_USER", "ROLE_REVIEWER"), "jane"));
2424
}
25-
26-
private static List<GrantedAuthority> getGrantedAuthorities(String...roles){
25+
26+
private static List<GrantedAuthority> getGrantedAuthorities(String... roles) {
2727
ArrayList<GrantedAuthority> authorities = new ArrayList<>();
28-
for (String role : roles){
28+
for (String role : roles) {
2929
authorities.add(new SimpleGrantedAuthority(role));
3030
}
3131
return authorities;
3232
}
33-
34-
public CustomUser loadUserByUserName(String username){
35-
if (DB_BASED_USER_MAPPING.containsKey(username)){
33+
34+
public CustomUser loadUserByUserName(String username) {
35+
if (DB_BASED_USER_MAPPING.containsKey(username)) {
3636
return DB_BASED_USER_MAPPING.get(username);
3737
}
38-
throw new UsernameNotFoundException("User "+username+" cannot be found");
38+
throw new UsernameNotFoundException("User " + username + " cannot be found");
3939
}
40-
41-
public boolean isValidUsername(String username){
40+
41+
public boolean isValidUsername(String username) {
4242
return DB_BASED_USER_MAPPING.containsKey(username);
4343
}
44-
45-
public boolean isValidRole(String roleName){
44+
45+
public boolean isValidRole(String roleName) {
4646
return roleName.startsWith("ROLE_");
4747
}
48-
49-
public List<String> getAllUsernames(){
48+
49+
public List<String> getAllUsernames() {
5050
List<String> usernames = new ArrayList<>();
5151
usernames.add("jane");
5252
usernames.add("john");
5353
usernames.add("jack");
5454
return usernames;
5555
}
56-
56+
5757
}

‎spring-security-core/src/main/java/org/baeldung/methodsecurity/service/UserRoleService.java‎

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,88 +20,87 @@
2020

2121
@Service
2222
public class UserRoleService {
23-
23+
2424
@Autowired
2525
UserRoleRepository userRoleRepository;
26-
26+
2727
@Secured("ROLE_VIEWER")
28-
public String getUsername(){
28+
public String getUsername() {
2929
SecurityContext securityContext = SecurityContextHolder.getContext();
3030
return securityContext.getAuthentication().getName();
3131
}
32-
33-
@Secured({"ROLE_VIEWER","ROLE_EDITOR"})
34-
public boolean isValidUsername(String username){
32+
33+
@Secured({ "ROLE_VIEWER", "ROLE_EDITOR" })
34+
public boolean isValidUsername(String username) {
3535
return userRoleRepository.isValidUsername(username);
3636
}
37-
37+
3838
@RolesAllowed("ROLE_VIEWER")
39-
public String getUsername2(){
39+
public String getUsername2() {
4040
SecurityContext securityContext = SecurityContextHolder.getContext();
4141
return securityContext.getAuthentication().getName();
4242
}
43-
44-
@RolesAllowed({"ROLE_VIEWER","ROLE_EDITOR"})
45-
public boolean isValidUsername2(String username){
43+
44+
@RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" })
45+
public boolean isValidUsername2(String username) {
4646
return userRoleRepository.isValidUsername(username);
4747
}
48-
48+
4949
@PreAuthorize("hasRole('ROLE_VIEWER')")
50-
public String getUsernameInUpperCase(){
50+
public String getUsernameInUpperCase() {
5151
return getUsername().toUpperCase();
5252
}
53-
53+
5454
@PreAuthorize("hasAuthority('SYS_ADMIN')")
55-
public String getUsernameInLowerCase(){
55+
public String getUsernameLC() {
5656
return getUsername().toLowerCase();
5757
}
58-
58+
5959
@PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')")
60-
public boolean isValidUsername3(String username){
60+
public boolean isValidUsername3(String username) {
6161
return userRoleRepository.isValidUsername(username);
6262
}
63-
63+
6464
@PreAuthorize("#username == authentication.principal.username")
65-
public String getMyRoles(String username){
65+
public String getMyRoles(String username) {
6666
SecurityContext securityContext = SecurityContextHolder.getContext();
6767
return securityContext
6868
.getAuthentication()
6969
.getAuthorities()
70-
.stream().map(auth -> auth.getAuthority())
71-
.collect(Collectors.joining(","));
70+
.stream()
71+
.map(auth -> auth.getAuthority()).collect(Collectors.joining(","));
7272
}
73-
73+
7474
@PostAuthorize("returnObject.username == authentication.principal.nickName")
75-
public CustomUser loadUserDetail(String username){
75+
public CustomUser loadUserDetail(String username) {
7676
return userRoleRepository.loadUserByUserName(username);
7777
}
78-
78+
7979
@PreFilter("filterObject != authentication.principal.username")
80-
public String joinUsernames(List<String> usernames){
80+
public String joinUsernames(List<String> usernames) {
8181
return usernames.stream().collect(Collectors.joining(";"));
8282
}
83-
84-
@PreFilter(value="filterObject != authentication.principal.username",filterTarget="usernames")
85-
public String joinUsernamesAndRoles(List<String> usernames,List<String> roles){
86-
return usernames.stream().collect(Collectors.joining(";"))
87-
+":"+roles.stream().collect(Collectors.joining(";"));
83+
84+
@PreFilter(value = "filterObject != authentication.principal.username", filterTarget = "usernames")
85+
public String joinUsernamesAndRoles(List<String> usernames, List<String> roles) {
86+
return usernames.stream().collect(Collectors.joining(";")) + ":" + roles.stream().collect(Collectors.joining(";"));
8887
}
89-
88+
9089
@PostFilter("filterObject != authentication.principal.username")
91-
public List<String> getAllUsernamesExceptCurrent(){
90+
public List<String> getAllUsernamesExceptCurrent() {
9291
return userRoleRepository.getAllUsernames();
9392
}
94-
93+
9594
@IsViewer
96-
public String getUsername4(){
95+
public String getUsername4() {
9796
SecurityContext securityContext = SecurityContextHolder.getContext();
9897
return securityContext.getAuthentication().getName();
9998
}
100-
99+
101100
@PreAuthorize("#username == authentication.principal.username")
102101
@PostAuthorize("returnObject.username == authentication.principal.nickName")
103-
public CustomUser securedLoadUserDetail(String username){
102+
public CustomUser securedLoadUserDetail(String username) {
104103
return userRoleRepository.loadUserByUserName(username);
105104
}
106-
105+
107106
}

0 commit comments

Comments
 (0)