概要
記事最下部に追記があります。(2014/07/21)
みんな大好きSpringSecurityCore!
簡単にログイン機能を追加できる便利なプラグインですが、最新のGrails2.4では利用することができません。。。
手前味噌ですが、以前Grails2.2でSpringSecurityCoreの導入方法を書きました。
ログイン機能を実装(Spring Security Plugin)
なお、Grails2.3でもちゃんと動作します。
でも、Grails2.4とSpringSecurityCore2.0-RC3でこの手順通りに進んで、いざrun-appしようとすると以下のようなエラーが出力されます。
H2(デフォルトのDB)の場合
| Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Error 2014-06-26 14:13:44,834 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table secure_user_secure_role drop constraint FK_hqd1oog246jv37esgfp75jmb0 if exists
| Error 2014-06-26 14:13:44,837 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - テーブル "SECURE_USER_SECURE_ROLE" が見つかりません
Table "SECURE_USER_SECURE_ROLE" not found; SQL statement:
alter table secure_user_secure_role drop constraint FK_hqd1oog246jv37esgfp75jmb0 if exists [42102-176]
| Error 2014-06-26 14:13:44,837 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table secure_user_secure_role drop constraint FK_r2mpjh0isokkncviqho11l7xj if exists
| Error 2014-06-26 14:13:44,837 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - テーブル "SECURE_USER_SECURE_ROLE" が見つかりません
Table "SECURE_USER_SECURE_ROLE" not found; SQL statement:
alter table secure_user_secure_role drop constraint FK_r2mpjh0isokkncviqho11l7xj if exists [42102-176]
| Server running. Browse to http://localhost:8080/hogehoge
| Application loaded in interactive mode. Type 'stop-app' to shutdown.
| Enter a script name to run. Use TAB for completion:
grails> stop-app
PostgreSQLの場合
| Running Grails application
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Error 2014-06-26 14:22:05,441 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table secure_user_secure_role drop constraint if exists FK_hqd1oog246jv37esgfp75jmb0
| Error 2014-06-26 14:22:05,443 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: relation "secure_user_secure_role" does not exist
| Error 2014-06-26 14:22:05,444 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table secure_user_secure_role drop constraint if exists FK_r2mpjh0isokkncviqho11l7xj
| Error 2014-06-26 14:22:05,444 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: relation "secure_user_secure_role" does not exist
| Error 2014-06-26 14:22:05,445 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table secure_role cascade
| Error 2014-06-26 14:22:05,445 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: table "secure_role" does not exist
| Error 2014-06-26 14:22:05,449 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table secure_user cascade
| Error 2014-06-26 14:22:05,449 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: table "secure_user" does not exist
| Error 2014-06-26 14:22:05,449 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table secure_user_secure_role cascade
| Error 2014-06-26 14:22:05,449 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: table "secure_user_secure_role" does not exist
| Error 2014-06-26 14:22:05,450 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop sequence hibernate_sequence
| Error 2014-06-26 14:22:05,450 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - ERROR: sequence "hibernate_sequence" does not exist
| Server running. Browse to http://localhost:8080/hogehoge
メッセージが日本語か英語かどうかくらいで内容に変わりはなさそう?
原因
ソースを追っていないのでなんとも言えませんが、単純にSpringSecurityCoreがまだGrails2.4に対応していないためだと思われます。
Hibernate4とAsset-Pipelineにまだ対応していないようです。
Grails2.4から、Hibernate4と、Resourceプラグインの代わりにAsset-Pipelineプラグインがデフォルトになりました。
Asset-PipelineについてはAsset-Pipelineプラグインのメモをどうぞ。
Hibernate4とAsset-Pipelneの対応がされるまで、以下のような暫定対応が必要になります。
暫定対応
- Hibernate3を利用する
- Asset-Pipelineプラグインの代わりに旧来のResourceプラグインを利用する。
の2点でとりあえずはGrails2.4でSpringSecurityCore2を利用することができます。
Hibernate3を利用する
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
//cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
}
//runtime ":hibernate4:4.3.5.4" // or ":hibernate:3.6.10.16"
runtime ":hibernate:3.6.10.16"
Assert-Pipelineの代わりにResourceプラグインを利用する
//compile ":asset-pipeline:1.8.11"
runtime ":resources:1.2.8"
これでGrailsを起動してアクセスすると500エラーが出る。
というのも、layout/main.gspでAsset-Pipelineのtaglibを利用しているため。
とりあえずそれらをコメントアウトする。
Asset-Pipelineのtaglibの利用箇所をコメントアウトする
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"><!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><g:layoutTitle default="Grails"/></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
%{--<link rel="shortcut icon" href="${assetPath(src: 'favicon.ico')}" type="image/x-icon">--}%
%{--<link rel="apple-touch-icon" href="${assetPath(src: 'apple-touch-icon.png')}">--}%
%{--<link rel="apple-touch-icon" sizes="114x114" href="${assetPath(src: 'apple-touch-icon-retina.png')}">--}%
%{--<asset:stylesheet src="application.css"/>--}%
%{--<asset:javascript src="application.js"/>--}%
<g:layoutHead/>
</head>
<body>
%{--<div id="grailsLogo" role="banner"><a href="http://grails.org"><asset:image src="grails_logo.png" alt="Grails"/></a></div>--}%
<g:layoutBody/>
<div class="footer" role="contentinfo"></div>
<div id="spinner" class="spinner" style="display:none;"><g:message code="spinner.alt" default="Loading…"/></div>
</body>
</html>
これでOK。
ゼロからアプリケーションを作成する場合は、どうせ自分でmain.gspをイジる(or 利用しない)ことになるので、特に問題にはならないはず。
SpringSecurityCoreがAsset-Pipelineを意識していないっぽい件に関しては、そのうち修正されると思うけど、RC2からRC3までかなり時間がかかっていたのでまだまだ時間がかかるかも。
ということで今回の修手順で暫定対応できます。
追記(2014/07/21)
SpringSecurityCore2.0-RC4が7月8日にリリースされています。
このバージョンを使うと、まだHibernate3を利用する必要はありますが、AssetPipelineに対応されたっぽいので、この記事のAssetPipelineに関する記述は無視してください。