MySQLNonTransientConnectionException:无法创建与数据库服务器的连接,并且无法获取jdbc连接

时间:2018-09-06 06:36:28

标签: java mysql hibernate spring-boot

我正在做一个关于春季安全性的项目。当我运行项目时,出现上述异常。 应用程序在localhost中运行但是。注册后,我得到“无法获取JDBC连接”。有人可以帮帮我。

// application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/mobile
spring.datasource.username = moto_g
spring.datasource.password = ThePassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
logging.level.org.hibernate.SQL=debug

//实体类

@Entity
    @Table(name = "user")
    public class User {
    @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "user_id")
        private int id;
        @Column(name = "email")
        @Email(message = "*Please provide a valid Email")
        @NotEmpty(message = "*Please provide an email")
        private String email;
        @Column(name = "password")
        @Length(min = 5, message = "*Your password must have at least 5 characters")
        @NotEmpty(message = "*Please provide your password")
        private String password;
        @Column(name = "name")
        @NotEmpty(message = "*Please provide your name")
        private String name;
        @Column(name = "last_name")
        @NotEmpty(message = "*Please provide your last name")
        private String lastName;
        @Column(name = "active")
        private int active;
        @ManyToMany(cascade = CascadeType.ALL)
        @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
        private Set<Role> roles;
    omitted getters and setters.....

//存储库

@Repository("userRepository")
public interface UserRepository extends JpaRepository<User, Long> {
     User findByEmail(String email);
}

//服务实施

@Service("userService")
    public class UserServiceImpl implements UserService{
    @Autowired
        private UserRepository userRepository;
        @Autowired
        private RoleRepository roleRepository;
        @Autowired
        private BCryptPasswordEncoder bCryptPasswordEncoder;

        @Override
        public User findUserByEmail(String email) {
            return userRepository.findByEmail(email);
        }

        @Override
        public void saveUser(User user) {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            user.setActive(1);
            Role userRole = roleRepository.findByRole("ADMIN");
            user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
            userRepository.save(user);
        }
    }

// WebConfiguration

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private DataSource dataSource;

    @Value("${spring.queries.users-query}")
    private String usersQuery;

    @Value("${spring.queries.roles-query}")
    private String rolesQuery;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.
            jdbcAuthentication()
                .usersByUsernameQuery(usersQuery)
                .authoritiesByUsernameQuery(rolesQuery)
                .dataSource(dataSource)
                .passwordEncoder(bCryptPasswordEncoder);
    }

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

        http.
            authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/registration").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest()
                .authenticated().and().csrf().disable().formLogin()
                .loginPage("/login").failureUrl("/login?error=true")
                .defaultSuccessUrl("/admin/home")
                .usernameParameter("email")
                .passwordParameter("password")
                .and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/").and().exceptionHandling()
                .accessDeniedPage("/access-denied");
    }

//这是我的控制台页面 console

0 个答案:

没有答案