分類
网站

使用laravel5自帶的用戶授權系統

我這個不是教程,只是記錄。目標是修改laravel5默認的用戶系統,使之能迅速使用。註冊時在原有信息基礎上添加手機號。至於搭建laravel看參見:在ubuntu上部署apache和laravel5

首先修改數據庫,添加phone列。由於我數據庫還是空的所以就先php artisan migrate:rollback,然後在database/migrations/2014_10_12_000000_create_users_table.php up函數中添加一行

$table->string('phone')->unique();

然後執行php artisan migrate這樣用戶數據庫就有phone了,而且phone還是唯一的。然後修改app/Http/Controllers/Auth/AuthController.php,主要是重載postRegister和postLogin兩個函數。原函數可從vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesAndRegistersUsers.php中複製過來,修改後是這樣的:

//app/Http/Controllers/Auth/AuthController.php
<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;

class AuthController extends Controller {



	use AuthenticatesAndRegistersUsers;
        //修改跳轉地址
        protected $redirectTo = '/';


	public function __construct(Guard $auth, Registrar $registrar)
	{
		$this->auth = $auth;
		$this->registrar = $registrar;

		$this->middleware('guest', ['except' => 'getLogout']);
	}

	public function postRegister(Request $request)
	{       
                //數據驗證
                $this->validate($request, ['phone'=>'required|numeric|unique:users',);
		$validator = $this->registrar->validator($request->all());

		if ($validator->fails())
		{
			$this->throwValidationException(
				$request, $validator
			);
		}

		$this->auth->login($this->registrar->create($request->all()));

		return redirect($this->redirectPath());
	}
        
        //這裏修改爲可以通過郵箱或手機登陸
        public function postLogin(Request $request)
	{
		
                $field = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
                $request->merge([$field => $request->input('login')]);
                $this->validate($request, [
			'login' => 'required', 'password' => 'required',
		]);

		$credentials = $request->only($field, 'password');

		if ($this->auth->attempt($credentials, $request->has('remember')))
		{
			return redirect()->intended($this->redirectPath());
		}

		return redirect($this->redirectPath())
					->withInput($request->only('email', 'remember'))
					->withErrors([
						'email' => $this->getFailedLoginMessage(),
					]);
	}
	protected function getFailedLoginMessage()
	{
		return '郵箱或手機與密碼不匹配。';
	}

}

然後修改下位於/home/zenggl/case1/resources/views/auth下的登陸和註冊的表單,登錄表單修改一下email輸入框的name爲login,註冊表單添加一個name爲phone的輸入框。

密碼重置功能laravel5中也是實現好的,只用配置一下郵件發送就好了,我以QQ企業郵箱爲例配置。首先在.env中填入郵件服務器信息,然後設置config中的mail就好了:

//.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.exmail.qq.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourPassword
//config/mail.php
//視郵箱服務器而定,QQ郵箱要用ssl加密,且發信人要和發信帳號一致
'from' => ['address' => '[email protected]', 'name' => null],
'encryption' => 'ssl',

修改提示語言爲簡體中文。首先修改config/app.index中的locale值爲zh-cmn-Hans,然後複製一份resources/lang/en並重命名爲zh-cmn-Hans,最後修改validation.php就可以了。

分類
软件

Sublime theme for Netbeans 8.0.2

ubuntu下Netbeans7安裝Sublime theme for Netbeans很順利,但是升級到Netbeans8.0.2後就無法導入,經過嘗試發現是enabledItems.info的問題。

解決辦法:先導出一份配置,工具》選項》導出》編輯器》確定。解壓出裏面的enabledItems.info,用它替換Sublime theme.zip包內的enabledItems.info。然後就可以順利導入了。

如果你還在使用Netbeans7那麼強烈建議你升級到8,衆多新特性很不錯!

本文更新於 2015/04/09。

分類
网站

在ubuntu上部署apache和laravel5

新建一個laravel項目:

#切換到目標目錄,如:
cd /var/www/
#創建項目l5
sudo composer create-project laravel/laravel l5
#改變文件所有者爲自己,方便編輯
sudo chown USER:USER -R l5
#這兩個文件夾要設置777,否則服務器5000錯誤
cd l5
sudo chmod 777 -R storage/ vendor/
#配置數據庫信息
nano .env
php artisan migrate

如果是新建的apache服務器,可能需要開啟mod_rewrite模塊,可在phpinfo()中搜索mod_rewrite查看模塊是否開啟成功。可以參考:ubuntu搭建web服务器。然後新建一個虛擬主機,我們直接在/etc/apache2/sites-available/000-default.conf中添加一個VirtualHost:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/l5/public"
    ServerName l5.local
    ServerAlias www.l5.local

