Spring 创建名为';securityConfig';:通过字段'表示未满足的依赖关系;安全服务';
证券配置 安全实用程序 错误: 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]的属性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
异常会告诉您出了什么问题<代码>无法在此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;
}
}