2017-09-15 96 views
1

我升級現有的應用程序使用Spring引導並從web.xml中移開。 雖然我已經實現的功能,有這對我的生活我無法弄清楚如何升級到春季啓動我的web.xml的一部分。 (如果它的事項我使用1.5.6)從web.xml中去春引導

我的web.xml的

相關部分:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>basic</web-resource-name> 
     <url-pattern>*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>User</role-name> 
    </auth-constraint> 
</security-constraint> 

<login-config> 
    <auth-method>BASIC</auth-method> 
    <realm-name>myrealm</realm-name> 
</login-config> 

這裏是我的應用程序類:

@SpringBootApplication 
public class Application extends SpringBootServletInitializer{ 

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

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

我一直沒能確定如何設置任何東西,以反映安全約束或使用代碼爲基礎的方法登錄,配置的功能。

如果任何人有如何或可以點我在正確的方向上沒有任何想法,我會非常讚賞。幾個小時的谷歌搜索和嘗試解決方案讓我一無所有。

+0

也許找一個教程 「春天啓動安全教程」?有了這個搜索詞,你可以找到很多。 – Heri

回答

0

查找到@EnableWebSecurityWebSecurityConfigurerAdapter

見下面的例子:

@EnableWebSecurity 
@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/**").authenticated() 
      .anyRequest() 
      .permitAll() 
      .and().httpBasic() 
      .and().sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
    } 
}