ここで説明するように、既存のサーブレット 2.4アプリケーションを XML ベースのセキュリティ構成から新しい Java 構成ベースのスタイルに変換するためのサンプルとして使用しているサンプル Web アプリがあります。Spring Security Java Config プレビュー
私のセキュリティ構成は次のとおりです。
@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public WebSecurityConfig() {
log.info("init");
}
@Override
protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeUrls()
.antMatchers("/","/home").permitAll()
.antMatchers("/secure/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.loginUrl("/login")
.permitAll();
}
}
問題は、ドキュメント/サンプルが web.xml を使用しないサーブレット 3.0 仕様に基づいていることです。
ドキュメントでは AbstractAnnotationConfigDispatcherServletInitializer を使用していますが、サーブレット 2.4 コンテナーでは使用できません。
次のようにweb.xmlを使用して MVC アプリをブートストラップします。
<servlet>
<servlet-name>my-spike</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.example.config.WebMvcConfig</param-value>
</init-param>
</servlet>
問題は、WebSecurityConfig を web.xml または Java コードのルート アプリケーション コンテキストに追加する方法です。