[BAEL-1411] Format code

This commit is contained in:
linhvovn 2017-12-31 23:32:33 +08:00
parent 1b7e6957bb
commit 2bac6f88e7
7 changed files with 145 additions and 147 deletions

View File

@ -10,6 +10,5 @@ import org.springframework.security.access.prepost.PreAuthorize;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole('VIEWER')") @PreAuthorize("hasRole('VIEWER')")
public @interface IsViewer public @interface IsViewer {
{
} }

View File

@ -6,7 +6,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class CustomUser extends User{ public class CustomUser extends User {
private String nickName; private String nickName;
@ -14,7 +14,7 @@ public class CustomUser extends User{
super(username, password, authorities); super(username, password, authorities);
} }
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities,String nickName) { public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities, String nickName) {
super(username, password, authorities); super(username, password, authorities);
this.nickName = nickName; this.nickName = nickName;
} }

View File

@ -14,39 +14,39 @@ import org.springframework.stereotype.Service;
@Service @Service
public class UserRoleRepository { public class UserRoleRepository {
static Map<String,CustomUser> DB_BASED_USER_MAPPING; static Map<String, CustomUser> DB_BASED_USER_MAPPING;
static{ static {
DB_BASED_USER_MAPPING = new LinkedHashMap<>(); 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("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("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("jack", new CustomUser("jack", "1234", getGrantedAuthorities("ROLE_USER", "ROLE_REVIEWER"), "jane"));
} }
private static List<GrantedAuthority> getGrantedAuthorities(String...roles){ private static List<GrantedAuthority> getGrantedAuthorities(String... roles) {
ArrayList<GrantedAuthority> authorities = new ArrayList<>(); ArrayList<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles){ for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role)); authorities.add(new SimpleGrantedAuthority(role));
} }
return authorities; return authorities;
} }
public CustomUser loadUserByUserName(String username){ public CustomUser loadUserByUserName(String username) {
if (DB_BASED_USER_MAPPING.containsKey(username)){ if (DB_BASED_USER_MAPPING.containsKey(username)) {
return DB_BASED_USER_MAPPING.get(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); return DB_BASED_USER_MAPPING.containsKey(username);
} }
public boolean isValidRole(String roleName){ public boolean isValidRole(String roleName) {
return roleName.startsWith("ROLE_"); return roleName.startsWith("ROLE_");
} }
public List<String> getAllUsernames(){ public List<String> getAllUsernames() {
List<String> usernames = new ArrayList<>(); List<String> usernames = new ArrayList<>();
usernames.add("jane"); usernames.add("jane");
usernames.add("john"); usernames.add("john");

View File

@ -25,82 +25,81 @@ public class UserRoleService {
UserRoleRepository userRoleRepository; UserRoleRepository userRoleRepository;
@Secured("ROLE_VIEWER") @Secured("ROLE_VIEWER")
public String getUsername(){ public String getUsername() {
SecurityContext securityContext = SecurityContextHolder.getContext(); SecurityContext securityContext = SecurityContextHolder.getContext();
return securityContext.getAuthentication().getName(); return securityContext.getAuthentication().getName();
} }
@Secured({"ROLE_VIEWER","ROLE_EDITOR"}) @Secured({ "ROLE_VIEWER", "ROLE_EDITOR" })
public boolean isValidUsername(String username){ public boolean isValidUsername(String username) {
return userRoleRepository.isValidUsername(username); return userRoleRepository.isValidUsername(username);
} }
@RolesAllowed("ROLE_VIEWER") @RolesAllowed("ROLE_VIEWER")
public String getUsername2(){ public String getUsername2() {
SecurityContext securityContext = SecurityContextHolder.getContext(); SecurityContext securityContext = SecurityContextHolder.getContext();
return securityContext.getAuthentication().getName(); return securityContext.getAuthentication().getName();
} }
@RolesAllowed({"ROLE_VIEWER","ROLE_EDITOR"}) @RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" })
public boolean isValidUsername2(String username){ public boolean isValidUsername2(String username) {
return userRoleRepository.isValidUsername(username); return userRoleRepository.isValidUsername(username);
} }
@PreAuthorize("hasRole('ROLE_VIEWER')") @PreAuthorize("hasRole('ROLE_VIEWER')")
public String getUsernameInUpperCase(){ public String getUsernameInUpperCase() {
return getUsername().toUpperCase(); return getUsername().toUpperCase();
} }
@PreAuthorize("hasAuthority('SYS_ADMIN')") @PreAuthorize("hasAuthority('SYS_ADMIN')")
public String getUsernameInLowerCase(){ public String getUsernameLC() {
return getUsername().toLowerCase(); return getUsername().toLowerCase();
} }
@PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')") @PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')")
public boolean isValidUsername3(String username){ public boolean isValidUsername3(String username) {
return userRoleRepository.isValidUsername(username); return userRoleRepository.isValidUsername(username);
} }
@PreAuthorize("#username == authentication.principal.username") @PreAuthorize("#username == authentication.principal.username")
public String getMyRoles(String username){ public String getMyRoles(String username) {
SecurityContext securityContext = SecurityContextHolder.getContext(); SecurityContext securityContext = SecurityContextHolder.getContext();
return securityContext return securityContext
.getAuthentication() .getAuthentication()
.getAuthorities() .getAuthorities()
.stream().map(auth -> auth.getAuthority()) .stream()
.collect(Collectors.joining(",")); .map(auth -> auth.getAuthority()).collect(Collectors.joining(","));
} }
@PostAuthorize("returnObject.username == authentication.principal.nickName") @PostAuthorize("returnObject.username == authentication.principal.nickName")
public CustomUser loadUserDetail(String username){ public CustomUser loadUserDetail(String username) {
return userRoleRepository.loadUserByUserName(username); return userRoleRepository.loadUserByUserName(username);
} }
@PreFilter("filterObject != authentication.principal.username") @PreFilter("filterObject != authentication.principal.username")
public String joinUsernames(List<String> usernames){ public String joinUsernames(List<String> usernames) {
return usernames.stream().collect(Collectors.joining(";")); return usernames.stream().collect(Collectors.joining(";"));
} }
@PreFilter(value="filterObject != authentication.principal.username",filterTarget="usernames") @PreFilter(value = "filterObject != authentication.principal.username", filterTarget = "usernames")
public String joinUsernamesAndRoles(List<String> usernames,List<String> roles){ public String joinUsernamesAndRoles(List<String> usernames, List<String> roles) {
return usernames.stream().collect(Collectors.joining(";")) return usernames.stream().collect(Collectors.joining(";")) + ":" + roles.stream().collect(Collectors.joining(";"));
+":"+roles.stream().collect(Collectors.joining(";"));
} }
@PostFilter("filterObject != authentication.principal.username") @PostFilter("filterObject != authentication.principal.username")
public List<String> getAllUsernamesExceptCurrent(){ public List<String> getAllUsernamesExceptCurrent() {
return userRoleRepository.getAllUsernames(); return userRoleRepository.getAllUsernames();
} }
@IsViewer @IsViewer
public String getUsername4(){ public String getUsername4() {
SecurityContext securityContext = SecurityContextHolder.getContext(); SecurityContext securityContext = SecurityContextHolder.getContext();
return securityContext.getAuthentication().getName(); return securityContext.getAuthentication().getName();
} }
@PreAuthorize("#username == authentication.principal.username") @PreAuthorize("#username == authentication.principal.username")
@PostAuthorize("returnObject.username == authentication.principal.nickName") @PostAuthorize("returnObject.username == authentication.principal.nickName")
public CustomUser securedLoadUserDetail(String username){ public CustomUser securedLoadUserDetail(String username) {
return userRoleRepository.loadUserByUserName(username); return userRoleRepository.loadUserByUserName(username);
} }

View File

@ -21,7 +21,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration @ContextConfiguration
public class TestMethodSecurity{ public class TestMethodSecurity {
@Autowired @Autowired
UserRoleService userRoleService; UserRoleService userRoleService;
@ -32,22 +32,22 @@ public class TestMethodSecurity{
} }
@Test(expected=AuthenticationCredentialsNotFoundException.class) @Test(expected = AuthenticationCredentialsNotFoundException.class)
public void givenNoSecurity_whenCallGetUsername_thenReturnException(){ public void givenNoSecurity_whenCallGetUsername_thenReturnException() {
String userName = userRoleService.getUsername(); String userName = userRoleService.getUsername();
assertEquals("john", userName); assertEquals("john", userName);
} }
@Test @Test
@WithMockUser(username="john",roles={"VIEWER"}) @WithMockUser(username = "john", roles = { "VIEWER" })
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){ public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() {
String userName = userRoleService.getUsername(); String userName = userRoleService.getUsername();
assertEquals("john", userName); assertEquals("john", userName);
} }
@Test @Test
@WithMockUser(username="john",roles={"EDITOR"}) @WithMockUser(username = "john", roles = { "EDITOR" })
public void givenUsernameJohn_whenCallIsValidUsername_thenReturnTrue(){ public void givenUsernameJohn_whenCallIsValidUsername_thenReturnTrue() {
boolean isValid = userRoleService.isValidUsername("john"); boolean isValid = userRoleService.isValidUsername("john");
assertEquals(true, isValid); assertEquals(true, isValid);
} }
@ -59,61 +59,61 @@ public class TestMethodSecurity{
} }
@Test(expected = AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithMockUser(username = "john", roles = {"USER"}) @WithMockUser(username = "john", roles = { "USER" })
public void givenRoleUser_whenCallGetUsername2_thenReturnAccessDenied() { public void givenRoleUser_whenCallGetUsername2_thenReturnAccessDenied() {
userRoleService.getUsername2(); userRoleService.getUsername2();
} }
@Test @Test
@WithMockUser(username="john",roles={"VIEWER","EDITOR"}) @WithMockUser(username = "john", roles = { "VIEWER", "EDITOR" })
public void givenRoleViewer_whenCallGetUsername2_thenReturnUsername(){ public void givenRoleViewer_whenCallGetUsername2_thenReturnUsername() {
String userName = userRoleService.getUsername2(); String userName = userRoleService.getUsername2();
assertEquals("john", userName); assertEquals("john", userName);
} }
@Test @Test
@WithMockUser(username="john",roles={"VIEWER"}) @WithMockUser(username = "john", roles = { "VIEWER" })
public void givenUsernameJerry_whenCallIsValidUsername2_thenReturnFalse(){ public void givenUsernameJerry_whenCallIsValidUsername2_thenReturnFalse() {
boolean isValid = userRoleService.isValidUsername2("jerry"); boolean isValid = userRoleService.isValidUsername2("jerry");
assertEquals(false, isValid); assertEquals(false, isValid);
} }
@Test @Test
@WithMockUser(username="JOHN",authorities={"SYS_ADMIN"}) @WithMockUser(username = "JOHN", authorities = { "SYS_ADMIN" })
public void givenAuthoritySysAdmin_whenCallGetUsernameInLowerCase_thenReturnUsername(){ public void givenAuthoritySysAdmin_whenCallGetUsernameInLowerCase_thenReturnUsername() {
String username = userRoleService.getUsernameInLowerCase(); String username = userRoleService.getUsernameLC();
assertEquals("john", username); assertEquals("john", username);
} }
@Test @Test
@WithMockUser(username="john",roles={"ADMIN","USER","VIEWER"}) @WithMockUser(username = "john", roles = { "ADMIN", "USER", "VIEWER" })
public void givenUserJohn_whenCallGetMyRolesWithJohn_thenReturnRoles(){ public void givenUserJohn_whenCallGetMyRolesWithJohn_thenReturnRoles() {
String roles = userRoleService.getMyRoles("john"); String roles = userRoleService.getMyRoles("john");
assertEquals("ROLE_ADMIN,ROLE_USER,ROLE_VIEWER", roles); assertEquals("ROLE_ADMIN,ROLE_USER,ROLE_VIEWER", roles);
} }
@Test(expected=AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithMockUser(username="john",roles={"ADMIN","USER","VIEWER"}) @WithMockUser(username = "john", roles = { "ADMIN", "USER", "VIEWER" })
public void givenUserJane_whenCallGetMyRolesWithJane_thenAccessDenied(){ public void givenUserJane_whenCallGetMyRolesWithJane_thenAccessDenied() {
userRoleService.getMyRoles("jane"); userRoleService.getMyRoles("jane");
} }
@Test(expected=AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithAnonymousUser @WithAnonymousUser
public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied(){ public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied() {
userRoleService.getUsername(); userRoleService.getUsername();
} }
@Test @Test
@WithMockJohnViewer @WithMockJohnViewer
public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername(){ public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername() {
String userName = userRoleService.getUsername(); String userName = userRoleService.getUsername();
assertEquals("john", userName); assertEquals("john", userName);
} }
@Test @Test
@WithMockUser(username="jane") @WithMockUser(username = "jane")
public void givenListContainCurrentUsername_whenJoinUsernames_thenReturnUsernames(){ public void givenListContainCurrentUsername_whenJoinUsernames_thenReturnUsernames() {
List<String> usernames = new ArrayList<>(); List<String> usernames = new ArrayList<>();
usernames.add("jane"); usernames.add("jane");
usernames.add("john"); usernames.add("john");
@ -124,8 +124,8 @@ public class TestMethodSecurity{
} }
@Test @Test
@WithMockUser(username="john") @WithMockUser(username = "john")
public void givenListNotContainCurrentUsername_whenCallContainCurrentUser_thenReturnAccessDenied(){ public void givenListNotContainCurrentUsername_whenCallContainCurrentUser_thenReturnAccessDenied() {
List<String> usernames = new ArrayList<>(); List<String> usernames = new ArrayList<>();
usernames.add("jane"); usernames.add("jane");
usernames.add("john"); usernames.add("john");
@ -135,13 +135,13 @@ public class TestMethodSecurity{
roles.add("ROLE_ADMIN"); roles.add("ROLE_ADMIN");
roles.add("ROLE_TEST"); roles.add("ROLE_TEST");
String containCurrentUser = userRoleService.joinUsernamesAndRoles(usernames,roles); String containCurrentUser = userRoleService.joinUsernamesAndRoles(usernames, roles);
assertEquals("jane;jack:ROLE_ADMIN;ROLE_TEST", containCurrentUser); assertEquals("jane;jack:ROLE_ADMIN;ROLE_TEST", containCurrentUser);
} }
@Test @Test
@WithMockUser(username="john") @WithMockUser(username = "john")
public void givenUserJohn_whenCallGetAllUsernamesExceptCurrent_thenReturnOtherusernames(){ public void givenUserJohn_whenCallGetAllUsernamesExceptCurrent_thenReturnOtherusernames() {
List<String> others = userRoleService.getAllUsernamesExceptCurrent(); List<String> others = userRoleService.getAllUsernamesExceptCurrent();
assertEquals(2, others.size()); assertEquals(2, others.size());
assertTrue(others.contains("jane")); assertTrue(others.contains("jane"));
@ -149,15 +149,15 @@ public class TestMethodSecurity{
} }
@Test @Test
@WithMockUser(username="john",roles={"VIEWER"}) @WithMockUser(username = "john", roles = { "VIEWER" })
public void givenRoleViewer_whenCallGetUsername4_thenReturnUsername(){ public void givenRoleViewer_whenCallGetUsername4_thenReturnUsername() {
String userName = userRoleService.getUsername4(); String userName = userRoleService.getUsername4();
assertEquals("john", userName); assertEquals("john", userName);
} }
@Test(expected=AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithMockUser(username="john") @WithMockUser(username = "john")
public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied(){ public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied() {
userRoleService.getUsername4(); userRoleService.getUsername4();
} }

View File

@ -14,13 +14,13 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration @ContextConfiguration
@WithMockUser(username="john",roles={"VIEWER"}) @WithMockUser(username = "john", roles = { "VIEWER" })
public class TestWithMockUserAtClassLevel { public class TestWithMockUserAtClassLevel {
@Test @Test
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){ public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() {
String currentUserName = userService.getUsername(); String currentUserName = userService.getUsername();
assertEquals("john",currentUserName); assertEquals("john", currentUserName);
} }
@Autowired @Autowired

View File

@ -28,29 +28,29 @@ public class TestWithUserDetails {
} }
@Test @Test
@WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService")
public void whenJohn_callLoadUserDetail_thenOK(){ public void whenJohn_callLoadUserDetail_thenOK() {
CustomUser user = userService.loadUserDetail("jane"); CustomUser user = userService.loadUserDetail("jane");
assertEquals("jane",user.getNickName()); assertEquals("jane", user.getNickName());
} }
@Test @Test
@WithUserDetails(value="jane",userDetailsServiceBeanName="userDetailService") @WithUserDetails(value = "jane", userDetailsServiceBeanName = "userDetailService")
public void givenJane_callSecuredLoadUserDetailWithJane_thenOK(){ public void givenJane_callSecuredLoadUserDetailWithJane_thenOK() {
CustomUser user = userService.securedLoadUserDetail("jane"); CustomUser user = userService.securedLoadUserDetail("jane");
assertEquals("jane",user.getNickName()); assertEquals("jane", user.getNickName());
assertEquals("jane",user.getUsername()); assertEquals("jane", user.getUsername());
} }
@Test(expected=AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService")
public void givenJohn_callSecuredLoadUserDetailWithJane_thenAccessDenied(){ public void givenJohn_callSecuredLoadUserDetailWithJane_thenAccessDenied() {
userService.securedLoadUserDetail("jane"); userService.securedLoadUserDetail("jane");
} }
@Test(expected=AccessDeniedException.class) @Test(expected = AccessDeniedException.class)
@WithUserDetails(value="john",userDetailsServiceBeanName="userDetailService") @WithUserDetails(value = "john", userDetailsServiceBeanName = "userDetailService")
public void givenJohn_callSecuredLoadUserDetailWithJohn_thenAccessDenied(){ public void givenJohn_callSecuredLoadUserDetailWithJohn_thenAccessDenied() {
userService.securedLoadUserDetail("john"); userService.securedLoadUserDetail("john");
} }
} }