Spring Acegi Securityインスタンス解析2
7483 ワード
1. MySqlで次のスクリプトを実行します.
2. 次のJarパッケージをプロジェクトにインポートします:acegi-security-0.8.3.jar、commons-logging-1.0.4.jar、javax.servlet.jar、mysql-connector-java-5.0.3-bin.jar、oro-2.0.8.JArとspring-1.2-RC 2.jar.そして工事のclasspathに参加します.
3. web.xmlプロファイルの構成は次のとおりです.
4. applicationContext-acegi-security.xmlの構成は次のとおりです.
5. 登録フォームは次のとおりです.
Drop TABLE IF EXISTS `test`.`student`;
Create TABLE `test`.`student` (
`name` varchar(40) NOT NULL,
`psw` varchar(10) NOT NULL,
`enabled` boolean
);
insert into student values("lanp","lanpiao",true);
Drop TABLE IF EXISTS `test`.`user_privileges`;
Create TABLE `test`.`user_privileges` (
`name` varchar(40) NOT NULL,
`privilege` varchar(40) NOT NULL
);
insert into user_privileges values("lanp","ROLE_PRESIDENT");
2. 次のJarパッケージをプロジェクトにインポートします:acegi-security-0.8.3.jar、commons-logging-1.0.4.jar、javax.servlet.jar、mysql-connector-java-5.0.3-bin.jar、oro-2.0.8.JArとspring-1.2-RC 2.jar.そして工事のclasspathに参加します.
3. web.xmlプロファイルの構成は次のとおりです.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myacegi</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-acegi-security.xml
</param-value>
</context-param>
<filter>
<filter-name>MyAcegiFilter</filter-name>
<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
<init-param>
<param-name>targetBean</param-name>
<param-value>filterChainProxy</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyAcegiFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. applicationContext-acegi-security.xmlの構成は次のとおりです.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="filterChainProxy" class="net.sf.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=httpSessionContextIntegrationFilter,authenticationProcessingFilter
</value>
</property>
</bean>
<bean id="httpSessionContextIntegrationFilter" class="net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter">
<property name="context"><value>net.sf.acegisecurity.context.security.SecureContextImpl</value></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>157891</value>
</property>
</bean>
<bean id="authenticationDao" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="usersByUsernameQuery">
<value>SELECT name, psw, enabled FROM student WHERE name=?</value>
</property>
<property name="authoritiesByUsernameQuery">
<value>SELECT name, privilege FROM user_privileges where name=?</value>
</property>
</bean>
<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="authenticationDao">
<ref bean="authenticationDao"/>
</property>
</bean>
<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
<property name="providers">
<list>
<ref bean="daoAuthenticationProvider"/>
</list>
</property>
</bean>
<bean id="authenticationEntryPoint" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
<property name="loginFormUrl">
<value>/login.jsp</value>
</property>
<property name="forceHttps"><value>true</value></property>
</bean>
<bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
<property name="filterProcessesUrl">
<value>/j_acegi_security_check</value>
</property>
<property name="authenticationFailureUrl">
<value>/login.jsp?failed=true</value>
</property>
<property name="defaultTargetUrl">
<value>/jsp/index.jsp</value>
</property>
<property name="authenticationManager">
<ref bean="authenticationManager"/>
</property>
</bean>
</beans>
5. 登録フォームは次のとおりです.
<body>
<form method="POST" action="j_acegi_security_check">
Name: <input width="100" type="text" name="j_username"><br>
Password: <input width="100" type="password" name="j_password"><br>
<input type="submit">
</form>
</body>