SpringFrameworkでFilterを使用したいのですが設定がわからず困っています。
お手数ですが設定方法をご教示いただければと思います。
【やりたいこと】
・自作したFilter(authenticateFilter)の設定
・MyBatisのMapperを@Autowiredする
【試したこと】
・以下のDBやMyBatisの設定をroot-context.xmlを読み込めば Filter でもDB接続は可能でしたが、通常のサービスでロールバックができなくなってしまいました。
datasource-context.xml
mybatis-context.xml
【web.xml】
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- filter -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>authenticateFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>authenticateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
【root-context.xml】
<!-- filter -->
<bean id="authenticateFilter" class="com.hatak.filter.AuthenticateFilter"></bean>
【servlet-context.xml】
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<import resource="./datasource-context.xml" />
<import resource="./mybatis-context.xml" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.hatak.mamafriends" />
<context:component-scan base-package="com.hatak.filter">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 例外のハンドラクラス -->
<bean class="com.hatak.handler.GlobalExceptionResolver"/>
【AuthenticateFilter.java】
package com.hatak.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.hatak.mamafriends.constants.AppConstants;
import com.hatak.mamafriends.params.authenticate.AuthenticateParams;
import com.hatak.mamafriends.service.AuthenticateService;
/**
* 認証フィルタクラス
*/
@Controller
public class AuthenticateFilter implements Filter {
/**
* 認証サービス
*/
@Autowired
private AuthenticateService service;
public AuthenticateService getService() {
return service;
}
public void setService(AuthenticateService service) {
this.service = service;
}
/**
* フィルタ処理
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
AuthenticateParams params = new AuthenticateParams();
// TODO 認証情報の設定
boolean res = service.authenticate(params);
System.out.println(res);
chain.doFilter(request, response);
}
/**
* 終了処理
*/
@Override
public void destroy() {
}
}