【发布时间】:2018-08-24 08:02:10
【问题描述】:
我正在尝试将身份验证功能添加到我的项目中,但我遇到了这个问题:错误创建名称为“springSecurityFilterChain”的 bean 原因:java.lang.NoClassDefFoundError: org/springframework/security/web/authentication/ui/DefaultLogoutPageGeneratingFilter
@Service
public class MyUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(MyUserDetailsAuthenticationProvider.class);
private final Map<String, String> usersAndPasswords = new HashMap<>();
@PostConstruct
public void initUsers() {
// this will add a user and encode its password to md5
// Spring StandardPasswordEncoder will add salt by default
usersAndPasswords.put("admin", "admin");
usersAndPasswords.put("hamou", "amroun");
}
@Override
public void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
// Do nothing
}
@Override
public UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
LOGGER.debug("Authentication = {}", authentication);
if (!usersAndPasswords.containsKey(username)) {
throw new UsernameNotFoundException("User name not found : " + username);
}
String password = authentication.getCredentials().toString();
String encodedPassword = usersAndPasswords.get(username);
if (!password.equals(encodedPassword)) {
throw new BadCredentialsException("Wrong password");
}
if (username.equals("hamou")) {
return new User(username, "", createAuthorities("ROLE_USER"));
} else {
return new User(username, "", createAuthorities("ROLE_ADMIN"));
}
}
public UserDetails whoMe(String username) {
if (!usersAndPasswords.containsKey(username)) {
throw new UsernameNotFoundException("User name not found : " + username);
} else {
if (username.equals("hamou")) {
return new User(username, "", createAuthorities("ROLE_USER"));
} else {
return new User(username, "", createAuthorities("ROLE_ADMIN"));
}
}
}
private Collection<? extends GrantedAuthority> createAuthorities(String... roles) {
Collection<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
}
这是我的依赖 spring security 和 spring-boot parent
<!-- dependency for security -->
<dependency>
<!-- Starter for using Spring Security -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<!-- Make method based security testing easier -->
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- JSON Web Token Support -->
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<!-- dependency for security -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
【问题讨论】:
-
org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE -
我会说,你有一个依赖问题,因此你必须发布你的 POM 的依赖部分。使用edit 链接为您的问题添加信息。不要在 cmets 中这样做。
-
感谢您的回答 :) 您的意思是版本问题?
-
是的,Spring Security 的版本有些问题。您使用 BOM 吗?
-
是的,我分享了它,实际上我使用的是 2.0.3 版本的 spring-boot
标签: java spring-boot authentication spring-security