Refactor UserInterceptor examples
This commit is contained in:
parent
dca46bcee3
commit
d8493fef63
@ -1,9 +1,5 @@
|
|||||||
package org.baeldung.web.interceptor;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
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.View;
|
||||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
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 {
|
public class UserInterceptor extends HandlerInterceptorAdapter {
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(UserInterceptor.class);
|
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
|
* Used before model is generated, based on session
|
||||||
* @param session
|
|
||||||
*/
|
*/
|
||||||
private void addToModelUserDetails(HttpSession session) {
|
private void addToModelUserDetails(HttpSession session) {
|
||||||
log.info("================= addToModelUserDetails ============================");
|
log.info("================= addToModelUserDetails ============================");
|
||||||
@ -55,7 +54,6 @@ public class UserInterceptor extends HandlerInterceptorAdapter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Used when model is available
|
* Used when model is available
|
||||||
* @param model
|
|
||||||
*/
|
*/
|
||||||
private void addToModelUserDetails(ModelAndView model) {
|
private void addToModelUserDetails(ModelAndView model) {
|
||||||
log.info("================= addToModelUserDetails ============================");
|
log.info("================= addToModelUserDetails ============================");
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
package org.baeldung.web.interceptor;
|
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.PersistenceConfig;
|
||||||
import org.baeldung.spring.SecurityWithoutCsrfConfig;
|
import org.baeldung.spring.SecurityWithoutCsrfConfig;
|
||||||
import org.baeldung.spring.WebConfig;
|
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.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
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)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@Transactional
|
@Transactional
|
||||||
@ContextConfiguration(classes = { SecurityWithoutCsrfConfig.class, PersistenceConfig.class, WebConfig.class })
|
@ContextConfiguration(classes = {SecurityWithoutCsrfConfig.class, PersistenceConfig.class, WebConfig.class})
|
||||||
@WithMockUser(username="admin",roles={"USER","ADMIN"})
|
@WithMockUser(username = "admin", roles = {"USER", "ADMIN"})
|
||||||
public class UserInterceptorTest {
|
public class UserInterceptorTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
WebApplicationContext wac;
|
WebApplicationContext wac;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
MockHttpSession session;
|
MockHttpSession session;
|
||||||
|
|
||||||
@ -42,12 +43,11 @@ public class UserInterceptorTest {
|
|||||||
/**
|
/**
|
||||||
* After execution of HTTP GET logs from interceptor will be displayed in
|
* After execution of HTTP GET logs from interceptor will be displayed in
|
||||||
* the console
|
* the console
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testInterceptors() throws Exception {
|
public void testInterceptors() throws Exception {
|
||||||
mockMvc.perform(get("/auth/admin")).andExpect(status().is2xxSuccessful());
|
mockMvc.perform(get("/auth/admin"))
|
||||||
|
.andExpect(status().is2xxSuccessful());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user