Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 创建名为';securityConfig';:通过字段'表示未满足的依赖关系;安全服务';_Spring_Spring Boot_Spring Security_Spring Data Jpa - Fatal编程技术网

Spring 创建名为';securityConfig';:通过字段'表示未满足的依赖关系;安全服务';

Spring 创建名为';securityConfig';:通过字段'表示未满足的依赖关系;安全服务';,spring,spring-boot,spring-security,spring-data-jpa,Spring,Spring Boot,Spring Security,Spring Data Jpa,证券配置 安全实用程序 错误: org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“securityConfig”的bean时出错:通过字段“securityService”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.unsatifiedpendencyException:创建名为“securityService”的bean时出错:通过字段“userRep

证券配置

安全实用程序

错误: org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“securityConfig”的bean时出错:通过字段“securityService”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.unsatifiedpendencyException:创建名为“securityService”的bean时出错:通过字段“userRepository”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“userRepository”的bean时出错:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:未能为方法public abstract com.book.entity.User com.book.entity.security.impl.UserRepository.findByUsername(java.lang.String)创建查询!在此ManagedType[com.book.entity.User]上找不到具有给定名称[username]的属性


异常会告诉您出了什么问题<代码>无法在此ManagedType[com.book.entity.User]上找到具有给定名称[用户名]的属性。。您的
用户
没有
用户名
字段。我如何修复它,先生?谢谢,先生,,,我将用户名字段重命名为用户名…但我不知道为什么会发生这种情况。请解释一下。关于例外情况有什么不清楚的。您的字段名为
userName
而不是
userName
。后者是预期的,因为您将方法命名为
findByUsername
package com.book.entity.security.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.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;

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

    @Autowired
    private Environment env;

    @Autowired
    private SecurityService securityService;

    private BCryptPasswordEncoder passwordEncoder() {
        return SecurityUtility.passwordEncoder();
    }

    private static final String[] PUBLIC_MATCHERS = { "/css/**", "/js/**", "/image/**", "/", "/myAccount" };

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().
        /* antMatchers("/**"). */
                antMatchers(PUBLIC_MATCHERS).permitAll().anyRequest().authenticated();

        http.csrf().disable().cors().disable().formLogin().failureUrl("/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(securityService).passwordEncoder(passwordEncoder());
    }
}
package com.book.entity.security.impl;
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 = "ABCEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        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;
    }
}
package com.book.entity.security.impl;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.book.entity.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long>{
    User findByUsername(String userName);
}
package com.book.entity.security.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.book.entity.User;

@Service
public class SecurityService implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(userName);

        if (null == user) {
            throw new UsernameNotFoundException("Username not found");
        }

        return user;
    }
}