lundi 29 avril 2019

Spring been type not well define and missing configuration

I am making changes to the security of Spring to connect to the database, however it says that the bean is not well define and I have use the @Autowired and its corresponding @Bean; I am still learning the configuration.

Could this be a missing plug?

Error:
Description: Field env in com.bookstore.config.SecurityConfig required a bean of type 'org.hibernate.cfg.Environment' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.hibernate.cfg.Environment' in your configuration.

Github: https://github.com/latinprogrammer/Ecommerce-Spring-Boot

SecurityConfig.java

    package com.bookstore.config;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.hibernate.cfg.Environment;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    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 org.springframework.security.web.util.matcher.AntPathRequestMatcher;

    import com.bookstore.service.impl.UserSecurityService;
    import com.bookstore.utility.SecurityUtility;

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


@Autowired
private Environment env;


@Autowired
private UserSecurityService userSecurityService;

private BCryptPasswordEncoder passwordEncoder() {

    return SecurityUtility.passwordEncoder();

}

private static final String[] PUBLIC_MATCHERS= {
        "/CSS/**",
        "/JS/**",
        "/img/**",
        "/",
        "/myAccount"


};

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

    http

         .authorizeRequests(). 
      /*   antMatchers("/**").*/
         antMatchers(PUBLIC_MATCHERS).
         permitAll().anyRequest().authenticated();

    http

    .csrf().disable().cors().disable()
    .formLogin().failureForwardUrl("/login?error").defaultSuccessUrl("/")
    .loginPage("/login").permitAll()
    .and()
    .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    .logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll()
    .and().rememberMe();


}

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

    auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());

}

}

---------------------------------------------------------
SecurityUtility.java


package com.bookstore.utility;

import java.security.SecureRandom;
import java.util.Random;

import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class SecurityUtility {

    private static final String SALT = "salt"; // Salt should be protected carefully

     @Bean
     public static BCryptPasswordEncoder passwordEncoder() {
        return new  BCryptPasswordEncoder(12, new SecureRandom(SALT.getBytes()));

     }

     @Bean
     public static String randomPassword() {

         String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
         StringBuilder salt = new StringBuilder();
         Random rnd = new Random();

         while(salt.length() < 18)
         {
             int index = (int) (rnd.nextFloat() * SALTCHARS.length());
             salt.append(SALTCHARS.charAt(index));
         }

         String saltStr = salt.toString();
         return saltStr;
     }

}


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->

    </parent>
    <groupId>com.bookstore</groupId>
    <artifactId>bookstore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bookstore</name>
    <description>Frontend part for bookstore project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>




Aucun commentaire:

Enregistrer un commentaire