网页内容访问 、 部署动态网站 、 安全Web服务案例

1 案例1:配置网页内容访问

1.1 问题

本例要求在 Web 网站 http://server0.example.com 的 DocumentRoot 目录下创建一个名为 private 的子目录,要求如下:

从 http://classroom/pub/materials/private.html 下载一个文件副本到这个目录,重命名为 index.html
不要对文件 index.html 的内容作任何修改
从 server0 上,任何人都可以浏览 private 的内容,但是从其他系统不能访问这个目录的内容

1.2 方案

配置Web内容的访问控制需要添加Directory区段,主要形式可参考

<Directory “父目录路径”>
Require all denied //上层目录拒绝任何访问

<Directory “子目录1路径”>
Require all granted //子目录1允许任何访问

<Directory “子目录2路径”>
Require ip IP或网段地址 … … //子目录2允许少数客户机

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署网页子目录及文档

1)建立子目录

[root@server0 ~]# mkdir /var/www/html/private
2)部署网页

[root@server0 ~]# cd /var/www/html/private
[root@server0 private]# wget http://classroom/pub/materials/private.html -O index.html
… …
2016-11-26 20:30:28 (1.90 MB/s) - ‘index.html’ saved [14/14]

[root@server0 private]# cat index.html //检查网页文件
Private Site.
步骤二:为指定的网页子目录限制访问

在httpd服务的标准配置中,根目录 / 默认拒绝任何访问,但网页目录/var/www/默认允许任何访问。因此,只需要为个别子目录增加访问控制即可。

1)调整虚拟站点server0.example.com的配置文件

[root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
… …
<Directory “/var/www/html/private”>
Require ip 127.0.0.1 ::1 172.25.0.11

2)重启系统服务httpd

[root@server0 ~]# systemctl restart httpd
步骤三:测试目录访问限制

1)从desktop0上访问http://server0.example.com/private/被拒绝

[root@desktop0 ~]# elinks -dump http://server0.example.com/private/
Forbidden

You don’t have permission to access /private/ on this server.
2)从desktop0上访问http://server0.example.com/仍然是正常的

[root@desktop0 ~]# elinks -dump http://server0.example.com/
Default Site.
3)从server0本机上访问http://server0.example.com/private/也不受限制

[root@server0 ~]# elinks -dump http://server0.example.com/private/
Private Site.

2 案例2:使用自定Web根目录

2.1 问题

本例要求调整 Web 站点 http://server0.example.com 的网页目录,要求如下:

新建目录 /webroot,作为此站点新的网页目录
从 http://classroom/pub/materials/station.html 下载一个文件副本到这个目录,重命名为 index.html
不要对文件 index.html 的内容作任何修改
确保站点 http://server0.example.com 仍然可访问

2.2 方案

在SELinux强制启用模式下,增加新的合规网页目录的方法:

1)参照标准目录,重设新目录的属性

chcon [-R] --reference=模板目录 新目录
或者

2)将新目录增加到预设的标准Web目录范围

semanage fcontext -a -t httpd_sys_content_t ‘新目录(/.*)?’

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署网页目录及文档

1)建立网页目录

[root@server0 ~]# mkdir /webroot
2)部署网页文件

[root@server0 ~]# cd /webroot/
[root@server0 webroot]# wget http://classroom/pub/materials/station.html -O index.html
… …
2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
[root@server0 webroot]# cat index.html //检查网页文件
Default Site.
步骤二:调整虚拟站点http://server0.example.com/的配置

1)修改配置文件

[root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /webroot

… …
2)重启系统服务httpd

[root@server0 ~]# systemctl restart httpd
步骤三:确保虚拟站点http://server0.example.com/仍然可以访问

1)未调整网页目录SELinux上下文件的情况

为虚拟站点http://server0.example.com/更换了新的网页目录以后,从浏览器访问将会失败,只能看到红帽测试页。

[root@desktop0 ~]# elinks -dump http://server0.example.com/
Red Hat Enterprise Linux Test Page

This page is used to test the proper operation of the Apache HTTP server
after it has been installed. If you can read this page, it means that the
Apache HTTP server installed at this site is working properly.
… …
针对此问题,可以参考目录/var/www的属性为网页目录/webroot设置SELinux安全上下文。

[root@server0 ~]# chcon -R --reference=/var/www /webroot/
[root@server0 ~]# ls -Z /webroot/index.html //确认结果
-rw-r–r–. root root system_u:object_r:httpd_sys_content_t:s0 /webroot/index.html
2)未配置目录内容访问的情况

尽管已经调整过/webroot的SELinux安全上下文,但是从浏览器访问此虚拟站点时仍然会被拒绝,还是只能看到红帽测试页。

还需要修改对应的配置文件,添加内容访问控制:

[root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
<VirtualHost *:80>
ServerName server0.example.com
DocumentRoot /webroot

<Directory “/webroot”>
Require all granted

<Directory “/webroot/private”>
Require ip 127.0.0.1 ::1 172.25.0.11

[root@server0 ~]# systemctl restart httpd //重启httpd服务
若要保持原有private子目录,建议也拷贝过来:

[root@server0 ~]# cp -rf /var/www/html/private/ /webroot/
3)最终访问测试

