我正在使用 spring-security-oauth2 并且遇到了这样的异常:
Caused by: java.io.NotSerializableException: com.samsung.diary.security.UserDetailsServiceImpl
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
在我的配置文件中,我有:
<beans:bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
<beans:constructor-arg ref="clientDetails" />
</beans:bean>
<authentication-manager id="clientAuthenticationManager" >
<authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
如果我将其更改为:
<beans:bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
<beans:constructor-arg ref="clientDetails" />
</beans:bean>
<authentication-manager id="clientAuthenticationManager" >
<authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="2" password="2" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
一切都完美无缺。
我的 UserDetailsService 实现:
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
@Qualifier("usersDAO")
private UsersDAO dao;
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
final com.samsung.diary.domain.User userEntity = dao.findByLogin(username);
if (userEntity == null)
throw new UsernameNotFoundException("user not found");
String login = userEntity.getLogin();
String password = userEntity.getPassword();
boolean enabled = userEntity.getEnabled().equals("true");
boolean accountNonExpired = enabled;
boolean credentialsNonExpired = enabled;
boolean accountNonLocked = enabled;
GrantedAuthority auth = new GrantedAuthority() {
private static final long serialVersionUID = 1L;
public String getAuthority() {
return userEntity.getAuthority();
}
};
Set<GrantedAuthority> set = new HashSet<GrantedAuthority>();
set.add(auth);
UserDetails details = new User(login, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
set);
return details;
}
}
据我了解,有一些东西与 Serializable 接口有关,但我不知道如何解决它。
编辑:我制作了 UserDetailsServiceImpl 和 UsersDAO Serializable,现在它可以工作了。但我仍然不明白我的 UserDetailsServiceImpl 被写入流中的哪个位置。如果有人给我一个提示,我将不胜感激。