首先包没有倒错,在别的项目中可以正常使用
Could not autowire. No beans of ‘PasswordEncoder’ type found.
报错如下:
ERROR 3860 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field pw in com.cavan.springsecuritydemo.service.UserDetailsServiceImpl required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' 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.springframework.security.crypto.password.PasswordEncoder' in your configuration.
解决方案:
报错中已经提示了让自己注入一个PasswordEncoder,那就尝试一下
Action:
Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.
package com.cavan.springsecuritydemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@SpringBootApplication
public class SpringSecurityDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityDemoApplication.class, args);
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
再次运行代码,即可成功运行
当写完了跳转自定义登录页面方法时,又出现了新的错误
package com.cavan.springsecuritydemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login.html");
//拦截器
http.authorizeHttpRequests()
// 所有请求都必须被认证,必需登录之后才能被访问
.anyRequest().authenticated();
}
@Bean
public PasswordEncoder getPw(){
return new BCryptPasswordEncoder();
}
}
***************************
APPLICATION FAILED TO START
***************************
Description:
Field pw in com.cavan.springsecuritydemo.service.UserDetailsServiceImpl required a single bean, but 2 were found:
- getPw: defined by method 'getPw' in class path resource [com/cavan/springsecuritydemo/config/SecurityConfig.class]
- passwordEncoder: defined by method 'passwordEncoder' in com.cavan.springsecuritydemo.SpringSecurityDemoApplication
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
同样的根据错误描述,需要添加一个@Qualifier注解,在UserDetailsServiceImpl 中
package com.cavan.springsecuritydemo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Qualifier("getPw")
@Autowired
private PasswordEncoder pw;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 1、查询数据库判断用户名是否存在,如果不存在就会抛出UsernameNotFoundException异常
if(!"admin".equals(username)){
throw new UsernameNotFoundException("用户不存在");
}
// 2、把查询出来的密码(注册时已经加密过)进行解析,或者直接吧密码放入构造方法
String password = pw.encode("123");
return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));
}
}
问题解决,成功运行!