国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

記錄一下使用SpringBoot訪問靜態(tài)資源和SpringBoot打包之后的jar外部靜態(tài)資源,在開發(fā)的時(shí)候,一般選擇前后端分離的方式,前端使用vue 后端使用SpringBoot,通常情況下,部署都是前端通過http去請(qǐng)求后端資源,涉及到http請(qǐng)求,那么肯定需要資源的目標(biāo)地址IP,一般云端部署給到IP就可以,可是在某些特定的場(chǎng)合下,這個(gè)IP是不確定的,比如內(nèi)網(wǎng)里某臺(tái)內(nèi)網(wǎng)設(shè)備部署一套采集程序,在事先知道內(nèi)網(wǎng)IP的情況下可以前端用nginx部署 后臺(tái)就是SpingBoot的jar運(yùn)行,可是幾十臺(tái)內(nèi)網(wǎng)設(shè)備都需要部署的話,那么前端打包的http請(qǐng)求地址將會(huì)根據(jù)具體的IP改變。
(不可能每臺(tái)電腦都用http://127.0.0.1代替訪問,如此的話,在內(nèi)網(wǎng)里其他主機(jī)去訪問就訪問不到后臺(tái)數(shù)據(jù),因?yàn)槟繕?biāo)變成了你正在操作訪問的電腦了)

這里有一個(gè)解決辦法,就是將前端vue項(xiàng)目打包后放入SpringBoot的靜態(tài)資源里,實(shí)現(xiàn)了將前后端分離的應(yīng)用一體化了

SpringBoot訪問靜態(tài)資源 可以將靜態(tài)資源打包在jar內(nèi)

首先去尋找資源路徑(使用的是SpringBoot2.0.5)找到web啟動(dòng)類

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

點(diǎn)進(jìn)去找到包spring-boot-starter

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.0.5.RELEASE</version>
  <scope>compile</scope>
</dependency>

使用SpringBoot的基礎(chǔ)配置在spring-boot-autoconfigure(里面超級(jí)多配置)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
  <version>2.0.5.RELEASE</version>
  <scope>compile</scope>
</dependency>

SpringBoot啟動(dòng)默認(rèn)加載自動(dòng)配置類,自動(dòng)配置類的格式是xxxxxxAutoConfiguration,要尋找的(靜態(tài))資源配置在WebMvcAutoConfiguration類中
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
點(diǎn)進(jìn)去可以看到靜態(tài)資源的默認(rèn)配置
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里

“classpath:/META-INF/resources/”,
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”

他們的默認(rèn)訪問先后順序:/META-INF/resources/>/resources/>/static/>/public/
默認(rèn)訪問域名+端口,如果存在頁(yè)面會(huì)去找默認(rèn)頁(yè)面index.html或index定向的地址

classpath:/META-INF/resources/
有其他方式在,這種方式一般不會(huì)去使用的,嘗嘗用在WebJars情況,比如映入常用的swagger,jquery,springBoot會(huì)默認(rèn)把/webjars/**映射到 classpath:/META-INF/resources/webjars/這里,個(gè)人用的極少。

 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
 </dependency>
 <dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>swagger-bootstrap-ui</artifactId>
     <version>1.9.2</version>
 </dependency>
<dependency>
	 <groupId>org.webjars</groupId>
	 <artifactId>jquery</artifactId>
	 <version>3.6.0</version>
</dependency>
        

classpath:/resources/, classpath:/static, classpath:/public 相對(duì)來說 static和public用的比較多 resources用的比較少
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
熟悉了SpringBoot訪問靜態(tài)資源的默認(rèn)配置,那么可以將前端vue項(xiàng)目打包后來測(cè)試,首先vue項(xiàng)目中有一個(gè)點(diǎn)兒得注意,那就是注釋掉axios的統(tǒng)一攔截器里的baseURL 不需要用到這個(gè),如果忘記注釋,那極有可能是本地能訪問,線上不能訪問。
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
放入static的目錄下面
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
啟動(dòng)SpringBoot后瀏覽器訪問 只需要訪問

http://localhost:24800/

會(huì)自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面

http://localhost:24800/#/login

SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
如此SpringBoot加上前端vue打包后的代碼一起打包成jar就成了,可是出現(xiàn)的另外的問題,當(dāng)前端改變后,要打包,對(duì)于一些特定場(chǎng)景下的維護(hù)不太好,于是得找一個(gè)比較合理的方式,前端只需要將vue打包后的前端代碼打包到對(duì)應(yīng)的目錄里,后端代碼不需要嵌入前端的打包代碼,不用每次前端改變而重新打包。

SpringBoot訪問靜態(tài)資源不打包在jar內(nèi)

在配置文件里application.properties
Windows系統(tǒng)絕對(duì)路徑

spring.resources.static-locations=file:///C:/DevelopSpace/static/,file:///C:/DevelopSpace/public/

Windows系統(tǒng)相對(duì)路徑

spring.resources.static-locations=file:./static/,file:./public/

Centos7系統(tǒng)絕對(duì)路徑

spring.resources.static-locations=file:/home/boot-black/static/,file:/home/boot-black/public/

Centos7系統(tǒng)相對(duì)路徑

spring.resources.static-locations=file:./static/,file:./public/

使用這種方式在啟動(dòng)SpringBoot之前,必須將對(duì)應(yīng)的static或public提前建立并且把vue編譯后的源碼放進(jìn)去才能訪問到,如果先啟動(dòng)SpringBoot的jar 再來對(duì)應(yīng)地址新建static和public,將vue編譯后的源碼放進(jìn)去,那么瀏覽器訪問將會(huì)得到那熟悉的錯(cuò)誤頁(yè)面

另外,當(dāng)前端重新編譯后放到指定public和static后,需要重新刷新瀏覽器,清除緩存才能用,如此一般沒有遇到問題,用這種方式,具體前端vue編譯的代碼變化很大的情況,沒有詳細(xì)測(cè)試過。

SpringBoot整合vue編譯后的文件搞定之后,如果SpringBoot有攔截器,那么這里又會(huì)出現(xiàn)訪問路徑訪問不到的問題,可是用域名+端口去訪問 (http://localhost或者h(yuǎn)ttp://localhost:24800)
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里
我的攔截器配置
JwtTokenHandlerInterceptor.java

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class JwtTokenHandlerInterceptor implements HandlerInterceptor {

    private final Logger log =  LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL());
        String token = request.getHeader("token");
        log.warn(request.getRequestURI());
        if (!StringUtils.isNotEmptyBatch(token)) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            Response responseResult = ResponseCode.invalidHeaderTokenResponse("api接口");
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(responseResult);
            response.getWriter().println(str);
        	return false;
        }
		// 驗(yàn)證token 
		// to do
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }

}

JwtTokenInterceptorConfig.java

import org.springframework.context.annotation.Bean;
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 JwtTokenInterceptorConfig {

    @Bean
    public JwtTokenHandlerInterceptor jwtTokenHandlerInterceptor(){
        return new JwtTokenHandlerInterceptor();
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(jwtTokenHandlerInterceptor())
                        .addPathPatterns("/**")
                        .excludePathPatterns("/")
                        .excludePathPatterns("/login") // 登錄除外
            }
        };
    }
}

在攔截器里打印輸出可以看到,那么把index.html放開

/index.html
http://localhost:24800/index.html
/index.html
http://localhost:24800/index.html

@Bean
public WebMvcConfigurer webMvcConfigurer(){
    return new WebMvcConfigurer() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(jwtTokenHandlerInterceptor())
                    .addPathPatterns("/**")
                    .excludePathPatterns("/")
                    .excludePathPatterns("/index.html")
                    .excludePathPatterns("/login") // 登錄除外
        }
    };
}

訪問還是不行,又爆出其他路徑問題

/css/app.css
http://localhost:24800/css/app.css
/css/app.0.6.css
http://localhost:24800/css/app.0.6.css
/js/app.0.6.js
http://localhost:24800/js/app.0.6.js
/css/chunk-vendors.0.6.css
http://localhost:24800/css/chunk-vendors.0.6.css
/css/chunk-vendors.css
http://localhost:24800/css/chunk-vendors.css
/js/chunk-vendors.0.6.js

css js fonts img等路徑又被攔截器攔截了,那么全部放開

@Bean
public WebMvcConfigurer webMvcConfigurer(){
    return new WebMvcConfigurer() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(jwtTokenHandlerInterceptor())
                    .addPathPatterns("/**")
                    .excludePathPatterns("/")
                    .excludePathPatterns("/index.html")
                    .excludePathPatterns("/js/**")
                    .excludePathPatterns("/css/**")
                    .excludePathPatterns("/fonts/**")
                    .excludePathPatterns("/img/**")                    
                    .excludePathPatterns("/login") // 登錄除外
        }
    };
}

如此,便解決了攔截器攔截的問題 剛好這幾個(gè)對(duì)應(yīng)的是vue編譯后的css js fonts img index.html路徑
SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里文章來源地址http://www.zghlxwxcb.cn/news/detail-491168.html

到了這里,關(guān)于SpringBoot訪問靜態(tài)資源和jar外部靜態(tài)資源,部署前端打包后的vue項(xiàng)目放入靜態(tài)資源里的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包