从浏览器能成功访问调整后的虚拟站点http://server0.example.com/。

[root@desktop0 ~]# elinks -dump http://server0.example.com/
Default Site.

3 案例3:部署并测试WSGI站点

3.1 问题

本例要求为站点 webapp0.example.com 配置提供动态Web内容,要求如下:

此虚拟主机侦听在端口8909
测试网页从以下地址下载,不要作任何更改http://classroom/pub/materials/webinfo.wsgi
从浏览器访问 http://webapp0.example.com:8909 可接收到动态生成的 Web 页面
此站点必须能被 example.com 域内的所有系统访问

3.2 方案

为httpd增加对Python网页程序的支持,可以安装mod_wsgi模块。关于此模块的配置说明,建议参考软件包提供的readme文档。

在SELinux处于Enforcing模式时,若要开放非80、81等常规Web端口,需要调整SELinux保护策略。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署动态网页文档

1)创建网页目录

[root@server0 ~]# mkdir /var/www/webapp0
2)部署webinfo.wsgi网页程序

[root@server0 ~]# cd /var/www/webapp0
[root@server0 webapp0]# wget http://classroom/pub/materials/webinfo.wsgi
… …
2016-11-27 01:52:26 (16.0 MB/s) - ‘webinfo.wsgi’ saved [397/397]

[root@server0 webapp0]# cat webinfo.wsgi //检查下载文件
#!/usr/bin/env python
import time
… …
步骤二:配置新的虚拟主机http://webapp0.example.com:8909/

1)安装mod_wsgi模块软件包

[root@server0 ~]# yum -y install mod_wsgi
… …
2)为新虚拟主机建立配置

[root@server0 ~]# vim /etc/httpd/conf.d/02-webapp0.conf
Listen 8909
<VirtualHost *:8909>
DocumentRoot /var/www/webapp0
ServerName webapp0.example.com
WSGIScriptAlias / /var/www/webapp0/webinfo.wsgi

3)调整SELinux策略,允许Web服务使用8909端口

列出当前许可的Web端口:

[root@server0 ~]# semanage port -l | grep ^http_port
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
添加新的Web端口:

[root@server0 ~]# semanage port -a -t http_port_t -p tcp 8909
[root@server0 ~]#
确认配置结果:

[root@server0 ~]# semanage port -l | grep ^http_port
http_port_t tcp 8909, 80, 81, 443, 488, 8008, 8009, 8443, 9000
4)重启系统服务httpd

[root@server0 ~]# systemctl restart httpd
[root@server0 ~]# netstat -antpu | grep httpd //确认已监听8909端口
tcp6 0 0 :::443 ::😗 LISTEN 2477/httpd
tcp6 0 0 :::8909 ::😗 LISTEN 2477/httpd
tcp6 0 0 :::80 ::😗 LISTEN 2477/httpd
步骤三:测试动态网页效果

使用elinks或firefox访问此动态站点http://webapp0.example.com:8909/。

多刷新访问几次,每次看到的是动态网页内容,内容并不固定。

[root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
UNIX EPOCH time is now: 1480184916.52 //第1次访问
[root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
UNIX EPOCH time is now: 1480184919.21 //第2次访问
[root@desktop0 ~]# elinks -dump http://webapp0.example.com:8909/
UNIX EPOCH time is now: 1480184951.99 //第3次访问

4 案例4:配置安全Web服务

4.1 问题

本例要求为站点 http://server0.example.com 配置TLS加密

一个已签名证书从以下地址获取 http://classroom/pub/tls/certs/server0.crt
此证书的密钥从以下地址获取 http://classroom/pub/tls/private/server0.key
此证书的签名授权信息从以下地址获取http://classroom/pub/example-ca.crt

4.2 方案

安全Web传输协议及端口:TCP 443

访问HTTP站点(未加密):http://server0.example.com/

访问HTTPS站点(加密):https://server0.example.com/

为httpd服务端实现TLS加密的条件:1)启用一个 mod_ssl 模块;2)提供加密的素材:网站服务器的数字证书、网站服务器的私钥、根证书(证书颁发机构的数字证书)

