Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

Spring Boot Security many connections open after just configuration

Hello my app is deployed into Heroku which has limited number of database connections (20). This is why I need to watch carefully every opened connection. With my code number of connections from 0 reaches 10 after just refreshing the page. Why is that so? Here is my code:

package pl.jawegiel.springsecurityexample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .cors().and().csrf().disable()
                .authorizeRequests().antMatchers("/getUsersInfo")
                .fullyAuthenticated().and().httpBasic();

    }


    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder())
                .usersByUsernameQuery("SELECT username, password, 'true' as enabled from users where username=?")
                .authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");
    }
}

in application properties:

spring.datasource.url=...
spring.datasource.platform=postgres
spring.datasource.username=...
spring.datasource.password=...

and my controller.java

@RestController
@CrossOrigin(origins = "*")
public class Controller {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String abc() {
        return "abc";
    }

...

}

Could you tell me why there is so many connection and how to solve that? Is there anything you need else? Thank you in advance.

BEST ANSWER WILL BE MARKED AS ACCEPTED!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...