首页  

springboot应用安全防护     所属分类 springboot 浏览量 1582
SpringBoot于2014年首次发布
构建安全的SpringBoot应用

生产环境使用HTTPS
强制使用HTTPS,扩展WebSecurityConfigurerAdapter要求安全连接。

@Configuration
public class WebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requiresChannel().requiresSecure();
    }
}


使用Snyk检查依赖


升级到最新版本

启用CSRF保护   
跨站点请求伪造(Cross-Site Request Forgery )
Spring MVC的form标签或Thymeleaf @EnableWebSecurity,默认处于启用状态,CSRF令牌会作为隐藏字段自动添加 

如果使用Angular或React等JavaScript框架,需要配置CookieCsrfTokenRepository以便JavaScript可以读取cookie
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}

使用React,需要读取XSRF-TOKEN cookie 并将 X-XSRF-TOKEN 添加到 请求 header 中


使用内容安全策略防止XSS攻击
内容安全策略(CSP)
Content-Security-Policy
配置 启用 CSP header 

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers()
            .contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/");
    }
}


使用OpenID Connect进行身份验证
OAuth 2.0是行业标准的授权协议   但不是身份验证协议
OpenID Connect(OIDC)是一个OAuth 2.0扩展,提供用户信息,除了访问令牌之外,它还添加了ID令牌


使用密码哈希
Spring Security默认不允许使用明文密码
public interface PasswordEncoder {
    String encode(String rawPassword);
    boolean matches(String rawPassword, String encodedPassword);
}

安全地存储秘密
Spring Vault
使用注解从Spring Vault中提取密码
@Value("${password}")
String password;


代码安全审查

原文
https://developer.okta.com/blog/2018/07/30/10-ways-to-secure-spring-boot

上一篇     下一篇
netty读写流量统计实例

netty空闲连接检测

netty 服务端启动日志

netty ChannelFuture 实例

netty异常处理机制

Garbage Collection Roots