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>
我不建议使用连接链。我希望这有帮助!