上面重定向的问题:http://localhost:8080/crud/main 在用户不登录的状态,依然可以访问主页面信息?
2、拦截器 进行登录检查
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 登录拦截器 登录检查
* @author john
*
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
/**
*
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//return HandlerInterceptor.super.preHandle(request, response, handler);
Object attribute = request.getSession().getAttribute("loginUser");
if(StringUtils.isEmpty(attribute)){
request.setAttribute("msg", "没有权限请登录");
// 未登录,返回登录页面
request.getRequestDispatcher("/").forward(request, response);
// response.sendRedirect("/");
return false;
}else{
return true;
}
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import com.demo.springbootweb.component.LoginHandlerInterceptor;
import com.demo.springbootweb.component.MyLocaleResolver;
// 使用WebMvcConfigurationSupport可以来定制、扩展SpringMVC的功能
// @EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
/**
* 解决springboot项目中的html文件引用js、css、图片的问题
* 想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
super.addResourceHandlers(registry);
}
/**
* 页面跳转addViewControllers
* 注册路径映射
* @return
*/
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main").setViewName("dashboard");
super.addViewControllers(registry);
}
/**
* 注册拦截器
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
List<String> patterns = new ArrayList<String>(); // 排除访问请求
patterns.add("/");
patterns.add("/index.html");
patterns.add("/index");
patterns.add("/user/login");
patterns.add("/webjars/**");// webjars下的静态资源不需要拦截
patterns.add("/asserts/**");// asserts下的静态资源不需要拦截
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**") // 拦截所有请求
.excludePathPatterns(patterns);
super.addInterceptors(registry);
}
/**
* 配置自己的国际化语言解析器
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
}
|