springmvc 登录怎么跟数据库验证 springmvc怎么实现注解验证登录

springmvc\u7684controllor\uff0c\u600e\u4e48\u8fde\u6570\u636e\u5e93\u9a8c\u8bc1\u767b\u5f55\uff1f


\u5728\u5b9e\u4f53\u7c7b\u9700\u8981\u9a8c\u8bc1\u7684\u5c5e\u6027\u6807\u8bb0

springmvc登录验证使用db校验:
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.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import UserBean;

@Repository
@Service
@Transactional(readOnly=true)
public class SimpleUserDetailsService implements UserDetailsService {

@Autowired
private UserDao userDAO;

private UserBean domainUser;

public UserDetails loadUserByUsername(String login)
throws UsernameNotFoundException {
domainUser = userDAO.getUser(login);

boolean enabled = true;
boolean disabled = false;
boolean accountNonExpired = true;
boolean accountExpired = false;
boolean credentialsNonExpired = true;
boolean credentialsExpired = false;
boolean accountNonLocked = true;
boolean accountLocked = false;

if (null != domainUser){
return new User(
domainUser.getUsername(), //getLogin(),
domainUser.getPassword(),
enabled,
accountNonExpired,
credentialsNonExpired,
accountNonLocked,
getAuthorities(domainUser.getIduser() ) );

} else {
return new User(
"**20**",
"**//**",
disabled,
accountExpired,
credentialsExpired,
accountLocked,
getAuthorities(-20 ) ) ;
}

}

public Collection<? extends GrantedAuthority> getAuthorities(Integer userid) {
List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(userid));
return authList;
}

public List<String> getRoles(Integer userid) {

List<String> roles = new ArrayList<String>();
roles = userDAO.getUserRoles(userid);

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;
}

扩展阅读:www.sony.com.cn ... springmvc处理流程图 ... www.paperyy.com ... springmvc最新版本是多少 ... paperpass免费入口 ... spring mvc怎么使用 ... spring mvc 执行流程 ... spring mvc工作流程 ... spring mvc请求的处理过程 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网