LoginSignup
4
1

More than 5 years have passed since last update.

春の目覚め作戦 SpringMVC その2

Last updated at Posted at 2018-05-20

Spring-Scurityを取り扱う

その1にspring-securityとやらを追加しました。

忘れないようにφ(・

何がなんだか状態ですが、世のサンプルを切り貼りしてなんとか動くようにしやした。
ここがキモみたいです。

    <http auto-config="true" >
        <!-- 認可の設定 -->
        <intercept-url pattern="/top*" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER')" />
        <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/list*" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/user*" access="hasRole('ROLE_USER')" />

        <!-- 権限なし時の遷移先 -->
        <access-denied-handler error-page="/403" />

        <!-- 認証のログイン処理 -->
        <form-login
            login-page="/"
            default-target-url="/top"
            authentication-failure-url="/error"
            login-processing-url="/j_spring_security_check"/>

        <!-- 認証のログアウト処理 -->
        <logout
            logout-url="/logout"
            logout-success-url="/"
            invalidate-session="true"/>
        <!-- anonymousユーザのROLE -->
        <anonymous granted-authority="ROLE_ANONYMOUS" />
    </http>

参考にさせて頂いたサンプルだと「security-context.xml」でID/パスワード認証してるんですが、その際にWebアプリとDBとで接続をしています。
まぁ、これは当然なんですが、ログイン後に再びWebアプリとDBとで接続する設定処理を「servlet-context.xml」に記載するとエラーがでました。内容は「さっきWebアプリとDB間で接続作ったから2つ目は作れないよぉ~(# ゚Д゚)ノ」って意味のようでした。
しかたないんで「applicationContext.xml」に共通で使うDB設定を作ったらすんなり動きました。

参考までDBはこんな感じで作りました。

CREATE TABLE `todo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(50) DEFAULT NULL,
  `done` tinyint(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

CREATE TABLE `users` (
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `authority` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

ソースはこちらです。
https://github.com/pugachev/TodoApp.git

一応動作確認用のサイトです。
http://ikefukurou.com/TodoApp/

4
1
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
4
1