bug 出現
- 在項目中使用spring security 進行用戶登錄驗證,其中,出現了一個問題,久久不能解決
- 希望給遇到過同樣的問題或者即將遇到這個問題的夥伴,一個參考
- 項目使用spring boot 和spring security進行開發,程序已經能正常運行,但是在打包項目時,會出現如下的編譯錯誤。
- 項目打包時,遇到的錯誤,有時編譯過,有時編譯不過,反正各種不能編譯
D5Utils.java:[16,46] sun.misc.BASE64Encoder是內部專用 API, 可能會在未來發行版中刪除
[INFO] 6 warnings
[INFO] -------------------------------------------------------------
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /E:/SVNdownload/zhongcheroof/zhongcheroof/src/main/java/com/xinda/models/dto/ReturnUserInfo.java:[12,1] 對於User(沒有參數), 找不到合適的構造器
構造器 org.springframework.security.core.userdetails.User.User(java.lang.String,java.lang.String,java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>)不適用
(實際參數列表和形式參數列表長度不同)
構造器 org.springframework.security.core.userdetails.User.User(java.lang.String,java.lang.String,boolean,boolean,boolean,boolean,java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>)不適用
(實際參數列表和形式參數列表長度不同)
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.765 s
[INFO] Finished at: 2019-02-21T16:11:54+08:00
[INFO] Final Memory: 38M/283M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project zhongcheroof: Compilation failure
[ERROR] /E:/SVNdownload/zhongcheroof/zhongcheroof/src/main/java/com/xinda/models/dto/ReturnUserInfo.java:[12,1] 對於User(沒有參數), 找不到合適的構造器
[ERROR] 構造器 org.springframework.security.core.userdetails.User.User(java.lang.String,java.lang.String,java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>)不適用
[ERROR] (實際參數列表和形式參數列表長度不同)
[ERROR] 構造器 org.springframework.security.core.userdetails.User.User(java.lang.String,java.lang.String,boolean,boolean,boolean,boolean,java.util.Collection<? extends org.springframework.security.core.GrantedAuthority>)不適用
[ERROR] (實際參數列表和形式參數列表長度不同)
[ERROR] -> [Help 1]
[ERROR]
- 項目中出現編譯錯誤的代碼如下:
- 登錄的login接口
@Data
@Service
public class LoginValidate implements UserDetailsService{
@Autowired
private UserService userService;
private User userInfo;
@Autowired
UserRepos userRepos;
public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException {
userInfo = userService.getUserByUsername(arg0);
if (userInfo == null){
throw new UsernameNotFoundException("用戶不存在!");
}
String loginTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
userRepos.updateLoginTimeByUsername(userInfo.getUsername(),loginTime);
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
switch (userInfo.getLevel()) {
case 1://管理員
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
System.err.println("用戶權限:ROLE_ADMIN");
break;
case 2://普通用戶
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
System.err.println("用戶權限:ROLE_USER");
break;
default:
throw new UsernameNotFoundException("用戶權限配置錯誤");
}
System.err.println("進入login登錄驗證");
ReturnUserInfo returnUserInfo = new ReturnUserInfo(userInfo.getUsername(), userInfo.getPassword(), authorities);
returnUserInfo.setUsername(userInfo.getUsername());
returnUserInfo.setUserLevel(userInfo.getLevel());
returnUserInfo.setLoginTime(loginTime);
returnUserInfo.setRole(UserLevelEnum.getName(userInfo.getLevel()));
System.err.println("返回值:" + returnUserInfo);
return returnUserInfo;
}
}
- 返回給前端頁面的實體類
@Data
public class ReturnUserInfo extends User{
private String username;
private int userLevel;
private String role;
private String loginTime;
public ReturnUserInfo(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
public ReturnUserInfo(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, true, true, true, true, authorities);
}
}
- 上述代碼導致了編譯有時成功,有時不成功
問題原因及解決
-
在 ReturnUserInfo 中使用了 @Data 這個註解,
-
網上的說法是:
-
@Data 我自己嘗試了下,我們使用 @Data 註解就可以有下面幾個註解的功能: @ToString、@Getter、@Setter、@EqualsAndHashCode、@NoArgsConstructor 。
- 注意的是,同時使用@Data 和 @AllArgsConstructor 後 ,默認的無參構造函數失效,如果需要它,要重新設置 @NoArgsConstructor
- 這裏就是爲什麼我們的一個實體類需要加上三個註解
@Data @NoArgsConstructor @AllArgsConstructor public class WarningInfo { private Picture picture; private Video video; }
-
經過查詢User的源碼,發現確實User的這個類沒有無參構造器,只有另外的兩個構造器
public User(String username, String password,
Collection<? extends GrantedAuthority> authorities) {
this(username, password, true, true, true, true, authorities);
}
public User(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
if (((username == null) || "".equals(username)) || (password == null)) {
throw new IllegalArgumentException(
"Cannot pass null or empty values to constructor");
}
this.username = username;
this.password = password;
this.enabled = enabled;
this.accountNonExpired = accountNonExpired;
this.credentialsNonExpired = credentialsNonExpired;
this.accountNonLocked = accountNonLocked;
this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
}
-
因此,原因出現了,在使用了@data這個註解的時候,添加了默認的User 無參構造器,而源碼中並沒有User無參構造器,因此造成了編譯錯誤。
-
解決辦法
- 將@data註解註釋掉,用傳統的set和get方法進行標識,代碼如下,如下操作,便不會在有編譯錯誤
//@Data
public class ReturnUserInfo extends User{
private String username;
private int userLevel;
private String role;
private String loginTime;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserLevel() {
return userLevel;
}
public void setUserLevel(int userLevel) {
this.userLevel = userLevel;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getLoginTime() {
return loginTime;
}
public void setLoginTime(String loginTime) {
this.loginTime = loginTime;
}
public ReturnUserInfo(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
public ReturnUserInfo(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, true, true, true, true, authorities);
}
}
- 注意:使用 @Data 時候回變感嘆號,感嘆號的內容是
Generating equals/hashCode implementation but without a call to superclass,
even though this class does not extend java.lang.Object.
If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
-
翻譯過來是:生成equals/hashcode實現,但不調用超類,即使此類不擴展java.lang.object。如果有意這樣做,請將“@equalsandhashcode(callsuper=false)”添加到您的類型中。
-
寫一個類的時候,java會自動給這個類提供一個無參構造器。
-
本文理解不一定正確,有改善者請提出來