在当前基于XML的配置中,web.xml中具有以下配置,以禁用非SSL(HTTP)协议访问。
<security-constraint>
<web-resource-collection>
<web-resource-name>SecureConnection</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
我正在将这种基于XML的配置迁移到Java配置。如何在Spring WebApplicationInitializer中设置此配置。我发现@ams年前发布了same question,但没有完整/正确的答案。如何使用Java配置实现这一目标。
答案 0 :(得分:0)
您创建一个WebSecurityConfigurerAdapter
并配置HttpSecurity
您应该浏览HttpSecurity
的文档,以找出需要应用的正确配置。
基本示例:
@Configuration
@EnableWebSecurity
public class OpenIDLoginConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
http.authorizeRequests().antMatchers("/**").hasRole("USER").and().openidLogin()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("https://www.google.com/accounts/o8/id?id=lmkCn9xzPdsxVwG")
.password("password").roles("USER");
}
}
答案 1 :(得分:0)
我发现以下通过Spring HttpSecurity 类仅通过SSL(HTTPS)访问请求并禁用非SSL访问(HTTP)的解决方案。经过测试,工作正常。需要配置 WebSecurityConfigurerAdapter
的此覆盖的 configure()方法http.requiresChannel().anyRequest().requiresSecure();