2011-02-24 36 views
5

我試圖使用數據庫中的表我的Spring應用程序應用安全。自定義用戶春季安全細節

我有我的applicationContext-安全到目前爲止是:

<beans:bean id="userDetailsService" class="org.intan.pedigree.service.UserDetailsServiceImpl"></beans:bean> 

<http auto-config='true'> 
    <intercept-url pattern="/**" access="ROLE_USER" /> 
</http> 

<beans:bean id="daoAuthenticationProvider" 
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <beans:property name="userDetailsService" ref="userDetailsService" /> 
</beans:bean> 

<beans:bean id="authenticationManager" 
    class="org.springframework.security.authentication.ProviderManager"> 
    <beans:property name="providers"> 
     <beans:list> 
      <beans:ref local="daoAuthenticationProvider" /> 
     </beans:list> 
    </beans:property> 
</beans:bean> 


<authentication-manager> 
    <authentication-provider user-service-ref="userDetailsService"> 
     <password-encoder hash="plaintext" /> 
    </authentication-provider> 
</authentication-manager> 

我實施的UserDetailsS​​ervice看起來如下:

package org.intan.pedigree.service; 

import org.intan.pedigree.dao.UserEntityDAO; 
import org.intan.pedigree.dao.UserEntityDAOImpl; 
import org.intan.pedigree.form.UserEntity; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.dao.DataAccessException; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UserDetailsService; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 

@Service("userDetailsService") 
public class UserDetailsServiceImpl implements UserDetailsService { 

    @Autowired 
    private UserEntityDAO dao; 
    @Autowired 
    private Assembler assembler; 

    @Transactional(readOnly = true) 
    public UserDetails loadUserByUsername(String username) 
      throws UsernameNotFoundException, DataAccessException { 

     UserDetails userDetails = null; 
     UserEntity userEntity = dao.findByName(username); 
     if (userEntity == null) 
       throw new UsernameNotFoundException("user not found"); 

     return assembler.buildUserFromUserEntity(userEntity); 
    } 
} 

我的彙編程序如下所示:

package org.intan.pedigree.service; 

import java.util.ArrayList; 
import java.util.Collection; 

import org.intan.pedigree.form.SecurityRoleEntity; 
import org.intan.pedigree.form.UserEntity; 
//import org.springframework.security.core.GrantedAuthority; 
//import org.springframework.security.core.authority.GrantedAuthorityImpl; 
//import org.springframework.security.core.userdetails.User; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.GrantedAuthorityImpl; 
import org.springframework.security.core.userdetails.User; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

@Service("assembler") 
public class Assembler { 

    @Transactional(readOnly = true) 
    User buildUserFromUserEntity(UserEntity userEntity) { 

    String username = userEntity.getUsername(); 
    String password = userEntity.getPassword(); 
    boolean enabled = userEntity.isActive(); 
    boolean accountNonExpired = userEntity.isActive(); 
    boolean credentialsNonExpired = userEntity.isActive(); 
    boolean accountNonLocked = userEntity.isActive(); 
    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 
    for (SecurityRoleEntity role : userEntity.getUserSecurityRoleEntity()) { 
     authorities.add(new GrantedAuthorityImpl(role.getName())); 
    } 

    User user = new User(username, password, enabled, 
     accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); 
    return user; 
    } 
} 

現在用戶實體爲:

package org.intan.pedigree.form; 

import java.util.Date; 
import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
@Entity 
@Table(name="user") 
public class UserEntity { 

