我正在用Hibernate开发一个Spring MVC应用程序,但我面临着与数据库连接的问题。
我已经使用org.springframework.jdbc.datasource.DriverManagerDataSource
在web.xml
中定义了数据源,并设置了应用程序连接到的模式的属性,如下所示:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
但是,现在我想使用一个自定义的dataSource,因为(出于安全原因)我在数据库中有一个带有"连接链"的表,其中包含加密的dataSource属性(url、用户名、密码等),所以我不需要在xml中手动设置属性,只需通过该"连接链路"传递它们。
我不知道我是不是在迷惑别人,但我的英语说得不太好,我完全被鄙视了。谢谢和问候。
编辑:感谢您的快速回复!
很抱歉信息不准确。好吧,第一次我的应用程序用一个通用用户和密码(用户名:"你好",密码:"你好)连接到数据库中的一个通用模式,在那里它放置了一个表,其中包含包括我的各种应用程序的"连接链"。
该连接链包含用户、密码、url和MaxConnections。
再次感谢。
Spring包含一个处理用户、数据库哈希和会话的安全功能。您应该使用安全配置文件中定义的身份验证管理器,并创建一个bean来处理从表中获取用户的操作。
<beans:bean id="customUserDetailsService"
class="com.program.service.CustomUserDetailsService" />
<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService" />
</authentication-manager>
然后,您可以如下定义您的用户详细信息服务,以便从应该已经配置好的数据库中获得使用Hibernate 3的用户。
package com.program.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.program.dao.UserDAO;
@Service
@Transactional(readOnly = true)
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private UserDAO userDAO;
public UserDetails loadUserByUsername(String login)
throws UsernameNotFoundException {
com.program.model.User domainUser = userDAO.getUser(login);
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new User(domainUser.getUsername(), domainUser.getPassword(),
enabled, accountNonExpired, credentialsNonExpired,
accountNonLocked, getAuthorities(domainUser.getRole().getId())); //get role id of domain user
}
public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
return authList;
}
public List<String> getRoles(Integer role) {
List<String> roles = new ArrayList<String>();
if (role.intValue() == 1) {
roles.add("ROLE_ADMIN");
} else if (role.intValue() == 2) {
roles.add("ROLE_MODERATOR");
}
return roles;
}
public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
}
您的数据库应该在application.properties中配置如下:
#DB properties:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/program
db.username=root
db.password=password
#Hibernate Configuration:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
entitymanager.packages.to.scan=com.program.model
这将允许您使用如下登录表单从数据库中登录用户。
<form method="post" action="<c:url value='j_spring_security_check'/>">
<table border="0px" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td><spring:message code="login.login"/></td>
<td><input type="text" name="j_username" id="j_username"
size="30" maxlength="40" /></td>
</tr>
<tr>
<td><spring:message code="login.password"/></td>
<td><input type="password" name="j_password" id="j_password"
size="30" maxlength="32" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</tbody>
</table>
</form>
我不建议使用连接链。我希望这能有所帮助!