相关疑难解决方法(0)

为什么 Spring boot Security 基本身份验证很慢?

我有一个 Spring boot 2.0.1 服务,我向其中添加了使用 BCrypt 进行哈希处理的基本身份验证。但这项服务在添加基本身份验证之前平均需要 400 毫秒,现在需要 1 秒以上。我正在使用用户详细信息服务,它在哈希映射中查找发送的用户名并返回用户详细信息。我尝试将 BCrypt 轮数减少到 4,但这并没有产生太大影响。

早些时候,我启用了无状态身份验证,后来将其禁用,但性能仍然很差。该服务托管在 Docker 容器中。

以下是我的安全配置。

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsService userDetailsService;

    @Autowired
    public SecurityConfig(UserDetailsServiceImpl service) {
        this.userDetailsService = service;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        Map encoders = new HashMap<>();
        encoders.put(BCRYPT_ID, new BCryptPasswordEncoder(BCRYPT_ROUNDS));
        return new DelegatingPasswordEncoder(BCRYPT_ID,encoders);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.cors()
            .and()
            .csrf().disable()
            .httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder()); …
Run Code Online (Sandbox Code Playgroud)

java spring-security bcrypt basic-authentication spring-boot

6
推荐指数
3
解决办法
1万
查看次数

服务响应速度慢时间:Java SecureRandom和/ dev/random

我正在尝试调试部署在Tomcat上的应用程序提供的一些慢响应.现在,我侧重于SecureRandom/dev/random(其他一些可能的原因进行了调查和排除).模式如下:

  • Tomcat重启后第一次调用正好需要30.0 xy秒(即使请求在启动后4分钟到达)
  • 之后,一些调用只需要15.0 pq秒(我没有特定的模式可以建立,pq是TP99中所用的时间近似时间)

服务调用涉及加密和解密(AES/ECB/PKCS5Padding).

SecureRandom init/repopulating是否可能导致这种情况发生?

(虽然有一个用catalina.log写的日志说"Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [28,760] milliseconds.")

此外,为了检查是否/dev/random/dev/urandom正在使用,我到测试使用的这个问题.令我惊讶的是,我没有看到其中任何一个的读取,不像链接问题中发生的那样.这些是strace日志中的最后几行:

3561  lstat("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", {st_mode=S_IFREG|0644, st_size=258525, ...}) = 0
3561  open("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", O_RDONLY) = 6
3561  stat("/dev/random", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 8), ...}) = 0
3561  stat("/dev/urandom", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0
3561  open("/dev/random", O_RDONLY)     = 7
3561  open("/dev/urandom", O_RDONLY)    = 8 …
Run Code Online (Sandbox Code Playgroud)

java random tomcat aes strace

5
推荐指数
1
解决办法
4291
查看次数