    <Directory "/var/www/l5/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

修改host文件/etc/hosts,添加一行127.0.0.1對應l5.local

此時就能通過http://l5.local訪問laravel5項目,通過http://l5.local/home測試用戶註冊登陸等。

題外話:我這裏的代碼高亮用的WP SyntaxHighlighter Version 1.7.3,在顯示VirtualHost起始標籤的時候有錯誤,所以後來用了原始的pre標籤。不知道有什麼解決辦法。

本文更新於 2015/05/20。

分類
软件

DiskUsage

DiskUsageDiskUsage是一款免费且开源的分析磁盤用量的安卓軟件,可以迅速發現大文件及佔用磁盤最多的軟件,非常實用!之前手機提示內存超過75%,建議將資料轉移至儲存卡。我找了找,發現DCIM下有個隱藏的縮略圖文件夾,裏面放着從買手機後照過的所有圖片的縮略圖,有1個多G。然後通過DiskUsage發現google plus有七八百兆的緩存,firefox也有很大緩存,把他們一清理空間就回來了。

本文更新於 2018/02/23。

分類
Linux

在ubuntu14上配置denyhost

昨天ss忽然變慢,一看好多條非法登陸。CentOS下的記錄文件是/var/log/secure。

#root登陸失敗次數
##ubuntu
sudo grep "Failed password for root" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr | more
##centos
sudo grep "Failed password for root" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more
#無效用戶登陸失敗次數
sudo grep "Failed password for invalid user" /var/log/auth.log | awk '{print $13}' | sort | uniq -c | sort -nr | more

denyhosts可以防止上面這種攻擊。但是我在ubuntu14的軟件源裏沒找到(12裏有)。於是去搜索,找到http://denyhosts.sourceforge.net/這個主頁,按照說明安裝下來,竟然失敗。後來安裝了denyhost(注意:沒有s了,這是前一個的fork),很順利。下面記一下安裝過程:

#下載denyhost
cd /tmp/ && wget http://downloads.sourceforge.net/project/denyhost/denyhost-2.8/denyhosts-2.8.tar.gz
#解壓
tar xzf denyhosts*.tar.gz
cd DenyHosts*
#安裝
sudo python setup.py install
#添加到開機啓動
sudo cp /usr/local/bin/daemon-control-dist /etc/init.d/denyhosts
#修改配置文件
#將文件中"DENYHOSTS_BIN"的值爲"/usr/local/bin/denyhosts.py"
sudo nano /etc/init.d/denyhosts
#修改攔截配置文件
sudo nano /etc/denyhosts.conf
#這個文件內部註釋很清晰,按需求修改就好,不改也沒關係。
#啓動服務
sudo /etc/init.d/denyhosts start

上面命令參考自How To Install DenyHosts On Ubuntu 14.04 Server。當然,修改下ssh的默認的配置也是很好的。

sudo nano /etc/ssh/sshd_config
#Port 12345 #改爲一個5位端口号,不要超過60000
#PermitRootLogin no #禁止root用戶登陸
#重啓ssh服務
sudo /etc/init.d/ssh restart
#在/etc/sysconfig/iptables中添加下面一行,打開12345端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 12345 -j ACCEPT
#重啟iptables
service iptables restart

禁用密碼登陸ssh(使用證書)也是很好的選擇。參考:Centos6.4下使用Denyhosts

用iptables防止ssh暴力破解

#添加如下iptables規則即可防止普通ssh暴力破解
-A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name DEFAULT --rsource 
-A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --update --seconds 300 --hitcount 4 --name DEFAULT --rsource -j DROP

時間來到 2022 年,denyhost 已經不再更新,但是我們可以使用它的繼任者——fail2ban。

#安裝 fail2ban
yum -y install fail2ban
#增加配置文件
nano /etc/fail2ban/jail.d/jail.local
#文件內容如下:
[sshd]
enabled  = true
filter   = sshd
action   = iptables[name=ssh,port=22,protocol=tcp]
logpath  = /var/log/secure
bantime  = 600
maxretry = 3
#啓動服務
systemctl restart fail2ban
#添加到開機啓動
systemctl enable fail2ban
#查看服務狀態
fail2ban-client status sshd

如果系統是 AlamLinux 可能需要啟動 EPEL 軟件源:

sudo dnf install epel-release
/usr/bin/crb enable

本文更新於 2024/08/06。

分類
Linux 软件

nano文本編輯器

nano是一個方便易用的命令行文本編輯器,相對於vi等文本編輯器勝在學習成本低廉而且linux平臺基本自帶(甚至android的busybox都有)。用nano常用的命令就是Ctrl+O寫入文件,Ctrl+X關閉文件,Ctrl+W查找文字。其他功能也很容易找到幫助,很適合不經常用linux的用戶(vi不常用的話,命令真的忘的好快)。

nano的複製粘貼也是很方便的。常用的就是CRTL+6用來選擇,ALT+6用來複製,CTRL+U用來粘貼。複製當前行的話直接ALT+6即可。

如果想把nano設置為默認的文本編輯器,比如使用crontab -e時,可以在~/.bashrc最後添加

export EDITOR=nano
export VISUAL=nano

如果要在多個文件間複製粘貼,可以用nano -F file1以多文件模式打開。打開之後,複製完按CTRL+R開啟另一個文件,就可以粘貼進去了。文件間的切換用ALT+,和ALT+.。

本文更新於 2017/03/22。

分類
Linux

Host a Debian in Win7 VirtualBox

正常的安裝就是在VirtualBox裡添加虛擬機,掛載下載好的Debian鏡像,即可安裝完成,最後安裝VirtualBox的附加組件,一路應該很順利。實際感覺是Debian比Ubuntu要稍微麻煩些。

apt的設置

如果你在安裝系統的時候就設置好了且工作正常,則不用設置。設置軟件源

nano /etc/apt/sources.list
我的是
deb http://ftp.debian.org/debian stable main contrib non-free
deb-src http://ftp.debian.org/debian stable main contrib non-free

給apt設置代理

nano /etc/apt/apt.conf.d/70debconf
#ubuntu下是/etc/apt/apt.conf
我的是在底部添加
Acquire::http::proxy "http://192.168.1.2:8123/";
Acquire::ftp::proxy "http://192.168.1.2:8123/";
這是的代理是Win7里運行的Shadowsocks。

安裝VirtualBox附加組件

轉自Installing Guest Additions on Debian。安裝完成後就可以共享剪切板和自由縮放屏幕大小了。

