LoginSignup
8
5

More than 5 years have passed since last update.

Railsで簡単にGDPR対応 (EU圏のIPを判定する)

Posted at

背景

迫るGDPR対応。EU圏に住んでいる知人からも「最近色々なサービスからGDPR関連の長文メールがよく届く」みたいな話をききます。各社対応がギリギリすぎるのでは・・・笑。日本の企業もサービスの業種によっては対応しないといけないため、そのメモ。

基本方針

request.remote_ip からEU圏かどうか判定して、処理を切り替えられるようにする

利用ライブラリ

  • maxmind社がIPの地理情報を提供しているので、元データとしてはそちらを利用する。
  • IP判定のgemを検索すると geoip が出て来るが、更新もあまりされておらず、2019年1月に失効するmaxmindの旧バージョンのGeoデータしか読み込めないようなので非推奨
  • maxmind社公式サイトにてオススメしている maxminddb を利用
gem 'maxminddb'

実装

  1. http://dev.maxmind.com/geoip/geoip2/geolite2/ から GeoLite2-Country.mmdb をダウンロードして、任意のディレクトリに配置
  2. config/initializers/maxminddb.rb を作る
# Download latest free geo city database from here:
# http://dev.maxmind.com/geoip/geoip2/geolite2/
Maxminddb = MaxMindDB.new(Rails.root.join('./db/GeoLite2-Country.mmdb'))

あとはこんな感じでcontroller等に実装するだけでOK

#
# EU圏からのアクセス
#
def request_from_eu?
  request.remote_ip.present? && Maxminddb.lookup(request.remote_ip).continent.code == 'EU'
end

その他

  • Country DBを利用したが、City DBを利用すると都市単位まで分かるようになる。(ただしファイル容量は重い)
  • Country DBの精度は98%くらいあるとのこと
  • maxminddbの元データは毎月更新されるので、必要あれば自動更新を組むこと
8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5