我有一個User實作 spring security 的類UserDetails。然而,在登錄請求的認證程序中,它給出了一個ClassCastException是UserDetails不能轉換到我的User班。
完整的錯誤:
java.lang.ClassCastException:類 org.springframework.security.core.userdetails.User 不能被轉換為類 nl.teamrepositories.vliegmaatschappij.security.domain.User(org.springframework.security.core.userdetails.User 在未命名的模塊中加載器“應用程式”的;nl.teamrepositories.vliegmaatschappij.security.domain.User 在加載器 org.springframework.boot.devtools.restart.classloader.RestartClassLoader @6ef8cb6 的未命名模塊中)
用戶:
package nl.teamrepositories.vliegmaatschappij.security.domain;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*;
import java.util.Collection;
import java.util.List;
@Entity
@Table(name = "users")
@Getter @Setter @NoArgsConstructor
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
private String firstName;
private String lastName;
private String username;
private String password;
private boolean enabled;
private boolean tokenExpired;
public User(String firstName, String lastName, String username, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.password = password;
}
@ManyToMany
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
在 JwtAuthenticationFilter 中:
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain, Authentication authentication) {
User user = (User) authentication.getPrincipal(); // this is where the error occurs
// more code
}
我不明白為什么我的User班級不能被強制轉換UserDetails為 Spring 班級。
我應該改變什么?
提前致謝。
uj5u.com熱心網友回復:
在JwtAuthenticationFilter. 我猜你正在使用import org.springframework.security.core.userdetails.User而不是你自己的類import nl.teamrepositories.vliegmaatschappij.security.domain.User:
import nl.teamrepositories.vliegmaatschappij.security.domain.User;
(...)
public class JwtAuthenticationFilter {
(...)
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain, Authentication authentication) {
User user = (User) authentication.getPrincipal(); // this is where the error occurs
// more code
}
}
無論是在這里還是在代碼中的其他地方,您都使用了錯誤的User類(Spring 類而不是您的類)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351889.html
上一篇:如何使客戶端可以下載網頁的內容