  1. Login as root;
  2. Update your APT database with apt-get update;
  3. Install the latest security updates with apt-get upgrade;
  4. Install required packages with apt-get install build-essential module-assistant;
  5. Configure your system for building kernel modules by running m-a prepare;
  6. Click on Install Guest Additions… from the Devices menu of VirtualBox, then run mount /media/cdrom.
  7. Run sh /media/cdrom/VBoxLinuxAdditions.run, and follow the instructions on screen.

添加中文支持

dpkg-reconfigure locales
空格選擇,回車確認。我選擇了這幾個
  • zh_TW BIG5 - 繁體中文(台灣),使用 Big5 碼
  • zh_TW.UTF-8 UTF-8 - 繁體中文(台灣),使用 UTF-8 碼
  • zh_HK.UTF-8 UTF-8 - 繁體中文(香港),使用 UTF-8 碼
  • zh_CN GB2312 - 簡體中文,使用 GB2312-80
  • zh_CN.GBK GBK - 簡體中文,使用 GBK
  • zh_CN.UTF-8 UTF-8 - 簡體中文,使用 GB18030
然後首選項選擇了zh_CN.UTF-8。

安裝字體:

apt-get install ttf-arphic-uming  xfonts-intl-chinese  xfonts-wqy

安裝輸入法:

apt-get install fcitx-sunpinyin
apt-get install im-switch

重啟就好了。


Debian使用systemd開機啓動

我裝的是Debian8,似乎已經不再支持rc.local了。以ss爲例,開機啓動可以這麼添加:首先新建開機服務文件

nano /lib/systemd/system/ss.service
內容爲
[Unit]
Description=ss

[Service]
Type=simple
ExecStart=/usr/local/bin/ss-local -c /etc/shadowcksB.json > /home/$YOURNAME/.sslog 2$
PrivateTmp=true

[Install]
WantedBy=multi-user.target
ss用到的文件/home/$YOURNAME/.sslog需要自己新建touch下。

將服務設置爲開機啓動:

systemctl enable ss.service

測試服務是否正常:

systemctl start ss.service

查看當前服務狀態:

systemctl status ss.service -l

順便再貼一個polipo的開機啓動文件

[Unit]
Description=polipo
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/polipo start
ExecReload=/etc/init.d/polipo restart
ExecStop=/etc/init.d/polipo stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

安裝flashplayer

Debian自帶的瀏覽器和火狐都沒有Flash,可以按照如下步驟安裝:
1, Lauch the browser and download the Flash Player in a seperate folder.Download link = http://get.adobe.com/flashplayer/
2, tar xzvf yourdownloadedfilename.tar.gz
3, cp libflashplayer.so /usr/lib/mozilla/plugins/

本文更新於 2016/05/19。