package com.gkyt.ywgk.modular.filter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
public WebConfig() {
super();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
super.addResourceHandlers(registry);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 拦截规则
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/images/*")
.excludePathPatterns("/api/**").excludePathPatterns("/users/login")
.excludePathPatterns("/users/logout");
super.addInterceptors(registry);
}
}
package com.gkyt.ywgk.modular.filter;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.gkyt.ywgk.core.enums.ErrorEnums;
import com.gkyt.ywgk.modular.dao.dto.UsersDto;
import com.gkyt.ywgk.modular.service.PrisonConfigService;
public class MyInterceptor implements HandlerInterceptor {
@Autowired
private PrisonConfigService prisonConfigService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String url = request.getRequestURI();
if (url.indexOf("swagger") >= 0 || url.indexOf("api-docs") >= 0 || url.indexOf("/configuration/") >= 0) {
return true;
}
// 获取session
HttpSession session = request.getSession(true);
UsersDto users = (UsersDto) session.getAttribute("s_user");
// 未登录
if (users == null) {// 为登录业务处理....
response.setStatus(401);
ServletOutputStream out = response.getOutputStream();
out.println("unlogin");
out.flush();
out.close();
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}