TLS证书部署位置:/etc/pki/tls/certs/*.crt

TLS私钥部署位置:/etc/pki/tls/private/*.key

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置HTTPS网站服务器

1)安装mod_ssl模块软件包

[root@server0 ~]# yum -y install mod_ssl
… …
2)部署密钥、证书等素材

[root@server0 ~]# cd /etc/pki/tls/certs/
[root@server0 certs]# wget http://classroom/pub/example-ca.crt
… …
2016-11-27 01:04:51 (116 MB/s) - ‘example-ca.crt’ saved [1220/1220]

[root@server0 certs]# wget http://classroom/pub/tls/certs/server0.crt
… …
2016-11-27 01:04:06 (62.1 MB/s) - ‘server0.crt’ saved [3505/3505]

[root@server0 certs]# ls *.crt //确认部署结果
ca-bundle.crt example-ca.crt server0.crt
ca-bundle.trust.crt localhost.crt

[root@server0 certs]# cd /etc/pki/tls/private/
[root@server0 private]# wget http://classroom/pub/tls/private/server0.key
… …
2016-11-27 01:07:09 (39.0 MB/s) - ‘server0.key’ saved [916/916]
3)为SSL加密网站配置虚拟主机

[root@server0 ~]# vim /etc/httpd/conf.d/ssl.conf
Listen 443 https
… …

DocumentRoot “/var/www/html” //网页目录
ServerName server0.example.com:443 //站点的域名
… …
SSLCertificateFile /etc/pki/tls/certs/server0.crt //网站证书
… …
SSLCertificateKeyFile /etc/pki/tls/private/server0.key //网站私钥
… …
SSLCACertificateFile /etc/pki/tls/certs/example-ca.crt //根证书
4)重启系统服务httpd

[root@server0 ~]# systemctl restart httpd
[root@server0 ~]# netstat -antpu | grep httpd //确认已监听80、443端口
tcp6 0 0 :::443 ::😗 LISTEN 7954/httpd
tcp6 0 0 :::80 ::😗 LISTEN 7954/httpd
步骤二:验证HTTPS加密访问

使用firefox浏览器访问加密站点https://server0.example.com/,可以看到页面提示未信任连接“Untrusted Connection”(如图-2所示)。

在这里插入图片描述

图-2

若要继续访问,需要在页面下方单击超链接“I Understand the Risks”,表示用户已理解相关风险。然后在展开的页面内点击“Add Exception”按钮(如图-3所示)。

在这里插入图片描述

图-3

弹出添加安全例外对话窗口(如图-4所示),单击界面左下角的“Confirm Security Exception”按钮确认安全例外。

在这里插入图片描述

图-4

确认成功后即可看到对应的网页内容(如图-5所示)。

在这里插入图片描述

图-5
————————————————
版权声明:本文为CSDN博主「我爱DC」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36345864/article/details/104828333

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot的官方网站案例项目是一个示例性的应用程序,旨在展示Spring Boot的各种功能和特性。该项目提供了一个简单的网站,演示Spring Boot框架的用法和配置方式,并展示了如何构建和部署一个基于Spring Boot的应用程序。 该项目的主要特点包括: 1. 界面设计:该应用程序的界面简洁而直观,易于使用。页面布局和样式遵循最佳实践,展示了响应式设计的能力。 2. 登录和身份验证:用户可以通过注册帐户并登录来访问更多功能。该项目演示了如何使用Spring Security进行用户身份验证和权限控制。 3. 数据持久化:项目使用了关系型数据库(如MySQL)来存储用户信息和其他数据。Spring Data JPA用于实现对数据库的访问和操作,演示了如何使用Spring Boot和JPA进行数据库集成和数据持久化。 4. RESTful API:该项目还提供了一组RESTful API,用于获取和操作数据。这些API遵循最佳实践,并使用Spring MVC和Spring Data REST进行实现。 5. 部署和扩展性:该项目可以轻松地在不同的环境中进行部署和扩展。它使用了Spring Boot的自动配置和可插拔的特性,让开发人员能够根据需要调整和定制应用程序。 通过该案例项目,开发人员可以了解和学习如何使用Spring Boot构建现代化的Web应用程序。它提供了一个起点,使开发者能够快速上手并开始构建自己的应用程序。同时,官方网站案例项目也提供了一些最佳实践和示范代码,供开发人员参考和借鉴。 ### 回答2: Spring Boot官方网站提供了许多案例项目,以帮助用户更好地了解和使用Spring Boot。这些案例项目是用来展示Spring Boot的各种功能和特性,以及如何进行开发。以下是一些常见的官方案例项目: 1. "Getting Started":这个案例项目是一个简单的基础示例,用于介绍Spring Boot的核心概念和基本用法。它包含了一个简单的Web应用程序,展示了如何创建控制器、配置路由和返回Json数据。 2. "Building RESTful Web Services":这个案例项目展示了如何使用Spring Boot构建RESTful风格的Web服务。它演示了如何创建和配置控制器、处理GET和POST请求、访问数据库等。 3. "Accessing Data with JPA":这个案例项目演示了如何使用Spring Boot和JPA(Java Persistence API)访问数据库。它展示了如何定义实体类、创建仓库接口、执行常见的CRUD(创建、读取、更新和删除)操作等。 4. "Securing a Web Application":这个案例项目展示了如何使用Spring Boot实现Web应用程序的安全性。它演示了如何配置用户认证和授权、使用HTTPS、添加权限控制等。 5. "Messaging with RabbitMQ":这个案例项目演示了如何使用Spring Boot和RabbitMQ进行消息传递。它展示了如何创建消息生产者和消费者、定义消息队列、处理消息等。 这些案例项目为使用Spring Boot的开发者提供了一个学习和参考的资源,帮助他们更加深入地了解和掌握Spring Boot的各种功能和用法。通过仔细研究和实践这些案例项目,开发者可以更加轻松地开始使用Spring Boot来构建自己的应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值