我需要從數據庫驗證用戶,Spring Security文檔不會告訴如何使用hibernate進行身份驗證。這是可能的,我該怎麼做?Spring Security 3使用Hibernate進行數據庫身份驗證
回答
您必須創建自己的自定義身份驗證提供程序。
示例代碼:
服務從休眠加載用戶:
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 UserDao 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);
}
}
服務到您的實體轉換爲彈簧用戶對象:
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
@Service("assembler")
public class Assembler {
@Transactional(readOnly = true)
User buildUserFromUserEntity(UserEntity userEntity) {
String username = userEntity.getName();
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.getRoles()) {
authorities.add(new GrantedAuthorityImpl(role.getRoleName()));
}
User user = new User(username, password, enabled,
accountNonExpired, credentialsNonExpired, accountNonLocked, authorities, id);
return user;
}
}
然後基於amespace的應用程序上下文-security.xml文件看起來是這樣的:
<http>
<intercept-url pattern="/login.do*" filters="none"/>
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<form-login login-page="/login.do"
authentication-failure-url="/login.do?error=failed"
login-processing-url="/login-please.do" />
<logout logout-url="/logoff-please.do"
logout-success-url="/logoff.html" />
</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="md5"/>
</authentication-provider>
</authentication-manager>
感謝您提供完整且詳細的答案碼。你能告訴我爲什麼需要Assembler類,爲什麼你不能把這些代碼放在loadUserByUsername方法中? – newbie 2010-04-24 10:06:48
你說得對,彙編程序沒有真正的需要。只是認爲保持userDetailsService簡單並通過彙編器服務使轉換可重用是一個好主意。 – Kdeveloper 2010-04-24 20:12:11
你也希望檢查使用用戶的非棄用方法http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/core/userdetails/User。 html – Necronet 2011-03-03 16:52:10
一個java的配置可以。如果你使用JDBC訪問數據庫是這個樣子
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider =
new DaoAuthenticationProvider();
daoAuthenticationProvider
.setUserDetailsService(userDetailsService);
auth.authenticationProvider(daoAuthenticationProvider);
}
}
,那麼你可以使用以下身份驗證提供程序並避免創建自定義提供程序。它減少了需要9線XML的代碼:
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password from users where username=?" authorities-by-username-query="select u.username, r.authority from users u, roles r where u.userid = r.userid and u.username =?" />
</authentication-provider>
然後,您可以設置您的數據源如下
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/DB_NAME" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
看一看這個帖子:http://codehustler.org/blog/spring-security-tutorial-form-login/ 它涵蓋了你需要知道的一切關於定製Spring Security表單登錄。
我們可以使用散列密碼嗎? – Rafael 2015-02-25 14:53:31
您應該能夠在
- 1. 如何使用Hibernate 3 Annotated Classes配置Spring Security 2數據庫身份驗證?
- 2. spring-security:使用用戶證書對LDAP進行身份驗證
- 3. Spring,使用數據庫進行身份驗證
- 4. 使用Spring Security對Facebook用戶進行身份驗證
- 5. 否使用Spring Security進行身份驗證和授權
- 6. Spring Security 2.0.6使用活動目錄進行身份驗證
- 7. Spring Security:使用Ajax進行身份驗證
- 8. PartialResultException當使用Spring Security和JavaConfig進行身份驗證時
- 9. 使用Spring Security進行RSS身份驗證
- 10. 使用MyBatis進行Spring Security身份驗證
- 11. 使用Spring Security進行數據庫和LDAP驗證
- 12. Spring Boot OAuth2 - 使用本地用戶數據庫與Facebook進行身份驗證
- 13. Spring MVC未對數據庫進行身份驗證
- 14. Spring Security - 通過URL進行身份驗證
- 15. 我能夠在Spring Security中進行身份驗證?
- 16. Spring Security Web Service身份驗證
- 17. Spring-Security DAOAuthenticationProvider要求身份驗證
- 18. Spring Security的授權和身份驗證
- 19. 如何使用spring security使用空密碼進行基本身份驗證?
- 20. 使用Spring Security在運行時切換身份驗證方法?
- 21. 用戶使用Spring Security進行身份驗證BY用戶名查詢
- 22. 使用數據庫在線進行IOS身份驗證
- 23. 使用類中的變量值進行身份驗證的Spring Security
- 24. 如何使用Spring Security對Active Directory服務器進行身份驗證?
- 25. OpenId身份驗證和使用Spring Security 3.0.2進行自動註冊
- 26. 使用Spring MVC + Angular JS進行Windows身份驗證
- 27. 任何使用Spring-ws進行SAML身份驗證的人員?
- 28. 使用Spring與Heroku進行身份驗證
- 29. 使用Spring Security Java配置時禁用基本身份驗證
- 30. 使用grails和spring安全進行跨域身份驗證
請參閱http://stackoverflow.com/questions/2318467/how-do-i-configure-spring-security-2-database-authentication-with-hibernate-3-ann – axtavt 2010-04-21 13:33:50