大屏项目初始化
This commit is contained in:
25
src/main/java/com/mini/mybigscreen/Config/CorsConfig.java
Normal file
25
src/main/java/com/mini/mybigscreen/Config/CorsConfig.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.mini.mybigscreen.Config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class CorsConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
// 允许所有路径跨域
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
// 允许的请求方法
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
// 允许的请求头
|
||||
.allowedHeaders("*")
|
||||
// 允许携带Cookie
|
||||
.allowCredentials(true)
|
||||
// 预检请求有效期(秒)
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.mini.mybigscreen.Config;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
|
||||
@Component
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
HttpSession session = request.getSession(false);
|
||||
if (session == null) {
|
||||
response.sendRedirect(request.getContextPath() + "/index.html");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
29
src/main/java/com/mini/mybigscreen/Config/WebConfig.java
Normal file
29
src/main/java/com/mini/mybigscreen/Config/WebConfig.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.mini.mybigscreen.Config;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Resource
|
||||
private LoginInterceptor loginInterceptor;
|
||||
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册登录拦截器,拦截所有请求,排除登录接口
|
||||
registry.addInterceptor(loginInterceptor)
|
||||
.addPathPatterns("/**") // 拦截所有路径
|
||||
.excludePathPatterns(
|
||||
"/",
|
||||
"/index.html",
|
||||
"/userLogin"
|
||||
)
|
||||
.excludePathPatterns("/static/**")
|
||||
.excludePathPatterns("/assets/**")
|
||||
; // 排除静态资源(前端打包后的文件)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user