[BAEL1411-tlinh2110] Add Class Level Security Example
This commit is contained in:
parent
0d84b1f0c1
commit
1b7e6957bb
@ -0,0 +1,18 @@
|
||||
package org.baeldung.methodsecurity.service;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
public class SystemService {
|
||||
|
||||
public String getSystemYear(){
|
||||
return "2017";
|
||||
}
|
||||
|
||||
public String getSystemDate(){
|
||||
return "31-12-2017";
|
||||
}
|
||||
|
||||
}
|
@ -103,5 +103,5 @@ public class UserRoleService {
|
||||
public CustomUser securedLoadUserDetail(String username){
|
||||
return userRoleRepository.loadUserByUserName(username);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package org.baeldung.methodsecurity;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.baeldung.methodsecurity.service.SystemService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestClassLevelSecurity {
|
||||
|
||||
@Autowired
|
||||
SystemService systemService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.methodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username="john",roles={"ADMIN"})
|
||||
public void givenRoleAdmin_whenCallGetSystemYear_return2017(){
|
||||
String systemYear = systemService.getSystemYear();
|
||||
assertEquals("2017",systemYear);
|
||||
}
|
||||
|
||||
@Test(expected=AccessDeniedException.class)
|
||||
@WithMockUser(username="john",roles={"VIEWER"})
|
||||
public void givenRoleViewer_whenCallGetSystemYear_returnAccessDenied(){
|
||||
String systemYear = systemService.getSystemYear();
|
||||
assertEquals("2017",systemYear);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username="john",roles={"ADMIN"})
|
||||
public void givenRoleAdmin_whenCallGetSystemDate_returnDate(){
|
||||
String systemYear = systemService.getSystemDate();
|
||||
assertEquals("31-12-2017",systemYear);
|
||||
}
|
||||
}
|
@ -160,4 +160,5 @@ public class TestMethodSecurity{
|
||||
public void givenDefaultRole_whenCallGetUsername4_thenAccessDenied(){
|
||||
userRoleService.getUsername4();
|
||||
}
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.test.context.support.WithUserDetails;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
@ -35,8 +36,21 @@ public class TestWithUserDetails {
|
||||
|
||||
@Test
|
||||
@WithUserDetails(value="jane",userDetailsServiceBeanName="userDetailService")
|
||||
public void whenJohn_callSecuredLoadUserDetail_thenOK(){
|
||||
CustomUser user = userService.securedLoadUserDetail("john");
|
||||
public void givenJane_callSecuredLoadUserDetailWithJane_thenOK(){
|
||||
CustomUser user = userService.securedLoadUserDetail("jane");
|
||||
assertEquals("jane",user.getNickName());
|
||||
assertEquals("jane",user.getUsername());
|
||||
}
|
||||
|
||||
@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(){
|
||||
userService.securedLoadUserDetail("john");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user