拦截器类 需要两个配合使用这里只有一个
@Configuration
public class ServletContextConfig extends WebMvcConfigurerAdapter {
public void
addResourceHandlers(Re
sourceHandlerRegistry registry) {
registry.
addResourceHandler("/static/**").
addResourceLocations("classpath:/static/");
registry.
addResourceHandler("/templates/**").
addResourceLocations("classpath:/templates/");
super.
addResourceHandlers(registry);
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new
HandlerMyInterceptorAdapter()).addPathPatterns("/**")
.excludePathPatterns("/")
// .excludePathPatterns("/expressions/getExpressionsList")
.excludePathPatterns("/loginInfo/getCordByIsPhone")
.excludePathPatterns("/loginInfo/login11") //token失效跳轉
.excludePathPatterns("/loginInfo/insertLoginInfo") //注册
.excludePathPatterns("/loginInfo/login") //登录
.excludePathPatterns("/upload") //上传文件
.excludePathPatterns("/uploadListen") //上传文件
.excludePathPatterns("/admin/user/goLogin") //后台跳转登录
.excludePathPatterns("/admin/user/login") //后台登录
.excludePathPatterns("/loginInfo/getLoginInfo") //忘记密码
.excludePathPatterns("/loginInfo/getCord") //短信验证码
.excludePathPatterns("/loginInfo/getIsLoginInfo") //判断验证码&&登录
.excludePathPatterns("/loginInfo/getIsLoginInfo1") //判断验证码
.excludePathPatterns("/loginInfo/setPassWord") //设置密码
@Override
public void configureDefaultServletHandling(DefaultServlet
HandlerConfigurer configurer) {
configurer.enable();
@Component
public class
HandlerMyInterceptorAdapter implements
HandlerInterceptor {
@Autowired
private HeartbeatServiceImpl heartbeatService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object
handler) throws IOException {
String url = request.getRequestURI();
if (url.contains("admin")) {
User user = (User) request.getSession().getAttribute("user");
try {
if (user.equals(null)) {
response.sendRedirect(serverConfig.SERVER + "admin/user/goLogin");
return false;
} else {
return true;
}catch (Exception e){
response.sendRedirect(serverConfig.SERVER + "admin/user/goLogin");
return false;
}else {
String token = request.getHeader("token");
if (token != null) {
Jedis jedis = new Jedis(com.sevenSteps.util.RedisConfig.HOST, RedisConfig.PORT);
String s = jedis.get(token);
if(token.equals(s)) {
heartbeatService = SpringUtil.getBean(HeartbeatServiceImpl.class);
return heartbeatService.setOutDate(token);
}else {
response.sendRedirect(serverConfig.SERVER + "loginInfo/login11");
return true;
}else {
response.sendRedirect(serverConfig.SERVER + "loginInfo/login11");
return true;
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
springboot+nginx访问文件或重写addResourceHandlers映射文件路径
每天多学一点点~
最近跳槽了去新公司,清闲了两周,心里面有点慌,遂研究了点新东西~
话不多说,这就开始吧…
文章目录springboot+nginx访问文件或重写addResourceHandlers映射文件路径1.序言2.addResourceHandlers方法3.测试映射文件路径4.nginx配...
场景:部署的
Springboot项目,打成jar包,需要通过url直接访问jar外部路径的
资源,本人的
Springboot为2.1.11.RELEASE。
一、
配置WebMvcConfigurer,设置url访问静态
资源映射
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
自定义资源映射addResourceHandlers:
在springboot中,我们可以通过重写addResourceHandlers方法来映射静态资源目录;
具体做法:编写类继承WebMvcConfigurerAdapter类,重写该类的addResourceHandlers方法;其中addResourceHandler指向映射路径,addResourceLocations指向资源文件路径;资...
开始我是这样设置的
registry.
addResourceHandler("/upload/**").
addResourceLocations("file:" + path); 完整代码如下图:
path对应的路径在 yml 文件
中设置,如下图
然后项目启动后,通过在浏览器
中直接访问文件的地址(例如:http://localhost:8090/app...
@Configuration
public class MyWebMVCConfig implements WebMvcConfigurer {
@Value("${file.
location}") // D:/test/
String file
location; // 这两个是路径
@Value("${file.path}") // /file/**
String
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.se...