Tôi đã cố gắng tìm ra cách kiểm tra đơn vị nếu các URL của các bộ điều khiển của tôi được bảo mật đúng cách. Chỉ trong trường hợp ai đó thay đổi mọi thứ xung quanh và vô tình xóa cài đặt bảo mật.Kiểm tra và bảo mật mùa xuân: Cách xác thực giả?
phương pháp điều khiển của tôi trông như thế này:
@RequestMapping("/api/v1/resource/test")
@Secured("ROLE_USER")
public @ResonseBody String test() {
return "test";
}
tôi thiết lập một WebTestEnvironment như vậy:
import javax.annotation.Resource;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
"file:src/main/webapp/WEB-INF/spring/security.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/servlet-context.xml" })
public class WebappTestEnvironment2 {
@Resource
private FilterChainProxy springSecurityFilterChain;
@Autowired
@Qualifier("databaseUserService")
protected UserDetailsService userDetailsService;
@Autowired
private WebApplicationContext wac;
@Autowired
protected DataSource dataSource;
protected MockMvc mockMvc;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected UsernamePasswordAuthenticationToken getPrincipal(String username) {
UserDetails user = this.userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(
user,
user.getPassword(),
user.getAuthorities());
return authentication;
}
@Before
public void setupMockMvc() throws NamingException {
// setup mock MVC
this.mockMvc = MockMvcBuilders
.webAppContextSetup(this.wac)
.addFilters(this.springSecurityFilterChain)
.build();
}
}
Trong thử nghiệm thực tế của tôi, tôi đã cố gắng để làm một cái gì đó như thế này:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import eu.ubicon.webapp.test.WebappTestEnvironment;
public class CopyOfClaimTest extends WebappTestEnvironment {
@Test
public void signedIn() throws Exception {
UsernamePasswordAuthenticationToken principal =
this.getPrincipal("test1");
SecurityContextHolder.getContext().setAuthentication(principal);
super.mockMvc
.perform(
get("/api/v1/resource/test")
// .principal(principal)
.session(session))
.andExpect(status().isOk());
}
}
Tôi đã chọn thẻ này tại đây:
- http://java.dzone.com/articles/spring-test-mvc-junit-testing đây:
- http://techdive.in/solutions/how-mock-securitycontextholder-perfrom-junit-tests-spring-controller hoặc ở đây:
- How to JUnit tests a @PreAuthorize annotation and its spring EL specified by a spring MVC Controller?
Tuy nhiên, nếu nhìn kỹ này chỉ giúp khi không gửi các yêu cầu thực tế để URL, nhưng chỉ khi thử nghiệm các dịch vụ trên mức chức năng. Trong trường hợp của tôi là "access denied" ngoại lệ được ném:
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:206) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60) ~[spring-security-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.2.1.RELEASE.jar:3.2.1.RELEASE]
...
Hai thông điệp log sau đây là đáng chú ý về cơ bản nói rằng không có người dùng được xác nhận chỉ ra rằng thiết lập Principal
đã không làm việc, hoặc rằng nó đã được ghi đè.
14:20:34.454 [main] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Secure object: ReflectiveMethodInvocation: public java.util.List test.TestController.test(); target is of class [test.TestController]; Attributes: [ROLE_USER]
14:20:34.454 [main] DEBUG o.s.s.a.i.a.MethodSecurityInterceptor - Previously Authenticated: org.sprin[email protected]9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
tên công ty của bạn, eu.ubicon, được hiển thị trong nhập khẩu của bạn . Đó không phải là một nguy cơ bảo mật? –
Xin chào, cảm ơn vì đã bình luận! Tôi không thể hiểu tại sao. Đó là phần mềm nguồn mở anyway. Nếu bạn quan tâm, hãy xem https://bitbucket.org/ubicon/ubicon (hoặc https://bitbucket.org/dmir_wue/everyaware cho ngã ba mới nhất). Hãy cho tôi biết nếu tôi bỏ lỡ một cái gì đó. –
Kiểm tra giải pháp này (câu trả lời dành cho mùa xuân 4): https://stackoverflow.com/questions/14308341/how-to-login-a-user-with-spring-3-2-new-mvc-testing/47069613 # 47069613 –