当前位置: 首页 > 知识库问答 >
问题:

如何加快SpringBoot2.0基本身份验证?

祁霖
2023-03-14

我最近将一个Spring Boot应用程序从V1.5更新到V2.0.3。它是一个具有作为RESTendpoint公开的方法的应用程序,并通过基本的HTTP身份验证加以保护。用户名和密码硬编码在应用程序加载的属性文件中。

自更新以来,响应时间增加了近200ms,处理请求的98%时间花费在BasicAuthenticationFilter.doFilter()中。

更具体地说,需要花费时间对请求中的密码进行编码,以便将其与配置提供的密码进行比较。

@EnableWebSecurity
@PropertySource("classpath:auth/auth.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

     @Value("${user.name}")
     private String userName;
     @Value("${user.password}")
     private String userPassword;
     @Value("${user.roles}")
     private String[] userRoles;

     @Override
     protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        UserDetails user = User.withUsername(userName).password(encoder.encode(userPassword)).roles(userRoles).build();
        auth.inMemoryAuthentication().withUser(user);
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        //make sure that the basic authentication header is always required
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        //set the correct authentication entry point to make sure the 401 response is in JSON format
        http.exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint());

        //enable http basic authentication
        http.httpBasic();

        //make sure authentication is required for all requests except /actuator/info
        http.authorizeRequests().antMatchers("/actuator/info").permitAll().anyRequest().authenticated();
        http.authorizeRequests().antMatchers("/actuator/**").hasAnyRole("MONITOR", "ACTUATOR");

        //disable the form login since this is a REST API
        http.formLogin().disable();

        //disable csrf since this is a REST API
        http.csrf().disable();
    }
}
  • 禁用oAuth(和cors)登录,因为它似乎也很慢https://github.com/spring-projects/spring-security-oauth/issues/943
  • 提供一个UserDetailsService bean,而不是配置AuthenticationManagerBuilder https://github.com/spring-projects/spring-boot/wiki/spring-boot-security-2.0#changing-the-username-and-password

我能做些什么来加快身份验证筛选器吗?

共有1个答案

蒙麒
2023-03-14

bcrypt是一个故意缓慢的散列函数。当谈到密码散列时,这种缓慢听起来很矛盾,但这并不是因为好人和坏人都慢了下来。如今,大多数密码攻击都是暴力字典攻击的变体。这意味着攻击者会像好人一样通过散列来尝试很多很多候选密码。如果有匹配项,则密码已被破解。

然而,如果坏人每次尝试都被放慢速度,当数百万次尝试时,这种速度就会放大,通常会挫败攻击。然而,当他们试图登录时,好人可能不会注意到一次尝试。

因此,brypt编码器中额外的200ms处理时间基本上是有意的,也是安全性的一部分,通过加速它(比如使用缓存),您降低了应用程序的安全级别。

 类似资料:
  • 问题内容: 从HttpClient 4.3开始,我一直在使用HttpClientBuilder。我正在连接到具有基本身份验证的REST服务。我将凭据设置如下: 但是,这不起作用(我正在使用的REST服务返回401)。怎么了? 问题答案: 从此处的 抢先身份验证 文档中: http://hc.apache.org/httpcomponents-client- ga/tutorial/html/aut

  • 我们有一个在Apache和nginx上运行的Angular 8应用程序。我们使用带有和的Basic Auth保护此应用程序。 不幸的是,由于对服务器的请求被基本身份验证阻止,应用程序无法加载,Angular无法加载这些文件: 此 路 不通http://root/polyfills-es2015.js因为这是一个跨来源请求,所以不要求提供凭据 已阻止http://root/runtime-es201

  • 目前我正在开发一个Java工具,它应该可以更新Confluence服务器页面。使用Curl一切都像一个符咒,但是当使用Postman或Java代码(HttpClient Java11)时,我得到了一个 HTTP状态401–未经授权 反应。 在下面的语句中使用curl curl--basic-u user:password-X PUT-H“内容类型:application/json”-d“@test

  • 几天没有任何进展,我需要你的帮助。 使用GWT,我试图与REST服务器通信,服务器位于不同的URL上(需要CORS)<我的配置:服务器spring boot 1.3.3 客户端-GWT 1.7-restygwt 2.0.3 当我在Spring中禁用安全性时,我可以在我的GWT客户端中获取数据。 但是当我启用它时,我总是收到401个请求。REST URL请求直接在Web浏览器中工作(带有其身份验证对

  • 我正在尝试使用OAuth2实现开发一个带有Spring Security性的rest api。但是如何删除基本身份验证呢。我只想向body发送用户名和密码,并在postman上获取令牌。 要删除基本身份验证,并从邮递员的get token中发送body标记中的用户名密码吗 我遇到了一些问题{"错误":"未经授权","error_description":"没有客户端身份验证。尝试添加适当的身份验证

  • 我尝试了angular.js,并从一个restful api的web-ui开始(使用超文本传输协议基本授权)。这真的很好,除了授权工作。 到目前为止,我使用的是设置密码,但大多数情况下浏览器会打开http basic auth的默认登录表单。另一个奇怪的行为是angular请求不包含授权头(选项和GET请求都不包含)。我还尝试使用header配置在每个请求上设置此header,但这也不起作用。 有