前端工程
访问方式
powershell
http://127.0.0.1:8080/context/frontEnd/index
放行
java
public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/frontEnd/**",
SysFrontEndController
java
import lombok.extern.slf4j.Slf4j;
import nl.basjes.shaded.org.springframework.core.io.ClassPathResource;
import nl.basjes.shaded.org.springframework.util.AntPathMatcher;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.Optional;
/**
* @author 蓝之静云
* @date 2023-08-10
*/
@RestController
@RequestMapping("/frontEnd")
@Slf4j
public class SysFrontEndController {
private AntPathMatcher antPathMatcher = new AntPathMatcher();
@RequestMapping("/**")
public void getStaticResource(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 获取完整的路径
String uri = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// 获取映射的路径
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String customPath = antPathMatcher.extractPathWithinPattern(pattern, uri);
if ("index".equals(customPath)) customPath = customPath + ".html";
// 按/截取
String[] split = customPath.split("/");
// 最后一个就是文件名
String filename = split[split.length - 1];
// 若是图片资源
if (filename.contains(".png")
|| filename.contains(".jpg")
|| filename.contains(".ico")
|| filename.contains(".gif")
|| filename.contains(".svg")
|| filename.contains(".pdf")
|| filename.contains(".jpeg")) {
ServletOutputStream outputStream = null;
InputStream inputStream = null;
try {
ClassPathResource classPathResource = new ClassPathResource("static/" + customPath);
inputStream = classPathResource.getInputStream();
response.setContentType("image/" + filename.split("\\.")[1]);
outputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStream.close();
inputStream.close();
}
} else {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/" + customPath);
assert stream != null;
ByteBuffer buf = ByteBuffer.allocate(stream.available());
Channels.newChannel(stream).read(buf);
buf.flip();
String fileStr = Charset.defaultCharset().decode(buf).toString();
buf.clear();
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(filename);
mediaType.ifPresent(type -> response.setContentType(type.toString() + ";charset=UTF-8"));
response.setCharacterEncoding("UTF-8");
response.getWriter().write(fileStr);
}
}
}
pom.xml
xml
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>