2016-01-01 63 views
1

我試圖用SHA編碼驗證與春季安全用戶和醃製我已經在用戶表中添加額外的字段額外的鹽和定製userdetail這種鹽,但每當我嘗試登錄它是拋出壞憑證異常我定製CustomJdbcDaoImpl類是衝刺安全與密碼編碼和鹽

public class CustomJdbcDaoImpl extends JdbcDaoImpl implements IChangePassword { 

    private Logger logger = LoggerFactory.getLogger(CustomJdbcDaoImpl.class); 


    @Override 
    protected UserDetails createUserDetails(String username,UserDetails userFromUserQuery, 
      List<GrantedAuthority> combinedAuthorities){ 
     String returnUsername = userFromUserQuery.getUsername(); 
     if(!isUsernameBasedPrimaryKey()){ 
      returnUsername = username; 
     } 
     logger.info("inside @class CustomJdbcDaoImpl @method createUserDetails USER DETAILS ARE: "+userFromUserQuery.getPassword()+"authritieds: "+combinedAuthorities); 
     return new SaltedUser(returnUsername, 
       userFromUserQuery.getPassword(), 
       userFromUserQuery.isEnabled(), 
       true, 
       true, 
       true, 
       combinedAuthorities, 
       ((SaltedUser)userFromUserQuery).getSalt()); 


    } 

    @Override 
    protected List<UserDetails> loadUsersByUsername(String username) { 
     return getJdbcTemplate() 
       .query(getUsersByUsernameQuery(), 
         new String[] {username}, 
         new RowMapper<UserDetails>() { 
        public SaltedUser mapRow(ResultSet rs, int rowNum) throws SQLException { 
         String username = rs.getString(1); 
         String password = rs.getString(2); 
         boolean enabled = rs.getBoolean(3); 
         String salt = rs.getString(4); 
         SaltedUser saltedUser = new SaltedUser(username, password, enabled, 
           true, 
           true, 
           true, 
           AuthorityUtils.NO_AUTHORITIES, 
           salt); 
         logger.info("inside @class @method loadUsersByUsername salted password is: "+saltedUser.getPassword()); 
         return saltedUser; 
        } 


       }); 
    } 

    @Override 
    public void changePassword(String username, String password) { 
     getJdbcTemplate(). 
     update("update users set password = ? where username = ?",password,username); 

    } 

} 

,每次更改密碼加鹽我DatabasePasswordSecurerBean類是

public class DatabasePasswordSecurerBean extends JdbcDaoSupport { 

     @Autowired 
     private PasswordEncoder passwordEncoder; 

     @Autowired 
     private SaltSource saltSource; 

     @Autowired 
     private UserDetailsService userDetailsService; 

     private Logger logger = LoggerFactory.getLogger(DatabasePasswordSecurerBean.class); 

     public void secureDatabase(){ 
      logger.info("inside @class DatabasePasswordSecurerBean @method secureDatabase entry..."); 
      getJdbcTemplate().query("select username,password from users",new RowCallbackHandler(){ 

       @Override 
       public void processRow(ResultSet rs) throws SQLException { 
        String username = rs.getString(1); 
        String password = rs.getString(2); 

        UserDetails user = userDetailsService.loadUserByUsername(username); 
        String encodedPassword = passwordEncoder.encodePassword(password,saltSource.getSalt(user)); 
        getJdbcTemplate().update("update users set password = ? where username = ?", 
          encodedPassword,username); 

       logger.info("@class DatabasePasswordSecurerBean @method secureDatabase updating password for user: "+username + "to: "+encodedPassword); 
       } 


      }); 

     } 

    } 

security.xml configurations are 




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

     <authentication-manager alias="authenticationManager"> 
       <authentication-provider user-service-ref="jdbcUserService"> 
        <password-encoder ref="passwordEncoder" > 
        <salt-source ref="saltSource" /> 
        </password-encoder> 



       </authentication-provider> 
     </authentication-manager> 


     </beans:beans> 

和我的application.xml是

<!-- Simple implementation of the standard JDBC DataSource interface, 
       configuring the plain old JDBC DriverManager via bean properties --> 
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
       <property name="driverClassName" value="${db.driverClassName}" /> 
       <property name="url" value="${db.connection.url}" /> 
       <property name="username" value="${db.connection.username}" /> 
       <property name="password" value="${db.connection.password}" /> 
      </bean> 

      <bean id="jdbcUserService" class="com.petCart.springsecurity.security.CustomJdbcDaoImpl"> 
       <property name="dataSource" ref="dataSource" /> 
       <property name="enableGroups" value="true"></property> 
       <property name="enableAuthorities" value="false"></property> 
       <property name="usersByUsernameQuery"> 
        <value> 
         select username,password,enabled,salt from users where username = ? 
        </value> 
       </property> 
       <property name="groupAuthoritiesByUsernameQuery"> 
        <value> 
         select r.roleid,r.role_name,p.permissionname from roles r 
         join userrole ur on ur.roleid = r.roleid 
         join users u on u.id = ur.userid 
         join rolepermission rp on r.roleid = rp.roleid 
         join permissions p on p.permissionid = rp.permissionid 
         where u.username = ? 
        </value> 
       </property> 
      </bean> 

      <!-- password encoder --> 
      <bean class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" id="passwordEncoder"/> 
      <bean class="com.petCart.springsecurity.security.DatabasePasswordSecurerBean" init-method="secureDatabase" depends-on="dataSource"> 
       <property name="dataSource" ref="dataSource" /> 
      </bean> 
      <bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"> 
       <property name="userPropertyToUse" value="username" /> 
      </bean> 
+0

我真的網我的假期與所有這些東西,請了一些一個可以幫助我...... – pawan

+0

可能不是問題的答案你期待,但你爲什麼不使用BCrypt,這已經是Spring Security提供的了?它比SHA更強,還包括鹽,一切由你管理,你只需要調用密碼編碼器。 – saljuama

回答

0

試圖改變從

<bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"> 
    <property name="userPropertyToUse" value="username" /> 
</bean> 

<bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource"> 
    <property name="userPropertyToUse" value="salt" /> 
</bean>