基於Springboot的個人博客開發

1. 使用IDEA創建Springboot項目

使用Spring Initializr創建Springboot項目,選擇使用maven構建項目,中間選擇依賴時,根據需要,選擇了web、jpa、mysql、thymeleaf、devtools等;

如果依賴下載的太慢的話可以修改中央倉庫爲阿里雲的倉庫。

2. 項目的開發

  • thymeleaf訪問map
    <p th:text="${ArticleComments['__${article.getId()}__'].size()}" >
  • 使用spring security進行訪問控制

a. 添加依賴

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

b. 基於註解方式接入security

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    }

c. 基於內存中數據的用戶認證

重寫WebSecurityConfigurerAdapter類中的protected void configure(AuthenticationManagerBuilder auth)方法;

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                .withUser("admin")
                .password("admin")
                .authorities("ADMIN"); //分配admin身份
        }

d. 自定義攔截器和登錄頁面

重寫WebSecurityConfigurerAdapter類中的protected void configure(HttpSecurity http)方法;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http.authorizeRequests()
                    .antMatchers("/").permitAll()   //任何人都可以訪問
                    .antMatchers("/admin/**").access("hasRole('ADMIN')"
                    //只有具有admin身份的用戶可以訪問/admin路徑下的頁面
                    .and()             					.formLogin().loginPage("/login").usernameParameter("username")
                        .passwordParameter("password").and()
                     .httpBasic();
        }

e. 解決There is no PasswordEncoder mapped for the id "null"報錯

        @Bean
        public static NoOpPasswordEncoder passwordEncoder() {
            return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
        }

參考:

Securing a Web Application

spring security 入門教程

  • 使用Git進行版本控制
    在IDEA中使用Git進行版本控制

3.將項目導出成war包並部署在阿里雲ESC上

  • 導出war包

該過程中有以下幾個注意點:

a. 需要改變application.properties中連接數據庫的設置;

b. 需要在pom.xml中加入依賴

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-tomcat</artifactId>
    	<scope>provided</scope>
    </dependency>

provided表明該包只在編譯和測試的時候用,使得項目部署在tomcat下後不與外部的tomcat衝突;

並將

<packaging>jar</packaging>

改爲

<packaging>war</packaging>

c. 需要改MyblogApplication

繼承SpringBootServletInitializer,並覆寫方法

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)

之後按網上的教程打包:

file->project structure->Artifacts 增加一個web application:archive

build->build Artifacts 得到war包

  • Centos上環境的搭建

參考:

手工部署Java Web項目

打造完美接口文檔 - 發佈springboot應用到阿里雲服務器

阿里雲部署Java網站和微信開發調試心得技巧(上)

  • 使用ftp上傳war包

使用了FileZilla,十分方便

  • 通過server.xml更改webapp映射

在tomcat/conf/server.xml中內添加:

    <Context path="" docBase="/usr/local/tomcat/webapps/xiguashu-v1" debug="0" privileged="true"> </Context>

path爲虛擬路徑

docBase爲web應用的物理路徑

這樣做使得不需通過x.x.x.x:port/project來訪問,而可以通過x.x.x.x:port/path

  • 使用sudo ./startup.sh 啓動80端口

通過修改server.xml,使tomcat監聽80端口

        <Connector port="80" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />

但啓動tomcat後發現80端口並沒有被偵聽,關閉防火牆、設置安全組之後依然不行。

搜索後得知,非root權限下運行tomcat無法監聽1024以下的端口。

最後的方法是:

    sudo ./startup.sh

奇怪的是以下命令卻還是無法啓動:

    sudo service tomcat start
  • 添加域名解析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章