2017-09-15 98 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); 
    } 
}