From 2bac6f88e71615c714f9a740c94bd98c44386533 Mon Sep 17 00:00:00 2001 From: linhvovn Date: Sun, 31 Dec 2017 23:32:33 +0800 Subject: [PATCH] [BAEL-1411] Format code --- .../methodsecurity/annotation/IsViewer.java | 3 +- .../methodsecurity/entity/CustomUser.java | 8 +- .../repository/UserRoleRepository.java | 42 +++---- .../service/UserRoleService.java | 73 ++++++----- .../methodsecurity/TestMethodSecurity.java | 116 +++++++++--------- .../TestWithMockUserAtClassLevel.java | 12 +- .../methodsecurity/TestWithUserDetails.java | 38 +++--- 7 files changed, 145 insertions(+), 147 deletions(-) diff --git a/spring-security-core/src/main/java/org/baeldung/methodsecurity/annotation/IsViewer.java b/spring-security-core/src/main/java/org/baeldung/methodsecurity/annotation/IsViewer.java index da933fb19f..711784adbb 100644 --- a/spring-security-core/src/main/java/org/baeldung/methodsecurity/annotation/IsViewer.java +++ b/spring-security-core/src/main/java/org/baeldung/methodsecurity/annotation/IsViewer.java @@ -10,6 +10,5 @@ import org.springframework.security.access.prepost.PreAuthorize; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @PreAuthorize("hasRole('VIEWER')") -public @interface IsViewer -{ +public @interface IsViewer { } \ No newline at end of file diff --git a/spring-security-core/src/main/java/org/baeldung/methodsecurity/entity/CustomUser.java b/spring-security-core/src/main/java/org/baeldung/methodsecurity/entity/CustomUser.java index 19a7719373..fb9174befa 100644 --- a/spring-security-core/src/main/java/org/baeldung/methodsecurity/entity/CustomUser.java +++ b/spring-security-core/src/main/java/org/baeldung/methodsecurity/entity/CustomUser.java @@ -6,15 +6,15 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; @SuppressWarnings("serial") -public class CustomUser extends User{ - +public class CustomUser extends User { + private String nickName; public CustomUser(String username, String password, Collection authorities) { super(username, password, authorities); } - - public CustomUser(String username, String password, Collection authorities,String nickName) { + + public CustomUser(String username, String password, Collection authorities, String nickName) { super(username, password, authorities); this.nickName = nickName; } diff --git a/spring-security-core/src/main/java/org/baeldung/methodsecurity/repository/UserRoleRepository.java b/spring-security-core/src/main/java/org/baeldung/methodsecurity/repository/UserRoleRepository.java index 82e74f0cd0..fc1a32289d 100644 --- a/spring-security-core/src/main/java/org/baeldung/methodsecurity/repository/UserRoleRepository.java +++ b/spring-security-core/src/main/java/org/baeldung/methodsecurity/repository/UserRoleRepository.java @@ -13,45 +13,45 @@ import org.springframework.stereotype.Service; @Service public class UserRoleRepository { - - static Map DB_BASED_USER_MAPPING; - - static{ + + static Map DB_BASED_USER_MAPPING; + + static { DB_BASED_USER_MAPPING = new LinkedHashMap<>(); - DB_BASED_USER_MAPPING.put("jane", new CustomUser("jane","1234", getGrantedAuthorities("ROLE_USER","ROLE_VIEWER"),"jane")); - DB_BASED_USER_MAPPING.put("john", new CustomUser("john","1234", getGrantedAuthorities("ROLE_EDITOR","ROLE_ADMIN"),"jane")); - DB_BASED_USER_MAPPING.put("jack", new CustomUser("jack","1234", getGrantedAuthorities("ROLE_USER","ROLE_REVIEWER"),"jane")); + DB_BASED_USER_MAPPING.put("jane", new CustomUser("jane", "1234", getGrantedAuthorities("ROLE_USER", "ROLE_VIEWER"), "jane")); + DB_BASED_USER_MAPPING.put("john", new CustomUser("john", "1234", getGrantedAuthorities("ROLE_EDITOR", "ROLE_ADMIN"), "jane")); + DB_BASED_USER_MAPPING.put("jack", new CustomUser("jack", "1234", getGrantedAuthorities("ROLE_USER", "ROLE_REVIEWER"), "jane")); } - - private static List getGrantedAuthorities(String...roles){ + + private static List getGrantedAuthorities(String... roles) { ArrayList authorities = new ArrayList<>(); - for (String role : roles){ + for (String role : roles) { authorities.add(new SimpleGrantedAuthority(role)); } return authorities; } - - public CustomUser loadUserByUserName(String username){ - if (DB_BASED_USER_MAPPING.containsKey(username)){ + + public CustomUser loadUserByUserName(String username) { + if (DB_BASED_USER_MAPPING.containsKey(username)) { return DB_BASED_USER_MAPPING.get(username); } - throw new UsernameNotFoundException("User "+username+" cannot be found"); + throw new UsernameNotFoundException("User " + username + " cannot be found"); } - - public boolean isValidUsername(String username){ + + public boolean isValidUsername(String username) { return DB_BASED_USER_MAPPING.containsKey(username); } - - public boolean isValidRole(String roleName){ + + public boolean isValidRole(String roleName) { return roleName.startsWith("ROLE_"); } - - public List getAllUsernames(){ + + public List getAllUsernames() { List usernames = new ArrayList<>(); usernames.add("jane"); usernames.add("john"); usernames.add("jack"); return usernames; } - + } diff --git a/spring-security-core/src/main/java/org/baeldung/methodsecurity/service/UserRoleService.java b/spring-security-core/src/main/java/org/baeldung/methodsecurity/service/UserRoleService.java index 7379ee5223..c980334e82 100644 --- a/spring-security-core/src/main/java/org/baeldung/methodsecurity/service/UserRoleService.java +++ b/spring-security-core/src/main/java/org/baeldung/methodsecurity/service/UserRoleService.java @@ -20,88 +20,87 @@ import org.springframework.stereotype.Service; @Service public class UserRoleService { - + @Autowired UserRoleRepository userRoleRepository; - + @Secured("ROLE_VIEWER") - public String getUsername(){ + public String getUsername() { SecurityContext securityContext = SecurityContextHolder.getContext(); return securityContext.getAuthentication().getName(); } - - @Secured({"ROLE_VIEWER","ROLE_EDITOR"}) - public boolean isValidUsername(String username){ + + @Secured({ "ROLE_VIEWER", "ROLE_EDITOR" }) + public boolean isValidUsername(String username) { return userRoleRepository.isValidUsername(username); } - + @RolesAllowed("ROLE_VIEWER") - public String getUsername2(){ + public String getUsername2() { SecurityContext securityContext = SecurityContextHolder.getContext(); return securityContext.getAuthentication().getName(); } - - @RolesAllowed({"ROLE_VIEWER","ROLE_EDITOR"}) - public boolean isValidUsername2(String username){ + + @RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" }) + public boolean isValidUsername2(String username) { return userRoleRepository.isValidUsername(username); } - + @PreAuthorize("hasRole('ROLE_VIEWER')") - public String getUsernameInUpperCase(){ + public String getUsernameInUpperCase() { return getUsername().toUpperCase(); } - + @PreAuthorize("hasAuthority('SYS_ADMIN')") - public String getUsernameInLowerCase(){ + public String getUsernameLC() { return getUsername().toLowerCase(); } - + @PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')") - public boolean isValidUsername3(String username){ + public boolean isValidUsername3(String username) { return userRoleRepository.isValidUsername(username); } - + @PreAuthorize("#username == authentication.principal.username") - public String getMyRoles(String username){ + public String getMyRoles(String username) { SecurityContext securityContext = SecurityContextHolder.getContext(); return securityContext .getAuthentication() .getAuthorities() - .stream().map(auth -> auth.getAuthority()) - .collect(Collectors.joining(",")); + .stream() + .map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); } - + @PostAuthorize("returnObject.username == authentication.principal.nickName") - public CustomUser loadUserDetail(String username){ + public CustomUser loadUserDetail(String username) { return userRoleRepository.loadUserByUserName(username); } - + @PreFilter("filterObject != authentication.principal.username") - public String joinUsernames(List usernames){ + public String joinUsernames(List usernames) { return usernames.stream().collect(Collectors.joining(";")); } - - @PreFilter(value="filterObject != authentication.principal.username",filterTarget="usernames") - public String joinUsernamesAndRoles(List usernames,List roles){ - return usernames.stream().collect(Collectors.joining(";")) - +":"+roles.stream().collect(Collectors.joining(";")); + + @PreFilter(value = "filterObject != authentication.principal.username", filterTarget = "usernames") + public String joinUsernamesAndRoles(List usernames, List roles) { + return usernames.stream().collect(Collectors.joining(";")) + ":" + roles.stream().collect(Collectors.joining(";")); } - + @PostFilter("filterObject != authentication.principal.username") - public List getAllUsernamesExceptCurrent(){ + public List getAllUsernamesExceptCurrent() { return userRoleRepository.getAllUsernames(); } - + @IsViewer - public String getUsername4(){ + public String getUsername4() { SecurityContext securityContext = SecurityContextHolder.getContext(); return securityContext.getAuthentication().getName(); } - + @PreAuthorize("#username == authentication.principal.username") @PostAuthorize("returnObject.username == authentication.principal.nickName") - public CustomUser securedLoadUserDetail(String username){ + public CustomUser securedLoadUserDetail(String username) { return userRoleRepository.loadUserByUserName(username); } - + } diff --git a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestMethodSecurity.java b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestMethodSecurity.java index 4e4b665fb2..bc66c0b081 100644 --- a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestMethodSecurity.java +++ b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestMethodSecurity.java @@ -21,144 +21,144 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @ContextConfiguration -public class TestMethodSecurity{ - +public class TestMethodSecurity { + @Autowired UserRoleService userRoleService; - + @Configuration @ComponentScan("org.baeldung.methodsecurity.*") public static class SpringConfig { } - - @Test(expected=AuthenticationCredentialsNotFoundException.class) - public void givenNoSecurity_whenCallGetUsername_thenReturnException(){ + + @Test(expected = AuthenticationCredentialsNotFoundException.class) + public void givenNoSecurity_whenCallGetUsername_thenReturnException() { String userName = userRoleService.getUsername(); assertEquals("john", userName); } - + @Test - @WithMockUser(username="john",roles={"VIEWER"}) - public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){ + @WithMockUser(username = "john", roles = { "VIEWER" }) + public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() { String userName = userRoleService.getUsername(); assertEquals("john", userName); } - + @Test - @WithMockUser(username="john",roles={"EDITOR"}) - public void givenUsernameJohn_whenCallIsValidUsername_thenReturnTrue(){ + @WithMockUser(username = "john", roles = { "EDITOR" }) + public void givenUsernameJohn_whenCallIsValidUsername_thenReturnTrue() { boolean isValid = userRoleService.isValidUsername("john"); assertEquals(true, isValid); } - + @Test(expected = AccessDeniedException.class) @WithMockUser(username = "john", roles = { "ADMIN" }) public void givenRoleAdmin_whenCallGetUsername_thenReturnAccessDenied() { userRoleService.getUsername(); } - + @Test(expected = AccessDeniedException.class) - @WithMockUser(username = "john", roles = {"USER"}) + @WithMockUser(username = "john", roles = { "USER" }) public void givenRoleUser_whenCallGetUsername2_thenReturnAccessDenied() { userRoleService.getUsername2(); } - + @Test - @WithMockUser(username="john",roles={"VIEWER","EDITOR"}) - public void givenRoleViewer_whenCallGetUsername2_thenReturnUsername(){ + @WithMockUser(username = "john", roles = { "VIEWER", "EDITOR" }) + public void givenRoleViewer_whenCallGetUsername2_thenReturnUsername() { String userName = userRoleService.getUsername2(); assertEquals("john", userName); } - + @Test - @WithMockUser(username="john",roles={"VIEWER"}) - public void givenUsernameJerry_whenCallIsValidUsername2_thenReturnFalse(){ + @WithMockUser(username = "john", roles = { "VIEWER" }) + public void givenUsernameJerry_whenCallIsValidUsername2_thenReturnFalse() { boolean isValid = userRoleService.isValidUsername2("jerry"); assertEquals(false, isValid); } - + @Test - @WithMockUser(username="JOHN",authorities={"SYS_ADMIN"}) - public void givenAuthoritySysAdmin_whenCallGetUsernameInLowerCase_thenReturnUsername(){ - String username = userRoleService.getUsernameInLowerCase(); + @WithMockUser(username = "JOHN", authorities = { "SYS_ADMIN" }) + public void givenAuthoritySysAdmin_whenCallGetUsernameInLowerCase_thenReturnUsername() { + String username = userRoleService.getUsernameLC(); assertEquals("john", username); } - + @Test - @WithMockUser(username="john",roles={"ADMIN","USER","VIEWER"}) - public void givenUserJohn_whenCallGetMyRolesWithJohn_thenReturnRoles(){ + @WithMockUser(username = "john", roles = { "ADMIN", "USER", "VIEWER" }) + public void givenUserJohn_whenCallGetMyRolesWithJohn_thenReturnRoles() { String roles = userRoleService.getMyRoles("john"); assertEquals("ROLE_ADMIN,ROLE_USER,ROLE_VIEWER", roles); } - - @Test(expected=AccessDeniedException.class) - @WithMockUser(username="john",roles={"ADMIN","USER","VIEWER"}) - public void givenUserJane_whenCallGetMyRolesWithJane_thenAccessDenied(){ + + @Test(expected = AccessDeniedException.class) + @WithMockUser(username = "john", roles = { "ADMIN", "USER", "VIEWER" }) + public void givenUserJane_whenCallGetMyRolesWithJane_thenAccessDenied() { userRoleService.getMyRoles("jane"); } - - @Test(expected=AccessDeniedException.class) + + @Test(expected = AccessDeniedException.class) @WithAnonymousUser - public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied(){ + public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied() { userRoleService.getUsername(); } - + @Test @WithMockJohnViewer - public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername(){ + public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername() { String userName = userRoleService.getUsername(); assertEquals("john", userName); } - + @Test - @WithMockUser(username="jane") - public void givenListContainCurrentUsername_whenJoinUsernames_thenReturnUsernames(){ + @WithMockUser(username = "jane") + public void givenListContainCurrentUsername_whenJoinUsernames_thenReturnUsernames() { List usernames = new ArrayList<>(); usernames.add("jane"); usernames.add("john"); usernames.add("jack"); String containCurrentUser = userRoleService.joinUsernames(usernames); assertEquals("john;jack", containCurrentUser); - + } - + @Test - @WithMockUser(username="john") - public void givenListNotContainCurrentUsername_whenCallContainCurrentUser_thenReturnAccessDenied(){ + @WithMockUser(username = "john") + public void givenListNotContainCurrentUsername_whenCallContainCurrentUser_thenReturnAccessDenied() { List usernames = new ArrayList<>(); usernames.add("jane"); usernames.add("john"); usernames.add("jack"); - + List roles = new ArrayList<>(); roles.add("ROLE_ADMIN"); roles.add("ROLE_TEST"); - - String containCurrentUser = userRoleService.joinUsernamesAndRoles(usernames,roles); + + String containCurrentUser = userRoleService.joinUsernamesAndRoles(usernames, roles); assertEquals("jane;jack:ROLE_ADMIN;ROLE_TEST", containCurrentUser); } - + @Test - @WithMockUser(username="john") - public void givenUserJohn_whenCallGetAllUsernamesExceptCurrent_thenReturnOtherusernames(){ + @WithMockUser(username = "john") + public void givenUserJohn_whenCallGetAllUsernamesExceptCurrent_thenReturnOtherusernames() { List others = userRoleService.getAllUsernamesExceptCurrent(); assertEquals(2, others.size()); assertTrue(others.contains("jane")); assertTrue(others.contains("jack")); } - + @Test - @WithMockUser(username="john",roles={"VIEWER"}) - public void givenRoleViewer_whenCallGetUsername4_thenReturnUsername(){ + @WithMockUser(username = "john", roles = { "VIEWER" }) + public void givenRoleViewer_whenCallGetUsername4_thenReturnUsername() { String userName = userRoleService.getUsername4(); assertEquals("john", userName); } - - @Test(expected=AccessDeniedException.class) - @WithMockUser(username="john") - public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied(){ + + @Test(expected = AccessDeniedException.class) + @WithMockUser(username = "john") + public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied() { userRoleService.getUsername4(); } - + } \ No newline at end of file diff --git a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithMockUserAtClassLevel.java b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithMockUserAtClassLevel.java index 319aee63a6..4df1af8ca9 100644 --- a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithMockUserAtClassLevel.java +++ b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithMockUserAtClassLevel.java @@ -14,18 +14,18 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @ContextConfiguration -@WithMockUser(username="john",roles={"VIEWER"}) +@WithMockUser(username = "john", roles = { "VIEWER" }) public class TestWithMockUserAtClassLevel { - + @Test - public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){ + public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() { String currentUserName = userService.getUsername(); - assertEquals("john",currentUserName); + assertEquals("john", currentUserName); } - + @Autowired UserRoleService userService; - + @Configuration @ComponentScan("org.baeldung.methodsecurity.*") public static class SpringConfig { diff --git a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithUserDetails.java b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithUserDetails.java index 6c1d2ab62c..3ef5996554 100644 --- a/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithUserDetails.java +++ b/spring-security-core/src/test/java/org/baeldung/methodsecurity/TestWithUserDetails.java @@ -17,40 +17,40 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @ContextConfiguration public class TestWithUserDetails { - + @Autowired UserRoleService userService; - + @Configuration @ComponentScan("org.baeldung.methodsecurity.*") public static class SpringConfig { } - + @Test - @WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") - public void whenJohn_callLoadUserDetail_thenOK(){ + @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService") + public void whenJohn_callLoadUserDetail_thenOK() { CustomUser user = userService.loadUserDetail("jane"); - assertEquals("jane",user.getNickName()); + assertEquals("jane", user.getNickName()); } - + @Test - @WithUserDetails(value="jane",userDetailsServiceBeanName="userDetailService") - public void givenJane_callSecuredLoadUserDetailWithJane_thenOK(){ + @WithUserDetails(value = "jane", userDetailsServiceBeanName = "userDetailService") + public void givenJane_callSecuredLoadUserDetailWithJane_thenOK() { CustomUser user = userService.securedLoadUserDetail("jane"); - assertEquals("jane",user.getNickName()); - assertEquals("jane",user.getUsername()); + assertEquals("jane", user.getNickName()); + assertEquals("jane", user.getUsername()); } - - @Test(expected=AccessDeniedException.class) - @WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") - public void givenJohn_callSecuredLoadUserDetailWithJane_thenAccessDenied(){ + + @Test(expected = AccessDeniedException.class) + @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService") + public void givenJohn_callSecuredLoadUserDetailWithJane_thenAccessDenied() { userService.securedLoadUserDetail("jane"); } - - @Test(expected=AccessDeniedException.class) - @WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") - public void givenJohn_callSecuredLoadUserDetailWithJohn_thenAccessDenied(){ + + @Test(expected = AccessDeniedException.class) + @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService") + public void givenJohn_callSecuredLoadUserDetailWithJohn_thenAccessDenied() { userService.securedLoadUserDetail("john"); } }