实现UserDetailsS​​ervice时java.io.NotSerializableException

时间:2013-04-26 15:18:52

标签: spring spring-security oauth-2.0

我正在使用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>

everething工作得很完美。

我的UserDetailsS​​ervice实现:

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接口相关,但我无法弄清楚如何解决它。

编辑:我制作了UserDetailsS​​erviceImpl和UsersDAO Serializable,现在可以使用了。但我仍然不明白我的UserDetailsS​​erviceImpl在流中的位置。 如果有人给我一个提示,我将非常感激。

0 个答案:

没有答案