Skip to content

Commit 7781773

Browse files
committed
[tlinh2110@gmail.com] Add resource for Testing Method Security
1 parent 5014f50 commit 7781773

15 files changed

Lines changed: 447 additions & 0 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.testmethodsecurity.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
5+
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
6+
7+
@Configuration
8+
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
9+
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
10+
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.testmethodsecurity.entity;
2+
3+
import java.util.Collection;
4+
5+
import org.springframework.security.core.GrantedAuthority;
6+
import org.springframework.security.core.userdetails.User;
7+
8+
@SuppressWarnings("serial")
9+
public class CustomUser extends User{
10+
11+
private String nickName;
12+
private int age;
13+
14+
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
15+
super(username, password, authorities);
16+
}
17+
18+
public CustomUser(String username, String password, Collection<? extends GrantedAuthority> authorities,String nickName) {
19+
super(username, password, authorities);
20+
this.nickName = nickName;
21+
}
22+
23+
public String getNickName() {
24+
return nickName;
25+
}
26+
27+
public void setNickName(String nickName) {
28+
this.nickName = nickName;
29+
}
30+
31+
public int getAge() {
32+
return age;
33+
}
34+
35+
public void setAge(int age) {
36+
this.age = age;
37+
}
38+
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.testmethodsecurity.repository;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import org.baeldung.testmethodsecurity.entity.CustomUser;
9+
import org.springframework.security.core.GrantedAuthority;
10+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
11+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
12+
import org.springframework.stereotype.Service;
13+
14+
@Service
15+
public class UserRoleRepository {
16+
17+
static Map<String,CustomUser> DB_BASED_USER_MAPPING;
18+
19+
static{
20+
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"));
24+
}
25+
26+
private static List<GrantedAuthority> getGrantedAuthorities(String...roles){
27+
ArrayList<GrantedAuthority> authorities = new ArrayList<>();
28+
for (String role : roles){
29+
authorities.add(new SimpleGrantedAuthority(role));
30+
}
31+
return authorities;
32+
}
33+
34+
public CustomUser loadUserByUserName(String username){
35+
if (DB_BASED_USER_MAPPING.containsKey(username)){
36+
return DB_BASED_USER_MAPPING.get(username);
37+
}
38+
throw new UsernameNotFoundException("User "+username+" cannot be found");
39+
}
40+
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.testmethodsecurity.service;
2+
3+
import org.baeldung.testmethodsecurity.repository.UserRoleRepository;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
import org.springframework.security.core.userdetails.UserDetailsService;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service("userDetailService")
10+
public class CustomUserDetailsService implements UserDetailsService {
11+
12+
@Autowired
13+
UserRoleRepository userRoleRepo;
14+
15+
@Override
16+
public UserDetails loadUserByUsername(String username) {
17+
return userRoleRepo.loadUserByUserName(username);
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.testmethodsecurity.service;
2+
3+
import org.springframework.security.access.annotation.Secured;
4+
import org.springframework.security.access.prepost.PreAuthorize;
5+
import org.springframework.stereotype.Service;
6+
7+
@Service
8+
public class SystemPropImpl implements SystemPropInterface{
9+
10+
@PreAuthorize("permitAll")
11+
@Override
12+
public String getSystemName() {
13+
return "Method Security";
14+
}
15+
16+
@Override
17+
public String sayHello(){
18+
return sayHi();
19+
}
20+
21+
@Secured("ROLE_USER")
22+
public String sayHi(){
23+
return "Hi";
24+
}
25+
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.testmethodsecurity.service;
2+
3+
public interface SystemPropInterface {
4+
5+
String getSystemName();
6+
7+
String sayHello();
8+
9+
String sayHi();
10+
11+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.testmethodsecurity.service;
2+
3+
import org.baeldung.testmethodsecurity.entity.CustomUser;
4+
import org.baeldung.testmethodsecurity.repository.UserRoleRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.access.prepost.PostAuthorize;
7+
import org.springframework.security.access.prepost.PreAuthorize;
8+
import org.springframework.security.core.context.SecurityContext;
9+
import org.springframework.security.core.context.SecurityContextHolder;
10+
import org.springframework.stereotype.Service;
11+
12+
@Service
13+
public class UserRoleService {
14+
15+
@Autowired
16+
UserRoleRepository userRoleRepository;
17+
18+
@PreAuthorize("hasRole('ROLE_VIEWER') or hasAuthority('SYS_ADMIN')")
19+
public String getUserName(){
20+
SecurityContext securityContext = SecurityContextHolder.getContext();
21+
return securityContext.getAuthentication().getName();
22+
}
23+
24+
@PostAuthorize("returnObject.username == authentication.principal.nickName")
25+
public CustomUser loadUserDetail(String username){
26+
return userRoleRepository.loadUserByUserName(username);
27+
}
28+
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.testmethodsecurity;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.baeldung.testmethodsecurity.service.UserRoleService;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.security.access.AccessDeniedException;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@ContextConfiguration
17+
public class TestCustomSecurityContext {
18+
19+
@Autowired
20+
UserRoleService userRoleService;
21+
22+
@Configuration
23+
@ComponentScan("org.baeldung.testmethodsecurity.*")
24+
public static class SpringConfig {
25+
26+
}
27+
28+
@Test
29+
@WithMockSysUser(systemUserName="jane")
30+
public void whenJane_callGetUserName_thenOK(){
31+
String userName = userRoleService.getUserName();
32+
assertEquals("jane",userName);
33+
}
34+
35+
@Test(expected=AccessDeniedException.class)
36+
@WithMockSysUser(systemUserName="john")
37+
public void whenJohn_callGetUserName_thenFail(){
38+
userRoleService.getUserName();
39+
}
40+
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.baeldung.testmethodsecurity;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.baeldung.testmethodsecurity.service.UserRoleService;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.security.access.AccessDeniedException;
12+
import org.springframework.security.test.context.support.WithAnonymousUser;
13+
import org.springframework.security.test.context.support.WithMockUser;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration
19+
public class TestMethodSecurity{
20+
21+
@Autowired
22+
UserRoleService userRoleService;
23+
24+
@Configuration
25+
@ComponentScan("org.baeldung.testmethodsecurity.*")
26+
public static class SpringConfig {
27+
28+
}
29+
30+
@Test
31+
@WithMockUser(username="john",roles={"VIEWER"})
32+
public void whenRoleViewer_callGetUserName_thenOK(){
33+
String userName = userRoleService.getUserName();
34+
assertEquals("john", userName);
35+
}
36+
37+
@Test
38+
@WithMockUser(username="john",authorities={"SYS_ADMIN"})
39+
public void whenSysAdmin_callGetUserName_thenOK(){
40+
String userName = userRoleService.getUserName();
41+
assertEquals("john", userName);
42+
}
43+
44+
@Test(expected=AccessDeniedException.class)
45+
@WithAnonymousUser
46+
public void whenAnomynous_callGetUserName_thenFail(){
47+
userRoleService.getUserName();
48+
}
49+
50+
@Test
51+
@WithMockJohnViewer
52+
public void whenJohnViewer_callGetUserName_thenOK(){
53+
String userName = userRoleService.getUserName();
54+
assertEquals("john", userName);
55+
}
56+
57+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.baeldung.testmethodsecurity;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.lang.reflect.Proxy;
8+
9+
import org.baeldung.testmethodsecurity.service.SystemPropImpl;
10+
import org.baeldung.testmethodsecurity.service.SystemPropInterface;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.context.annotation.ComponentScan;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
17+
import org.springframework.security.test.context.support.WithMockUser;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration
23+
public class TestSystemProp{
24+
25+
@Autowired
26+
SystemPropInterface systemProp;
27+
28+
@Test
29+
@WithMockUser(username="test")
30+
public void checkSystemPropInstance(){
31+
assertFalse(systemProp instanceof SystemPropImpl);
32+
assertTrue(systemProp instanceof SystemPropInterface);
33+
assertTrue(systemProp instanceof Proxy);
34+
35+
assertEquals("Method Security", systemProp.getSystemName());
36+
}
37+
38+
@Test
39+
public void whenNotAuthentication_callSayHello_thenOK(){
40+
String hello = systemProp.sayHello();
41+
assertEquals("Hi", hello);
42+
}
43+
44+
@Test(expected=AuthenticationCredentialsNotFoundException.class)
45+
public void whenNotAuthentication_callSayHi_thenFailed(){
46+
systemProp.sayHi();
47+
}
48+
49+
@Configuration
50+
@ComponentScan("org.baeldung.testmethodsecurity.*")
51+
public static class SpringConfig {
52+
53+
}
54+
}

0 commit comments

Comments
 (0)