LoginSignup
0
0

More than 5 years have passed since last update.

Spring Security デフォルトログイン画面の出力(Hello World!)

Last updated at Posted at 2018-05-24

Spring Securityで学んだことをここにログしておきます。

Spring Securityとは

アプリケーションに認証・認可機能を提供します。

ライブラリ

groupId:org.springframework.security
artifactId:spring-security-web

groupId:org.springframework.security
artifactId:spring-security-config

groupId:org.springframework.security
artifactId:spring-security-taglibs

versionはすべて4.2.2.RELEASEでやりました。

Bean定義

今回は、xml形式での設定をしました。

security-config.xml
xmlns:sec="http://www.springframework.org/schema/security"

<sec:http>
    <sec:intercept-url pattern="/**" access="isAuthenticated()"/>
    <sec:form-login />
</sec:http>

<sec:authentication-manager />

sec:httpで、すべてのパスに対して認証を必要とする設定をしました。
sec:authentication-managerで、認証機能用のコンポーネントを定義します。

web.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/META-INF/spring/security-config.xml</param-value>
</context-param>

これでcontextConfigLocationにsecurity-config.xmlを指定しました。

サーブレットフィルタの設定

サーブレットフィルタクラスをサーブレットコンテナに登録します。

web.xml
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

DelegatingFilterProxyをすでにDIコンテナで管理されているBeanをサーブレットコンテナに登録する。security-config.xmlに[sec:http]の記述をするとSpring Securityを利用するために必要となるBean定義が自動的に行われる。

ここまでの設定がうまくいっていれば、サーバーを起動するとデフォルトのログイン画面が表示されます。

感想

Spring Securityのデフォルト画面を出力するだけでかなり苦戦しました。
奥が深そうです、、。

0
0
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
0
0