首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试通过可选的x509客户端证书使用spring安全性

尝试通过可选的x509客户端证书使用spring安全性
EN

Stack Overflow用户
提问于 2017-03-26 17:32:23
回答 1查看 1.4K关注 0票数 0

我有一个简单的spring启动应用程序与SSL和x509客户端证书的spring安全。

我想只使用http而不使用客户端证书在dev-mode下运行它。以及使用https和x509证书的prod-mode。

我有一个简单的演示应用程序,运行安全的网站OK。https://coling01@bitbucket.org/coling01/ssldemo.git

使用证书访问安全站点工作正常a) curl --cacert server.pem --cert client1.p12:changeit https://localhost:8443/demo

我希望能够切换到不安全模式,并在没有证书的情况下在port80上使用b) http (我确实有8080端口在运行,它会响应,但在尝试检查未通过的客户端证书时遇到错误)

有什么想法吗?我希望有一个简单的开关来禁用client-auth,但是找不到它。

EN

回答 1

Stack Overflow用户

发布于 2017-03-27 20:26:16

按照Mkyong's example使用Spring Profiles。这样你就可以创建两个不同的WebSecurityConfigurerAdapter

第一个是dev配置文件:

代码语言:javascript
复制
@Profile("dev")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class DevSecurityConfiguration extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().permitAll();
  }
}

和第二个产品准备就绪并确保安全:

代码语言:javascript
复制
@Profile("prod")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ProductionSecurityConfiguration extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .x509()
        .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
        .userDetailsService(userDetailsService());
  }
}

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43027126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档