基于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
  • 添加域名解析
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值