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

Categories

0 votes
2.5k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...