Refactor UserInterceptor examples
This commit is contained in:
parent
dca46bcee3
commit
d8493fef63
|
@ -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,76 +8,78 @@ 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);
|
||||
private static Logger log = LoggerFactory.getLogger(UserInterceptor.class);
|
||||
|
||||
/**
|
||||
* Executed before actual handler is executed
|
||||
**/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
|
||||
if (isUserLogged()) {
|
||||
addToModelUserDetails(request.getSession());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
|
||||
if (isUserLogged()) {
|
||||
addToModelUserDetails(request.getSession());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executed before after handler is executed. If view is a redirect view, we don't need to execute postHandle
|
||||
**/
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model)
|
||||
throws Exception {
|
||||
if (model != null && !isRedirectView(model)) {
|
||||
if (isUserLogged()) {
|
||||
addToModelUserDetails(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model)
|
||||
throws Exception {
|
||||
if (model != null && !isRedirectView(model)) {
|
||||
if (isUserLogged()) {
|
||||
addToModelUserDetails(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used before model is generated, based on session
|
||||
* @param session
|
||||
*/
|
||||
private void addToModelUserDetails(HttpSession session) {
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
session.setAttribute("username", loggedUsername);
|
||||
log.info("user(" + loggedUsername + ") session : " + session);
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
/**
|
||||
* Used before model is generated, based on session
|
||||
*/
|
||||
private void addToModelUserDetails(HttpSession session) {
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
session.setAttribute("username", loggedUsername);
|
||||
log.info("user(" + loggedUsername + ") session : " + session);
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used when model is available
|
||||
* @param model
|
||||
*/
|
||||
private void addToModelUserDetails(ModelAndView model) {
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
model.addObject("loggedUsername", loggedUsername);
|
||||
log.trace("session : " + model.getModel());
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
/**
|
||||
* Used when model is available
|
||||
*/
|
||||
private void addToModelUserDetails(ModelAndView model) {
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
model.addObject("loggedUsername", loggedUsername);
|
||||
log.trace("session : " + model.getModel());
|
||||
log.info("================= addToModelUserDetails ============================");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isRedirectView(ModelAndView mv) {
|
||||
public static boolean isRedirectView(ModelAndView mv) {
|
||||
|
||||
String viewName = mv.getViewName();
|
||||
if (viewName.startsWith("redirect:/")) {
|
||||
return true;
|
||||
}
|
||||
String viewName = mv.getViewName();
|
||||
if (viewName.startsWith("redirect:/")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
View view = mv.getView();
|
||||
return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView());
|
||||
}
|
||||
|
||||
public static boolean isUserLogged() {
|
||||
try {
|
||||
return !SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser");
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
View view = mv.getView();
|
||||
return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView());
|
||||
}
|
||||
|
||||
public static boolean isUserLogged() {
|
||||
try {
|
||||
return !SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser");
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,34 +17,37 @@ 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;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
@Autowired
|
||||
WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
|
||||
}
|
||||
@Autowired
|
||||
MockHttpSession session;
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* After execution of HTTP GET logs from interceptor will be displayed in
|
||||
* the console
|
||||
*/
|
||||
@Test
|
||||
public void testInterceptors() throws Exception {
|
||||
mockMvc.perform(get("/auth/admin"))
|
||||
.andExpect(status().is2xxSuccessful());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue