- 追加された行はこの色です。
- 削除された行はこの色です。
*Java:SpringSecurityの事始め [#u7e1aa5b]
SpringSecurityの事始めです。
SpringMVCを知っている前提です。
***デフォルトの認証画面が出るまで [#t9f032cf]
以下の3つのファイルを編集、作成します。
-pom.xml
-applicationCntext.xml
-applicationScurity.xml(新規作成)
-web.xml
それぞれのファイルの編集内容を見ていきます。
まず、pom.xmlにSpringSecurityの依存関係を追加します。
-pom.xml
<!-- Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
次に、applicationScurity.xmlを作成し、以下の内容を追記します。
<?xml version="1.0" encoding="UTF-8"?>
-applicationScurity.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="test" password="test" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
次に、web.xmlのContext.xmlの設定の読み込み設定にapplicationSecurity.xmlを追加します。
-web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml classpath:applicationSecurity.xml
</param-value>
</context-param>
また、同じくweb.xmlにSpringSecurityのfilter設定等を追加します。
-web.xml
<!-- SpringSecurityのフィルター設定 -->
<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>
これで、SpringSecurityの標準のログイン画面が出ます。