2016-09-13 7 views
2

私はspring-security-jsp-authorizeの例を開発しました。この例では、バージョンプログラムがSpringBootServletInitializerに対して非推奨エラーを出さないようになるまで、私はspring-boot-starter-parentバージョン1.3.7.RELEASEを使用していました。春のimport org.springframework.boot.context.web.SpringBootServletInitializerの置き換えは1.4.0.RELEASEboot-starter-parentですか?1.4.0.RELEASEで廃止されたSpringBootServletInitializerの代わりに使用するもの

Application.java

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

enter image description here

のpom.xml

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

SecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(final AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
     .withUser("john").password("123").roles("USER") 
     .and() 
     .withUser("tom").password("111").roles("ADMIN"); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(final HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
     .antMatchers("/login").permitAll() 
     .antMatchers("/admin").hasRole("ADMIN") 
     .anyRequest().authenticated() 
     .and().formLogin().permitAll(); 
    } 
} 

それは>org.springframework.boot.context.web春ブーツ1.4用のドキュメントに記載されて

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    public MvcConfig() { 
     super(); 
    } 

    @Override 
    public void addViewControllers(final ViewControllerRegistry registry) { 
     super.addViewControllers(registry); 
     registry.addViewController("/").setViewName("forward:/index"); 
     registry.addViewController("/index"); 
    } 
} 

答えて

6

MvcConfig.java:

推奨されていません。 org.springframework.boot.web.support.SpringBootServletInitializer

の賛成で1.4のようにちょうどこの新しいパッケージに廃止予定のインポートを変更します。

+0

「スプリングブート」を使用しないとどうなりますか? – Daria

0

WebMvcConfigurerインターフェイスを使用できます。

public class Configuration implements WebMvcConfigurer { 

    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
     converters.add(mappingJackson2HttpMessageConverter()); 
    } 


    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurerconfigurer) { 
     configurer.enable(); 
    } 
} 
関連する問題