    @Id 
    @GeneratedValue 
    @Column(name="ID") 
    private int id; 
    @Column(name="first_name") 
    private String first_name; 
    @Column(name="family_name") 
    private String last_name; 
    @Column(name="dob") 
    private Date dob; 
    @Column(name="password") 
    private String password; 
    @Column(name="username") 
    private String username; 
    @Column(name="active") 
     @NotNull 
    private boolean isActive; 
    @Column(name="user_types_id") 
    private int user_types_id; 
    @Column(name="confirm_password") 
    public String confirmPassword; 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "user_address", joinColumns = { @JoinColumn(name = "user_id") }, 
      inverseJoinColumns = { @JoinColumn(name = "address_id") }) 
    private Set<Address> userAddress = new HashSet<Address>(0); 
    /*******************************************************************************/ 
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "user_security_role", joinColumns = { @JoinColumn(name = "user_id") }, 
      inverseJoinColumns = { @JoinColumn(name = "security_role_id") }) 
    private Set<SecurityRoleEntity> userSecurityRoleEntity = new HashSet<SecurityRoleEntity>(0); 

    public Set<Address> getUserAddress(){ 
     return this.userAddress; 
    } 

    public void setUserAddress(Set<Address> userAddress) { 
     this.userAddress = userAddress; 
    } 
    /*****************************************************************************/ 

    public Set<SecurityRoleEntity> getUserSecurityRoleEntity(){ 
     return this.userSecurityRoleEntity; 
    } 

    public void setUserSecurityRoleEntity(Set<SecurityRoleEntity> userSecurityRoleEntity) { 
     this.userSecurityRoleEntity = userSecurityRoleEntity; 
    } 


    public boolean isActive() { 
     return isActive; 
    } 

    public void setActive(boolean isActive) { 
     this.isActive = isActive; 
    } 

    public String getConfirmPassword() { 
     return confirmPassword; 
    } 
    public void setConfirmPassword(String confirmPassword) { 
     this.confirmPassword = confirmPassword; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getFirst_name() { 
     return first_name; 
    } 
    public void setFirst_name(String first_name) { 
     this.first_name = first_name; 
    } 
    public String getLast_name() { 
     return last_name; 
    } 
    public void setLast_name(String last_name) { 
     this.last_name = last_name; 
    } 
    public Date getDob() { 
     return dob; 
    } 
    public void setDob(Date dob) { 
     this.dob = dob; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getUsername() { 
     return username; 
    } 
    public void setUsername(String username) { 
     this.username = username; 
    } 
    public int getUser_types_id() { 
     return user_types_id; 
    } 
    public void setUser_types_id(int user_types_id) { 
     this.user_types_id = user_types_id; 
    } 


} 

我userentitydao接口:

package org.intan.pedigree.dao; 

import java.util.List; 

import org.intan.pedigree.form.UserEntity; 


public interface UserEntityDAO { 
    public void removeUserEntity(Integer id); 
    public List<UserEntity> listUserEntity() ; 
    public void addUserEntity(UserEntity user) ; 
    public void updateUserEntity(UserEntity user) ; 
    public UserEntity getUserEntityByID(Integer id); 
    public UserEntity findByName(String username); 
} 

和實施是:

@Repository 
public class UserEntityDAOImpl implements UserEntityDAO{ 

    @Autowired 
    private SessionFactory sessionFactory; 

    public void addUserEntity(UserEntity user) { 
     try { 
     sessionFactory.getCurrentSession().save(user); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

    public UserEntity findByName(String username) { 
     UserEntity user = (UserEntity) sessionFactory.getCurrentSession().createQuery(
       "select u form user u where u.username = '" + username + "'"); 
     return user; 

    } 

    public UserEntity getUserEntityByID(Integer id) { 
     UserEntity user = (UserEntity) sessionFactory.getCurrentSession().createQuery(
       "select u form user u where id = '" + id + "'"); 
     return user; 
    } 
    public void updateUserEntity(UserEntity user) { 
     try { 
     sessionFactory.getCurrentSession().update(user); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

    public List<UserEntity> listUserEntity() { 

     return sessionFactory.getCurrentSession().createQuery("from User") 
       .list(); 
    } 

    public void removeUserEntity(Integer id) { 
     UserEntity user = (UserEntity) sessionFactory.getCurrentSession().load(
       UserEntity.class, id); 
     if (null != user) { 
      sessionFactory.getCurrentSession().delete(user); 
     } 

    } 
} 
現在

當我嘗試在Tomcat上部署我得到以下異常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Injection of autowired dependencies failed; nested exception 
is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.intan.pedigree.dao.UserEntityDAO org.intan.pedigree.service.UserDetailsS 
erviceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.intan.pedigree.dao.UserEntityDAO] found 
for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotatio 
n.Autowired(required=true)} 

而不是我做什麼,我無法弄清楚它有什麼問題。任何的建議都受歡迎。

非常感謝

回答

1

我沒有在您的上下文中看到關於UserEntityDAOImplAssembler beans的任何聲明,也沒有組件掃描來自動檢測它。

你要麼需要聲明他們沿着UserDetailsServiceImpl,或添加<context:component-scan>地方。

+0

我的血統-servlet.xml中內部使用:<背景:組件掃描基礎包=「org.intan.pedigree」 />這是不夠的這應掃描的一切..我用@Controller控制器可以使用的服務和DAO的沒有問題.. :( - user529065 – giannisapi

+0

@ user529065:Spring Security沒有使用'-servlet.xml'上下文。您需要將組件掃描添加到根web應用程序上下文中,即與Spring Security的相同上下文中。 – skaffman

+0

好吧,我添加了組件掃描,現在它可以工作。但我可以問另一個問題嗎?當我嘗試登錄時,我從hibernate中得到這個似乎很奇怪的錯誤: org.hibernate.hql.ast.QuerySyntaxException:意外的標記:第1行第10列附近的表單[select u form form user u where u.username ='giannisapi '] 對此有任何線索? – giannisapi

0

我想你忘了你的應用程序上下文中定義UserEntityDAO。

+0

我的系譜-servlet.xml中內部使用: <上下文:組分掃描基包= 「org.intan.pedigree」/> 這是不夠的?這應該掃描的一切..我的控制器與@Controller可以使用的服務和DAO的沒有問題.. :( – giannisapi

1

的Spring Security的所有相關性(如您的DAO)應在根Web應用程序上下文decalred(在<context-param>命名contextConfigLocation配置applicationContext.xml或其他XML文件),而不是在serlvlet的conext(...-servlet.xml),因爲核心的Spring Security豆是在根上下文中聲明的,並且該上下文中的bean不能訪問servlet上下文中的bean。

如果使用<context:component-scan>,你可以按照this solution參見: