2014年10月14日火曜日

Tomcatでベーシック認証

tomcat-users.xml

<tomcat-users>
    <role rolename="aaaa" />
    <user username="bbb" password="ccc" roles="aaaa" />
</tomcat-users>


web.xml

<web-app>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>
                Authentication of BasicAuth
            </web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>basic</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>ProtectedRealm</realm-name>
    </login-config>
</web-app>

参考リンク
http://www.javaroad.jp/opensource/js_tomcat9.htm
http://www.coderanch.com/t/465990/java-Web-Component-SCWCD/certification/HTTP-Status-Access-requested-resource

2014年10月8日水曜日

JavaMailでsmtp-authでメールを送信する方法

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");  <----25か587のサブミッションポートでもok
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true")

    Authenticator auth = new SMTPAuthenticator();
    Session mailSession = Session.getDefaultInstance(props, auth);

    // パスワード認証のプライベートクラス
    private class SMTPAuthenticator extends javax.mail.Authenticator {  
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }


http://www.rgagnon.com/javadetails/java-0538.html

2014年10月4日土曜日

Postfixで587のサブミッションポートでsmtp-auth設定をした上で、25ポートでのsmtp-authをさせないように設定するには。

Postfixで587のサブミッションポートでsmtp-auth設定をした上で、25ポートでのsmtp-authをさせないように設定するには、/etc/postfix/master.cfを以下のように編集。

smtp      inet  n       -       -       -       -       smtpd

  -o smtpd_sasl_auth_enable=no  <---------- ここをno

submission inet n       -       -       -       -       smtpd
  -o smtpd_sasl_auth_enable=yes       <---------- ここをyes

参考リンク:

2014年10月3日金曜日

MySQLでデータのダンプとリストアをCSVファイルでやりとりする方法

MySQLでデータのダンプとリストアをCSVファイルでやりとりする方法を簡単にメモ。

ダンプ

SELECT * FROM tableA INTO OUTFILE "/tmp/dump.csv" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';

リストア

LOAD DATA INFILE "/tmp/dump.csv" INTO TABLE tableA FIELDS TERMINATED BY ',' ENCLOSED BY '"';

http://sasuke.main.jp/sqlcsv.html

注意した点として、Ubuntuなどではデフォルトで/tmp/ 以下からでしかCSVファイルを扱えなかったので、ダンプ・リストア時のエラーに注意。確かerrorno 13だった気が。。。

Javaのサーブレットでブラウザの戻るボタンを防ぐ方法

Javaのサーブレットでブラウザの戻るボタンを防ぐには、フィルターでレスポンスを制御する。

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, 
    FilterChain chain) throws IOException, ServletException 
    {
        HttpServletResponse response=(HttpServletResponse)res;
        response.setHeader("Cache-Control", "no-cache, no-store, "
        + "must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", -1);
        chain.doFilter(req, res);

    }


Postfixでsaslauthdにハマった。。。のでメモ。

UbuntuのPostfixでsmtp-auth認証を設定する時に一番ハマったのがsaslauthdの設定。結論からすると設定ファイルのミスという単純なものだったのだが、結構ハマったのでメモ。

/etc/default/saslauthdの中身

# Should saslauthd run automatically on startup? (default: no)

START=yes <-------ここをyesにするのだが、大文字のYESにしてしまうとsaslauthdは起動しない。

起動時のエラーメッセージで気付くべきだったのだが、、、、ハマってしまった。

チェックポイント:
/var/log/mail.log
/usr/sbin/testsaslauthd -u username -p password



参考リンク
https://forums.ubuntulinux.jp/viewtopic.php?id=6750



Postfixのsmtp-auth設定でRelay Access Denied 554エラーが出る

UbuntuでPostfixのsmtp-auth設定でRelay Access Denied 554エラーが出た場合の対処法。

/etc/postfix/main.cf


smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

ポイントはpermit_sasl_authenticatedをreject_unauth_destinationの前に追加してやること。これがないとsmtp-authでのリレーアクセスができない。

参考リンク
http://askubuntu.com/questions/390401/relay-access-denied-error-554

telnetでsmtp-authの動作確認方法

telnetでsmtp-authの動作確認をしたかったのでメモ。クライアントサイドの入力のみでサーバー側の応答は省く。

% telnet mail.test.com 25

% helo mail.test.com

% auth login      <----------- SMTP-AUTHをすることを伝える。

% xxxxxxxx      <----------- base64エンコードされたユーザアカウント。

% xxxxxxxx      <----------- base64エンコードされたパスワード。

% mail from: <sender@abc.com>

% rcpt to: <reciver@efg.com>

% data
% Subject: abcded
% abcdefg
%
% This is a test message....
% .     <---------- ピリオドで終える。

% quit


ハマったとこはbase64エンコードされたID/PASSの設定。どうもパスワードに特殊記号が含まれているとbase64が失敗しているのか、認証に失敗してしまっていた。ここは精製方法を要調査。

参考リンク
http://www.anta.net/misc/telnet-troubleshooting/smtp.shtml
https://www.ndchost.com//wiki/doku.php?id=mail/test-smtp-auth-telnet
http://vogel.at.webry.info/201308/article_1.html