Refactor UserInterceptor examples

This commit is contained in:
Grzegorz Piwowarek 2016-08-23 07:38:30 +02:00
parent dca46bcee3
commit d8493fef63
2 changed files with 84 additions and 86 deletions

View File

@ -1,9 +1,5 @@
package org.baeldung.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
@ -12,6 +8,10 @@ import org.springframework.web.servlet.SmartView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserInterceptor extends HandlerInterceptorAdapter {
private static Logger log = LoggerFactory.getLogger(UserInterceptor.class);
@ -42,7 +42,6 @@ public class UserInterceptor extends HandlerInterceptorAdapter {
/**
* Used before model is generated, based on session
* @param session
*/
private void addToModelUserDetails(HttpSession session) {
log.info("================= addToModelUserDetails ============================");
@ -55,7 +54,6 @@ public class UserInterceptor extends HandlerInterceptorAdapter {
/**
* Used when model is available
* @param model
*/
private void addToModelUserDetails(ModelAndView model) {
log.info("================= addToModelUserDetails ============================");

View File

@ -1,8 +1,5 @@
package org.baeldung.web.interceptor;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.baeldung.spring.PersistenceConfig;
import org.baeldung.spring.SecurityWithoutCsrfConfig;
import org.baeldung.spring.WebConfig;
@ -20,15 +17,19 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
@ContextConfiguration(classes = { SecurityWithoutCsrfConfig.class, PersistenceConfig.class, WebConfig.class })
@WithMockUser(username="admin",roles={"USER","ADMIN"})
@ContextConfiguration(classes = {SecurityWithoutCsrfConfig.class, PersistenceConfig.class, WebConfig.class})
@WithMockUser(username = "admin", roles = {"USER", "ADMIN"})
public class UserInterceptorTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
@ -42,12 +43,11 @@ public class UserInterceptorTest {
/**
* After execution of HTTP GET logs from interceptor will be displayed in
* the console
*
* @throws Exception
*/
@Test
public void testInterceptors() throws Exception {
mockMvc.perform(get("/auth/admin")).andExpect(status().is2xxSuccessful());
mockMvc.perform(get("/auth/admin"))
.andExpect(status().is2xxSuccessful());
}
}