authdaoimpl.java
来自「移动彩信管理平台」· Java 代码 · 共 221 行
JAVA
221 行
package com.my7g.zj.mobile.mms.util;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.context.ApplicationContextException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import com.my7g.zj.mobile.mms.bean.User;
import com.my7g.zj.mobile.mms.sys.DaoSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import javax.sql.DataSource;
public class AuthDaoImpl extends JdbcDaoSupport implements UserDetailsService {
private DaoSupport daosupportCp;
public static final String DEF_USERS_BY_USERNAME_QUERY = "SELECT username,password,enabled FROM users WHERE username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "SELECT username,authority FROM authorities WHERE username = ?";
// ~ Instance fields
// ================================================================================================
protected MappingSqlQuery authoritiesByUsernameMapping;
protected MappingSqlQuery usersByUsernameMapping;
private String authoritiesByUsernameQuery;
private String rolePrefix = "";
private String usersByUsernameQuery;
private boolean usernameBasedPrimaryKey = true;
// ~ Constructors
// ===================================================================================================
public AuthDaoImpl() {
usersByUsernameQuery = DEF_USERS_BY_USERNAME_QUERY;
authoritiesByUsernameQuery = DEF_AUTHORITIES_BY_USERNAME_QUERY;
}
// ~ Methods
// ========================================================================================================
/**
* Allows subclasses to add their own granted authorities to the list to be
* returned in the <code>User</code>.
*
* @param username
* the username, for use by finder methods
* @param authorities
* the current granted authorities, as populated from the
* <code>authoritiesByUsername</code> mapping
*/
protected void addCustomAuthorities(String username, List authorities) {
}
public String getAuthoritiesByUsernameQuery() {
return authoritiesByUsernameQuery;
}
public String getRolePrefix() {
return rolePrefix;
}
public String getUsersByUsernameQuery() {
return usersByUsernameQuery;
}
protected void initDao() throws ApplicationContextException {
initMappingSqlQueries();
}
/**
* Extension point to allow other MappingSqlQuery objects to be substituted
* in a subclass
*/
protected void initMappingSqlQueries() {
this.usersByUsernameMapping = new UsersByUsernameMapping(
getDataSource());
this.authoritiesByUsernameMapping = new AuthoritiesByUsernameMapping(
getDataSource());
}
public boolean isUsernameBasedPrimaryKey() {
return usernameBasedPrimaryKey;
}
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
List users = usersByUsernameMapping.execute(username);
if (users.size() == 0) {
throw new UsernameNotFoundException("User not found");
}
User user = (User) users.get(0); // contains no
// GrantedAuthority[]
List dbAuths = authoritiesByUsernameMapping.execute(user.getUsername());
addCustomAuthorities(user.getUsername(), dbAuths);
if (dbAuths.size() == 0) {
throw new UsernameNotFoundException("User has no GrantedAuthority");
}
GrantedAuthority[] arrayAuths = (GrantedAuthority[]) dbAuths
.toArray(new GrantedAuthority[dbAuths.size()]);
String returnUsername = user.getUsername();
if (!usernameBasedPrimaryKey) {
returnUsername = username;
}
return new User(returnUsername, user.getPassword(), user.isEnabled(),
true, true, true, arrayAuths, user.getUserid(), user
.getUsergroup(), user.getGroupop(),user.getObj());
}
public void setAuthoritiesByUsernameQuery(String queryString) {
authoritiesByUsernameQuery = queryString;
}
public void setRolePrefix(String rolePrefix) {
this.rolePrefix = rolePrefix;
}
public void setUsernameBasedPrimaryKey(boolean usernameBasedPrimaryKey) {
this.usernameBasedPrimaryKey = usernameBasedPrimaryKey;
}
public void setUsersByUsernameQuery(String usersByUsernameQueryString) {
this.usersByUsernameQuery = usersByUsernameQueryString;
}
// ~ Inner Classes
// ==================================================================================================
/**
* Query object to look up a user's authorities.
*/
protected class AuthoritiesByUsernameMapping extends MappingSqlQuery {
protected AuthoritiesByUsernameMapping(DataSource ds) {
super(ds, authoritiesByUsernameQuery);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
String roleName = rolePrefix + rs.getString(2);
GrantedAuthorityImpl authority = new GrantedAuthorityImpl(roleName);
return authority;
}
}
/**
* Query object to look up a user.
*/
protected class UsersByUsernameMapping extends MappingSqlQuery {
protected UsersByUsernameMapping(DataSource ds) {
super(ds, usersByUsernameQuery);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
String username = rs.getString(1);
String password = rs.getString(2);
boolean enabled = rs.getBoolean(3);
String userid = rs.getString(4);
String usergroup = rs.getString(5);
String groupop = rs.getString(6);
String table = "";
if (usergroup.equals("1")) {
table = "TbMmsCpOperator";
} else if (usergroup.equals("2")) {
table = "TbMmsCpManager";
} else if (usergroup.equals("3")) {
table = "TbMmsCpCustomerService";
} else if (usergroup.equals("4")) {
table = "TbMmsCpProvider";
}
List objlist = daosupportCp.find(" from " + table
+ " obj where obj.userId = " + userid);
Object obj = objlist.isEmpty()? null : objlist.get(0);
UserDetails user = new User(
username,
password,
enabled,
true,
true,
true,
new GrantedAuthority[] { new GrantedAuthorityImpl("HOLDER") },
userid, usergroup, groupop,obj);
return user;
}
}
public DaoSupport getDaosupportCp() {
return daosupportCp;
}
public void setDaosupportCp(DaoSupport daosupportCp) {
this.daosupportCp = daosupportCp;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?