Spring Security通过实现UserDetailsS​​ervice添加属性

时间:2014-08-06 12:50:15

标签: java hibernate spring-mvc jpa spring-security

我是Spring Security的新手,并且在向核心(spring Security)用户类添加新属性时遇到了问题。

我已经扩展并实现了我自己的UserDetailsS​​ervice版本,但在尝试登录时收到错误消息

  

您的登录失败。   引发:org.springframework.orm.jpa.JpaSystemException:没有实体的默认构造函数:: com.webshop.model.Shopper;嵌套异常是org.hibernate.InstantiationException:没有实体的默认构造函数:: com.webshop.model.Shopper

我自己的用户扩展:

@Entity
@Table(name = "users")
public class Shopper extends User{

    private static final long serialVersionUID = -7060154441729348386L;

    @Id
    private String username;

    @Column(name = "budget")
    private int budget;

    @OneToMany(mappedBy = "users",fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
    private List<Sales> sales = new ArrayList<Sales>();

    @OneToMany(mappedBy = "users",fetch = FetchType.EAGER)
    private List<Items> items = new ArrayList<Items>();

    @Column(name="enabled")
    private boolean enabled;

    public Shopper(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired,
            boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

    public Shopper(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

//getters and setters
}

我的UserDetailsS​​ervice

@Service("usersService")
public class UserServiceImpl implements UsersService, UserDetailsService {
    @Autowired
    private UserRepository usersRepository;

    @Override
    public Shopper find(String username) {
        return usersRepository.findOne(username);
    }

    @Override
    public Shopper save(Shopper user) {
        return usersRepository.save(user);
    }

    @Autowired
    private UserRepository userRepository;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            Shopper domainUser = userRepository.findOne(username);
            boolean enabled = true;
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;
            return new User(domainUser.getUsername(), domainUser.getPassword().toLowerCase(), enabled, accountNonExpired, credentialsNonExpired,
                    accountNonLocked,domainUser.getAuthorities());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

最后我的securtiy config xml

<http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login.html" access="isAnonymous()"/>
        <intercept-url pattern="/loginFailed.html" access="isAnonymous()"/>
        <intercept-url pattern="/logout.html" access="isAnonymous()"/>
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
        <form-login login-page="/login.html" authentication-failure-url="/loginFailed.html"/>
        <logout logout-success-url="/"/>
    </http>

    <beans:bean id="UserServiceImpl" class="com.webshop.service.UserServiceImpl" />
    <authentication-manager>
        <authentication-provider user-service-ref="UserServiceImpl">
            <!-- <password-encoder hash="bcrypt" /> -->
            <!-- <jdbc-user-service data-source-ref="dataSource" />  -->
        </authentication-provider>

    </authentication-manager>

    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost:3306/webshop" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="root" />
    </beans:bean>

1 个答案:

答案 0 :(得分:0)

错误消息是明确的:No default constructor for entity: com.webshop.model.Shopper

您应该添加默认构造函数:

public Shopper() {
    super();
}