Java/Spring引导Webapp不服务于公共资源目录中的资产
我正在开发一个由Spring Boot提供服务的AngularJS应用程序。我正在升级我们的构建管道以包括Webpack。Webpack正在将所有源代码绑定到一个Java/Spring引导Webapp不服务于公共资源目录中的资产,java,angularjs,spring,spring-mvc,Java,Angularjs,Spring,Spring Mvc,我正在开发一个由Spring Boot提供服务的AngularJS应用程序。我正在升级我们的构建管道以包括Webpack。Webpack正在将所有源代码绑定到一个/src/main/resources/static目录中,我被告知Spring Boot应该自动为该目录提供服务。然而,当我试图通过导航到http://localhost:8080,提供了index.html页面,但各种JS捆绑包没有。以下是一些用于配置此功能的相关文件: webpack.config.js /*global proc
/src/main/resources/static
目录中,我被告知Spring Boot应该自动为该目录提供服务。然而,当我试图通过导航到http://localhost:8080
,提供了index.html
页面,但各种JS捆绑包没有。以下是一些用于配置此功能的相关文件:
webpack.config.js
/*global process, module, __dirname*/
const path = require('path');
const proxyMiddleware = require('proxy-middleware');
const url = require('url');
const webpack = require('webpack');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const PATHS = {
app: path.join(__dirname, './src/main/webapp'),
dist: path.join(__dirname, './src/main/resources/static'),
test: path.join(__dirname, './src/test/webapp')
};
const isDevelopment = process.env.NODE_ENV === 'develop';
const isE2E = process.env.NODE_ENV === 'e2e';
const isTest = process.env.NODE_ENV === 'test';
const isProd = process.env.NODE_ENV === 'production';
// Webpack Loaders
const fontRule = {
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader',
options: {
name: '[name].[sha1:hash:base64:32].[ext]'
}
};
const htmlRule = {
test: /\.html$/,
loader: 'html-loader',
query: {
minimize: isProd
}
};
const imageRule = {
test: /\.png$/i,
loader: 'url-loader',
options: {
limit: 8192,
mimetype: 'image/png'
}
};
const javasscriptPreRule = {
test: /\.js$/,
exclude: /node_modules/,
enforce: 'pre',
loader: 'eslint-loader'
};
const javascriptRule = {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
};
const sassRule = {
test : /\.scss$/,
use: ExtractTextPlugin.extract({
use: [ 'css-loader', 'resolve-url-loader', 'sass-loader?sourceMap' ]
})
};
const entry = {
app: (() => {
let app = [ path.join(PATHS.app, 'app.js') ];
if (isProd || isE2E) {
app.push(path.join(PATHS.app, 'app.prod.js'));
} else {
app.push(path.join(PATHS.app, 'app.mock.js'));
}
return app;
})()
};
const output = {
path: PATHS.dist,
filename: isProd ? '[name].[chunkhash].js' : '[name].js'
};
const plugins = (() => {
let plugins = [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks(module) {
return module.context
&& module.context.indexOf('node_modules') !== -1;
}
}),
new webpack.optimize.CommonsChunkPlugin({ name: 'manifest' }),
new ExtractTextPlugin(isProd ? 'styles.[contenthash].css' : 'styles.css'),
new HtmlWebpackPlugin({ template: path.join(PATHS.app, 'index.html') }),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
})
];
if (isProd) {
plugins = plugins.concat([
new webpack.optimize.UglifyJsPlugin({
beautify: false,
comments: false,
compress: {
warnings: false
}
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new OptimizeCssAssetsWebpackPlugin({
cssProcessorOptions: {
discardComments: { removeAll: true }
}
})
]);
} else {
const server = (() => {
let server = {
baseDir: PATHS.dist
};
// Internal testing server configurations...
return server;
})();
plugins.push(
new BrowserSyncPlugin({
host: 'localhost',
port: 3000,
server
})
)
}
return plugins;
})();
function proxy(target) {
let options = url.parse(target);
options.route = '/api';
return proxyMiddleware(options);
}
module.exports = {
entry,
output,
plugins,
module: {
rules: [ fontRule, htmlRule, imageRule, javasscriptPreRule, javascriptRule, sassRule ]
}
};
WebSecurityConfig.java
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements InitializingBean {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
// Internal security configurations
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest().permitAll();
}
}
以下是存储在/src/target/classes/static
中的静态文件:
以下是打开浏览器时JS文件未显示的证据:
我发现了我遇到的问题。Spring Boot仍然默认为
src/main/webapp
来提供前端代码。虽然该目录中有一个index.html
文件,但它不包含Webpack生成的JavaScript包的
标记;Webpack负责自动添加这些标记。我必须更改加载捆绑文件的目录
为此,我编辑了src/main/resources/application.properties
文件,并添加了以下行:
spring.resources.static locations=classpath:/static/
发布target/classes/static
的目录输出。这是一个巨大的Webpack配置转储,但它并没有清楚地显示文件保存在正确的位置(或者在正确的时间,在Maven中这将是生成资源
阶段)。我用目标/类/静态
的屏幕截图更新了我的原始帖子。好的,看起来它们就在那里。你的网络标签呢?你有404吗?还有什么?只提取HTML文件,不幸的是没有其他资源。这不是答案。它没有说明是否没有请求其他资源(您没有包含脚本
标记),或者是否存在其他问题(确切的错误消息)。