mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-09-07 07:07:57 +08:00
init
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.hxkj.front;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* 启动器
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.hxkj"})
|
||||
@MapperScan(basePackages = {"com.hxkj.*.mapper"})
|
||||
@EnableTransactionManagement
|
||||
@SpringBootApplication(exclude = {RedisRepositoriesAutoConfiguration.class})
|
||||
public class LikeFrontApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LikeFrontApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.hxkj.front;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.hxkj.common.core.AjaxResult;
|
||||
import com.hxkj.common.enums.HttpEnum;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
*/
|
||||
@Component
|
||||
public class LikeFrontInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// 404拦截
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
if (response.getStatus() == 404) {
|
||||
AjaxResult result = AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), HttpEnum.REQUEST_404_ERROR.getMsg());
|
||||
response.getWriter().print(JSON.toJSONString(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
// 验证通过继续操作
|
||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
LikeFrontThreadLocal.remove();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.hxkj.front;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class LikeFrontThreadLocal {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public LikeFrontThreadLocal() {}
|
||||
|
||||
/**
|
||||
* 取得本地线程对象
|
||||
*/
|
||||
private static final java.lang.ThreadLocal<LinkedHashMap<String, Object>> MY_LOCAL = new java.lang.ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 写入本地线程
|
||||
*/
|
||||
public static void put(String key, Object val) {
|
||||
LinkedHashMap<String, Object> map = MY_LOCAL.get();
|
||||
if (map == null) {
|
||||
map = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
map.put(key, val);
|
||||
MY_LOCAL.set(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地线程
|
||||
*/
|
||||
public static Object get(String key) {
|
||||
return MY_LOCAL.get().getOrDefault(key, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户ID
|
||||
*/
|
||||
public static Integer getUserId() {
|
||||
String adminId = LikeFrontThreadLocal.get("userId").toString();
|
||||
if (adminId.equals("")) {
|
||||
return 0;
|
||||
}
|
||||
return Integer.parseInt(adminId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除本地线程
|
||||
*/
|
||||
public static void remove() {
|
||||
MY_LOCAL.remove();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.hxkj.front.config;
|
||||
|
||||
/**
|
||||
* 前台公共配置
|
||||
*/
|
||||
public class FrontConfig {
|
||||
|
||||
// 免登录验证
|
||||
public static String[] notLoginUri = new String[]{
|
||||
"/api/login"
|
||||
};
|
||||
|
||||
// 免权限验证
|
||||
public static String[] notAuthUri = new String[]{};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hxkj.front.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 分页插件集成
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.hxkj.front.config;
|
||||
|
||||
import com.hxkj.common.config.GlobalConfig;
|
||||
import com.hxkj.common.utils.YmlUtil;
|
||||
import com.hxkj.front.LikeFrontInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Resource
|
||||
LikeFrontInterceptor likeFrontInterceptor;
|
||||
|
||||
/**
|
||||
* 配置允许跨域
|
||||
*/
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins("*")
|
||||
.allowedHeaders("*")
|
||||
.allowedMethods("GET", "POST", "DELETE", "PUT")
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录拦截器
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(likeFrontInterceptor).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源目录映射
|
||||
*/
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
String directory = YmlUtil.get("like.upload-directory");
|
||||
if (directory == null || directory.equals("")) {
|
||||
directory = GlobalConfig.uploadDirectory;
|
||||
}
|
||||
|
||||
registry.addResourceHandler("/"+ GlobalConfig.publicPrefix +"/**")
|
||||
.addResourceLocations("file:" + directory);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.hxkj.front.controller;
|
||||
|
||||
import com.hxkj.common.core.AjaxResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class IndexController {
|
||||
|
||||
@GetMapping("/index")
|
||||
public Object index() {
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user