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